Added build command

This commit is contained in:
Sven Heidemann 2021-03-04 21:57:18 +01:00
parent 700bf9c937
commit 83d0451591
10 changed files with 333 additions and 30 deletions

View File

@ -1 +1,26 @@
{}
{
"ProjectSettings": {
"Name": "sh_cpl",
"Author": "Sven Heidemann",
"Description": "Common Python Library",
"LongDescription": "Common Python Library",
"CopyrightDate": "2020",
"CopyrightName": "sh-edraft.de",
"LicenseName": "MIT",
"LicenseDescription": ", see LICENSE for more details.",
"Version": {
"Major": "2021",
"Minor": "04",
"Micro": "01"
},
"SourcePath": "./cpl",
"DistPath": "../dist",
"Included": [
"./cpl_cli"
],
"Excluded": [
"__pycache__",
"logs"
]
}
}

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
sh_cpl Common Python Library
~~~~~~~~~~~~~~~~~~~
Common Python Library
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'sh_cpl.cpl_cli'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
__version__ = '2021.4.1'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2021, minor=4, micro=1)

View File

@ -1,14 +1,12 @@
from typing import Optional
from cpl.application.application_abc import ApplicationABC
from cpl.console.console import Console
from cpl_cli.command.build import Build
from cpl_cli.command_handler import CommandHandler
from cpl_cli.command_model import CommandModel
from cpl_cli.error import Error
from cpl_cli.command.help import Help
from cpl_cli.command.version import Version
from cpl_cli.publish.project_settings import ProjectSettings
class CLI(ApplicationABC):

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
sh_cpl Common Python Library
~~~~~~~~~~~~~~~~~~~
Common Python Library
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_cli.publish'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
__version__ = '2021.4.1'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2021, minor=4, micro=1)

View File

@ -13,13 +13,63 @@ class ProjectSettings(ConfigurationModelABC):
def __init__(self):
ConfigurationModelABC.__init__(self)
self._name: Optional[str] = None
self._author: Optional[str] = None
self._description: Optional[str] = None
self._long_description: Optional[str] = None
self._copyright_date: Optional[str] = None
self._copyright_name: Optional[str] = None
self._license_name: Optional[str] = None
self._license_description: Optional[str] = None
self._version: Optional[Version] = Version()
self._source_path: Optional[str] = None
self._dist_path: Optional[str] = None
self._excluded_files: list[str] = []
self._version: Optional[Version] = None
self._included: list[str] = []
self._excluded: list[str] = []
@property
def excluded_files(self) -> list[str]:
return self._excluded_files
def name(self) -> Optional[str]:
return self._name
@property
def author(self) -> Optional[str]:
return self._author
@property
def description(self) -> Optional[str]:
return self._description
@property
def long_description(self) -> Optional[str]:
return self._long_description
@property
def copyright_date(self) -> Optional[str]:
return self._copyright_date
@property
def copyright_name(self) -> Optional[str]:
return self._copyright_name
@property
def license_name(self) -> Optional[str]:
return self._license_name
@property
def license_description(self) -> Optional[str]:
return self._license_description
@property
def version(self) -> Optional[Version]:
return self._version
@property
def source_path(self) -> str:
return self._source_path
@source_path.setter
def source_path(self, source_path: str):
self._source_path = source_path
@property
def dist_path(self) -> str:
@ -30,14 +80,28 @@ class ProjectSettings(ConfigurationModelABC):
self._dist_path = dist_path
@property
def version(self) -> Version:
return self._version
def included(self) -> list[str]:
return self._included
@property
def excluded(self) -> list[str]:
return self._excluded
def from_dict(self, settings: dict):
try:
self._name = settings[ProjectSettingsName.name.value]
self._author = settings[ProjectSettingsName.author.value]
self._description = settings[ProjectSettingsName.description.value]
self._long_description = settings[ProjectSettingsName.long_description.value]
self._copyright_date = settings[ProjectSettingsName.copyright_date.value]
self._copyright_name = settings[ProjectSettingsName.copyright_name.value]
self._license_name = settings[ProjectSettingsName.license_name.value]
self._license_description = settings[ProjectSettingsName.license_description.value]
self._version.from_dict(settings[ProjectSettingsName.version.value])
self._source_path = settings[ProjectSettingsName.source_path.value]
self._dist_path = settings[ProjectSettingsName.dist_path.value]
self._excluded_files = settings[ProjectSettingsName.excluded_files.value]
self._version = settings[ProjectSettingsName.version.value]
self._included = settings[ProjectSettingsName.included.value]
self._excluded = settings[ProjectSettingsName.excluded.value]
except Exception as e:
Console.set_foreground_color(ForegroundColor.red)
Console.write_line(

View File

@ -3,7 +3,17 @@ from enum import Enum
class ProjectSettingsName(Enum):
dist_path = 'DistPath'
excluded_files = 'ExcludedFiles'
version = 'Version'
project = 'Project'
name = 'Name'
author = 'Author'
description = 'Description'
long_description = 'LongDescription'
copyright_date = 'CopyrightDate'
copyright_name = 'CopyrightName'
license_name = 'LicenseName'
license_description = 'LicenseDescription'
version = 'Version'
source_path = 'SourcePath'
dist_path = 'DistPath'
included = 'Included'
excluded = 'Excluded'

View File

@ -1,5 +1,6 @@
import os
import shutil
from string import Template as stringTemplate
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.console.console import Console
@ -13,8 +14,11 @@ class Publisher(PublisherABC):
PublisherABC.__init__(self)
self._runtime = runtime
self._project = project
self._project.source_path = os.path.join(self._runtime.working_directory, self._project.source_path)
self._project.dist_path = os.path.join(self._runtime.working_directory, self._project.dist_path)
self._included_files: list[str] = []
@property
def source_path(self) -> str:
@ -24,30 +28,144 @@ class Publisher(PublisherABC):
def dist_path(self) -> str:
return ''
def _create_dist_path(self):
self._project.dist_path = os.path.join(self._runtime.working_directory, self._project.dist_path)
@staticmethod
def _get_module_name_from_dirs(file: str) -> str:
dirs = os.path.dirname(file).split('/')
for d in dirs:
if d.__contains__('.'):
dirs.remove(d)
Console.write_line('DIST:', self._project.dist_path)
if len(dirs) == 0:
return os.path.basename(file)
else:
return '.'.join(dirs)
if os.path.isdir(self._project.dist_path):
@staticmethod
def _delete_path(path: str):
if os.path.isdir(path):
try:
shutil.rmtree(self._project.dist_path)
shutil.rmtree(path)
except Exception as e:
Console.error(f'{e}')
exit()
if not os.path.isdir(self._project.dist_path):
@staticmethod
def _create_path(path: str):
if not os.path.isdir(path):
try:
os.makedirs(self._project.dist_path)
os.makedirs(path)
except Exception as e:
Console.error(f'{e}')
exit()
def _read_sources(self):
for file in self._project.included:
rel_path = os.path.relpath(file)
if os.path.isdir(rel_path):
for r, d, f in os.walk(rel_path):
for sub_file in f:
relative_path = os.path.relpath(r)
file_path = os.path.join(relative_path, os.path.relpath(sub_file))
is_excluded = False
for excluded in self._project.excluded:
if excluded in relative_path:
is_excluded = True
if relative_path in excluded:
is_excluded = True
if not is_excluded:
self._included_files.append(os.path.relpath(file_path))
elif os.path.isfile(rel_path):
self._included_files.append(rel_path)
else:
Console.error(f'Path not found: {rel_path}')
for r, d, f in os.walk(self._project.source_path):
for file in f:
relative_path = os.path.relpath(r)
file_path = os.path.join(relative_path, os.path.relpath(file))
if relative_path not in self._project.excluded:
self._included_files.append(os.path.relpath(file_path))
def _create_packages(self):
pass
for file in self._included_files:
if file.endswith('__init__.py'):
template_content = ''
module_file_lines: list[str] = []
title = self._get_module_name_from_dirs(file)
if title == '':
title = self._project.name
elif not title.__contains__('.'):
title = f'{self._project.name}.{title}'
module_py_lines: list[str] = []
imports = ''
with open(file, 'r') as py_file:
module_file_lines = py_file.readlines()
py_file.close()
if len(module_file_lines) == 0:
imports = '# imports:'
else:
is_started = False
for line in module_file_lines:
if line.__contains__('# imports'):
is_started = True
if (line.__contains__('from') or line.__contains__('import')) and is_started:
module_py_lines.append(line.replace('\n', ''))
if len(module_py_lines) > 0:
imports = '\n'.join(module_py_lines)
with open(os.path.join(self._runtime.runtime_directory, 'templates/build/init.txt'), 'r') as template:
template_content = stringTemplate(template.read()).substitute(
Name=self._project.name,
Description=self._project.description,
LongDescription=self._project.long_description,
CopyrightDate=self._project.copyright_date,
CopyrightName=self._project.copyright_name,
LicenseName=self._project.license_name,
LicenseDescription=self._project.license_description,
Title=title if title is not None and title != '' else self._project.name,
Author=self._project.author,
Version=self._project.version.to_str(),
Major=self._project.version.major,
Minor=self._project.version.minor,
Micro=self._project.version.micro,
Imports=imports
)
with open(file, 'w+') as py_file:
py_file.write(template_content)
py_file.close()
def _dist_files(self):
pass
build_path = os.path.join(self._project.dist_path, 'build')
self._delete_path(build_path)
self._create_path(build_path)
for file in self._included_files:
output_path = os.path.join(build_path, os.path.dirname(file))
output_file = os.path.join(build_path, file)
try:
if not os.path.isdir(output_path):
os.makedirs(output_path, exist_ok=True)
except Exception as e:
Console.error(__name__, f'Cannot create directories: {output_path} -> {e}')
try:
shutil.copy(os.path.abspath(file), output_file)
except Exception as e:
Console.error(__name__, f'Cannot copy file: {file} to {output_path} -> {e}')
def include(self, path: str):
pass
@ -56,6 +174,7 @@ class Publisher(PublisherABC):
pass
def build(self):
self._read_sources()
Console.write_line('Creating internal packages:')
self._create_packages()
Console.write_line('Building application:')

View File

@ -4,7 +4,6 @@ from cpl.application.application_host import ApplicationHost
from cpl.application.application_host_abc import ApplicationHostABC
from cpl.application.startup_abc import StartupABC
from cpl.configuration.configuration_abc import ConfigurationABC
from cpl.console.console import Console
from cpl.dependency_injection.service_provider_base import ServiceProviderABC
from cpl_cli.command.build import Build
from cpl_cli.command_handler import CommandHandler

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
$Name $Description
~~~~~~~~~~~~~~~~~~~
$LongDescription
:copyright: (c) $CopyrightDate $CopyrightName
:license: $LicenseName$LicenseDescription
"""
__title__ = '$Title'
__author__ = '$Author'
__license__ = '$LicenseName'
__copyright__ = 'Copyright (c) $CopyrightDate $CopyrightName'
__version__ = '$Version'
from collections import namedtuple
$Imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=$Major, minor=$Minor, micro=$Micro)

View File

@ -1,11 +1,24 @@
{
"ProjectSettings": {
"Name": "sh_cpl.tests",
"Author": "Sven Heidemann",
"Description": "Common Python Library CLI",
"LongDescription": "Common Python Library CLI",
"CopyrightDate": "2020",
"CopyrightName": "sh-edraft.de",
"LicenseName": "MIT",
"LicenseDescription": ", see LICENSE for more details.",
"Version": {
"major": "2021",
"minor": "04",
"micro": "01"
"Major": "2021",
"Minor": "04",
"Micro": "01"
},
"ExcludedFiles": [],
"DistPath": "../../dist"
"SourcePath": "./",
"DistPath": "../../dist",
"Included": [],
"Excluded": [
"__pycache__",
"logs"
]
}
}