2021.4 #19

Merged
edraft merged 237 commits from 2021.4 into master 2021-04-01 10:13:33 +02:00
15 changed files with 234 additions and 90 deletions
Showing only changes of commit 6094b11068 - Show all commits

View File

@ -1,2 +1,3 @@
- add command start
- add command update
- add command install
- no args install deps from proj
- args install packages

View File

@ -1,11 +1,18 @@
class ConsoleArgument:
def __init__(self, token: str, name: str, aliases: list[str], value_token: str, console_arguments: list[
'ConsoleArgument'] = None):
def __init__(self,
token: str,
name: str,
aliases: list[str],
value_token: str,
is_value_token_optional: bool = None,
console_arguments: list['ConsoleArgument'] = None
):
self._token = token
self._name = name
self._aliases = aliases
self._value_token = value_token
self._is_value_token_optional = is_value_token_optional
self._console_arguments = console_arguments
@property
@ -24,6 +31,10 @@ class ConsoleArgument:
def value_token(self) -> str:
return self._value_token
@property
def is_value_token_optional(self) -> bool:
return self._is_value_token_optional
@property
def console_arguments(self) -> list['ConsoleArgument']:
return self._console_arguments

32
src/cpl/utils/pip.py Normal file
View File

@ -0,0 +1,32 @@
import subprocess
import sys
class Pip:
@staticmethod
def get_package(package: str) -> str:
result = subprocess.check_output([sys.executable, "-m", "pip", "show", package])
new_package: list[str] = str(result, 'utf-8').lower().split('\n')
new_version = ''
for atr in new_package:
if 'version' in atr:
new_version = atr.split(': ')[1]
return f'{package}=={new_version}'
@staticmethod
def install(package: str, *args, source: str = None, stdout=None, stderr=None):
pip_args = [sys.executable, "-m", "pip", "install"]
for arg in args:
pip_args.append(arg)
if source is not None:
pip_args.append(f'--extra-index-url')
pip_args.append(source)
pip_args.append(package)
subprocess.run(pip_args, stdout=stdout, stderr=stderr)

View File

@ -3,6 +3,7 @@ from typing import Optional
from cpl.application.application_abc import ApplicationABC
from cpl_cli.command.build_service import BuildService
from cpl_cli.command.generate_service import GenerateService
from cpl_cli.command.install_service import InstallService
from cpl_cli.command.new_service import NewService
from cpl_cli.command.publish_service import PublishService
from cpl_cli.command.start_service import StartService
@ -27,6 +28,7 @@ class CLI(ApplicationABC):
self._command_handler.add_command(CommandModel('build', ['h', 'B'], BuildService, True))
self._command_handler.add_command(CommandModel('generate', ['g', 'G'], GenerateService, True))
self._command_handler.add_command(CommandModel('help', ['h', 'H'], HelpService, False))
self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, False))
self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False))
self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, True))
self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, True))
@ -34,8 +36,21 @@ class CLI(ApplicationABC):
self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False))
def main(self):
if len(self._configuration.additional_arguments) < 1:
command = None
args = []
if len(self._configuration.additional_arguments) > 0:
command = self._configuration.additional_arguments[0]
if len(self._configuration.additional_arguments) > 1:
args = self._configuration.additional_arguments[1:]
else:
for cmd in self._command_handler.commands:
result = self._configuration.get_configuration(cmd.name)
if result is not None:
command = cmd.name
args.append(result)
if command is None:
Error.error(f'Expected command')
return
self._command_handler.handle(self._configuration.additional_arguments[0], self._configuration.additional_arguments[1:])
self._command_handler.handle(command, args)

View File

@ -0,0 +1,99 @@
import json
import os
import subprocess
import sys
from typing import Optional
from cpl.application import ApplicationRuntimeABC
from cpl.configuration import ConfigurationABC
from cpl.console.console import Console
from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl.utils.pip import Pip
from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration import ProjectSettingsNameEnum, VersionSettingsNameEnum, BuildSettingsNameEnum
from cpl_cli.configuration.build_settings import BuildSettings
from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.error import Error
class InstallService(CommandABC):
def __init__(self, runtime: ApplicationRuntimeABC, configuration: ConfigurationABC):
CommandABC.__init__(self)
self._runtime = runtime
self._config = configuration
def _install_project(self):
pass
@staticmethod
def _get_project_settings_dict(project: ProjectSettings) -> dict:
return {
ProjectSettingsNameEnum.name.value: project.name,
ProjectSettingsNameEnum.version.value: {
VersionSettingsNameEnum.major.value: project.version.major,
VersionSettingsNameEnum.minor.value: project.version.minor,
VersionSettingsNameEnum.micro.value: project.version.micro
},
ProjectSettingsNameEnum.author.value: project.author,
ProjectSettingsNameEnum.author_email.value: project.author_email,
ProjectSettingsNameEnum.description.value: project.description,
ProjectSettingsNameEnum.long_description.value: project.long_description,
ProjectSettingsNameEnum.url.value: project.url,
ProjectSettingsNameEnum.copyright_date.value: project.copyright_date,
ProjectSettingsNameEnum.copyright_name.value: project.copyright_name,
ProjectSettingsNameEnum.license_name.value: project.license_name,
ProjectSettingsNameEnum.license_description.value: project.license_description,
ProjectSettingsNameEnum.dependencies.value: project.dependencies,
ProjectSettingsNameEnum.python_version.value: project.python_version
}
@staticmethod
def _get_build_settings_dict(build: BuildSettings) -> dict:
return {
BuildSettingsNameEnum.source_path.value: build.source_path,
BuildSettingsNameEnum.output_path.value: build.output_path,
BuildSettingsNameEnum.main.value: build.main,
BuildSettingsNameEnum.entry_point.value: build.entry_point,
BuildSettingsNameEnum.include_package_data.value: build.include_package_data,
BuildSettingsNameEnum.included.value: build.included,
BuildSettingsNameEnum.excluded.value: build.excluded,
BuildSettingsNameEnum.package_data.value: build.package_data
}
def _install_package(self, package: str):
Console.spinner(
f'Installing: {package}',
Pip.install, package,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
)
new_package = Pip.get_package(package)
project: ProjectSettings = self._config.get_configuration(ProjectSettings)
build: BuildSettings = self._config.get_configuration(BuildSettings)
if project is None or build is None:
Error.error('The command requires to be run in an CPL project, but a project could not be found.')
return
project.dependencies.append(new_package)
config = {
ProjectSettings.__name__: self._get_project_settings_dict(project),
BuildSettings.__name__: self._get_build_settings_dict(build)
}
with open(os.path.join(self._runtime.working_directory, 'cpl.json'), 'w') as project_file:
project_file.write(json.dumps(config, indent=2))
project_file.close()
def run(self, args: list[str]):
if len(args) == 0:
self._install_project()
else:
self._install_package(args[0])
Console.write('\n')

View File

@ -10,9 +10,9 @@ from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl.console.console import Console
from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration.build_settings import BuildSettings
from cpl_cli.configuration.build_settings_name_enum import BuildSettingsName
from cpl_cli.configuration.build_settings_name_enum import BuildSettingsNameEnum
from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsName
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
from cpl_cli.configuration.version_settings_name_enum import VersionSettingsNameEnum
from cpl_cli.templates.new.console.license import LicenseTemplate
from cpl_cli.templates.new.console.readme_py import ReadmeTemplate
@ -44,41 +44,41 @@ class NewService(CommandABC):
def _create_project_settings(self, name: str):
self._project_dict = {
ProjectSettingsName.name.value: name,
ProjectSettingsName.version.value: {
ProjectSettingsNameEnum.name.value: name,
ProjectSettingsNameEnum.version.value: {
VersionSettingsNameEnum.major.value: '0',
VersionSettingsNameEnum.minor.value: '0',
VersionSettingsNameEnum.micro.value: '0'
},
ProjectSettingsName.author.value: '',
ProjectSettingsName.author_email.value: '',
ProjectSettingsName.description.value: '',
ProjectSettingsName.long_description.value: '',
ProjectSettingsName.url.value: '',
ProjectSettingsName.copyright_date.value: '',
ProjectSettingsName.copyright_name.value: '',
ProjectSettingsName.license_name.value: '',
ProjectSettingsName.license_description.value: '',
ProjectSettingsName.dependencies.value: [],
ProjectSettingsName.python_version.value: f'>={sys.version.split(" ")[0]}'
ProjectSettingsNameEnum.author.value: '',
ProjectSettingsNameEnum.author_email.value: '',
ProjectSettingsNameEnum.description.value: '',
ProjectSettingsNameEnum.long_description.value: '',
ProjectSettingsNameEnum.url.value: '',
ProjectSettingsNameEnum.copyright_date.value: '',
ProjectSettingsNameEnum.copyright_name.value: '',
ProjectSettingsNameEnum.license_name.value: '',
ProjectSettingsNameEnum.license_description.value: '',
ProjectSettingsNameEnum.dependencies.value: [],
ProjectSettingsNameEnum.python_version.value: f'>={sys.version.split(" ")[0]}'
}
self._project.from_dict(self._project_dict)
def _create_build_settings(self):
self._build_dict = {
BuildSettingsName.source_path.value: 'src',
BuildSettingsName.output_path.value: 'dist',
BuildSettingsName.main.value: 'main',
BuildSettingsName.entry_point.value: self._project.name,
BuildSettingsName.include_package_data.value: 'False',
BuildSettingsName.included.value: [],
BuildSettingsName.excluded.value: [
BuildSettingsNameEnum.source_path.value: 'src',
BuildSettingsNameEnum.output_path.value: 'dist',
BuildSettingsNameEnum.main.value: 'main',
BuildSettingsNameEnum.entry_point.value: self._project.name,
BuildSettingsNameEnum.include_package_data.value: 'False',
BuildSettingsNameEnum.included.value: [],
BuildSettingsNameEnum.excluded.value: [
'*/__pycache__',
'*/logs',
'*/tests'
],
BuildSettingsName.package_data.value: {}
BuildSettingsNameEnum.package_data.value: {}
}
self._build.from_dict(self._build_dict)

View File

@ -6,6 +6,7 @@ import sys
from cpl.application import ApplicationRuntimeABC
from cpl.console import ForegroundColorEnum
from cpl.console.console import Console
from cpl.utils.pip import Pip
from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration.project_settings import ProjectSettings
@ -18,37 +19,10 @@ class UpdateService(CommandABC):
self._runtime = runtime
self._project_settings = project_settings
@staticmethod
def _install(package: str, *args, source: str = None, stdout=None, stderr=None):
pip_args = [sys.executable, "-m", "pip", "install"]
for arg in args:
pip_args.append(arg)
if source is not None:
pip_args.append(f'--extra-index-url')
pip_args.append(source)
pip_args.append(package)
subprocess.run(pip_args, stdout=stdout, stderr=stderr)
@staticmethod
def _get_outdated() -> bytes:
return subprocess.check_output([sys.executable, "-m", "pip", "list", "--outdated"])
@staticmethod
def _get_package(package: str) -> str:
result = subprocess.check_output([sys.executable, "-m", "pip", "show", package])
new_package: list[str] = str(result, 'utf-8').lower().split('\n')
new_version = ''
for atr in new_package:
if 'version' in atr:
new_version = atr.split(': ')[1]
return f'{package}=={new_version}'
def _check_outdated(self):
table_str: bytes = Console.spinner(
'Analyzing for available package updates', self._get_outdated,
@ -84,7 +58,7 @@ class UpdateService(CommandABC):
name = package
if '==' in package:
name = package.split('==')[0]
self._install(
Pip.install(
name,
'--upgrade',
'--upgrade-strategy',
@ -93,7 +67,7 @@ class UpdateService(CommandABC):
stderr=subprocess.DEVNULL
)
self._project_json_update_dependency(package, self._get_package(name))
self._project_json_update_dependency(package, Pip.get_package(name))
def _check_project_dependencies(self):
Console.spinner(
@ -109,7 +83,10 @@ class UpdateService(CommandABC):
Console.spinner(
'Checking update for sh_cpl',
self._install, 'sh_cpl', source='https://pip.sh-edraft.de', stdout=subprocess.DEVNULL,
Pip.install, 'sh_cpl',
source='https://pip.sh-edraft.de',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
)

View File

@ -17,6 +17,10 @@ class CommandHandler(ServiceABC):
self._commands: list[CommandModel] = []
@property
def commands(self) -> list[CommandModel]:
return self._commands
def add_command(self, cmd: CommandModel):
self._commands.append(cmd)

View File

@ -21,9 +21,9 @@ from collections import namedtuple
# imports:
from .build_settings import BuildSettings
from .build_settings_name_enum import BuildSettingsName
from .build_settings_name_enum import BuildSettingsNameEnum
from .project_settings import ProjectSettings
from .project_settings_name_enum import ProjectSettingsName
from .project_settings_name_enum import ProjectSettingsNameEnum
from .version_settings import VersionSettings
from .version_settings_name_enum import VersionSettingsNameEnum

View File

@ -4,7 +4,7 @@ from typing import Optional
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
from cpl.console.console import Console
from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl_cli.configuration.build_settings_name_enum import BuildSettingsName
from cpl_cli.configuration.build_settings_name_enum import BuildSettingsNameEnum
class BuildSettings(ConfigurationModelABC):
@ -55,14 +55,14 @@ class BuildSettings(ConfigurationModelABC):
def from_dict(self, settings: dict):
try:
self._source_path = settings[BuildSettingsName.source_path.value]
self._output_path = settings[BuildSettingsName.output_path.value]
self._include_package_data = bool(settings[BuildSettingsName.include_package_data.value])
self._main = settings[BuildSettingsName.main.value]
self._entry_point = settings[BuildSettingsName.entry_point.value]
self._included = settings[BuildSettingsName.included.value]
self._excluded = settings[BuildSettingsName.excluded.value]
self._package_data = settings[BuildSettingsName.package_data.value]
self._source_path = settings[BuildSettingsNameEnum.source_path.value]
self._output_path = settings[BuildSettingsNameEnum.output_path.value]
self._include_package_data = bool(settings[BuildSettingsNameEnum.include_package_data.value])
self._main = settings[BuildSettingsNameEnum.main.value]
self._entry_point = settings[BuildSettingsNameEnum.entry_point.value]
self._included = settings[BuildSettingsNameEnum.included.value]
self._excluded = settings[BuildSettingsNameEnum.excluded.value]
self._package_data = settings[BuildSettingsNameEnum.package_data.value]
except Exception as e:
Console.set_foreground_color(ForegroundColorEnum.red)
Console.write_line(

View File

@ -1,7 +1,7 @@
from enum import Enum
class BuildSettingsName(Enum):
class BuildSettingsNameEnum(Enum):
source_path = 'SourcePath'
output_path = 'OutputPath'

View File

@ -5,7 +5,7 @@ from cpl.configuration.configuration_model_abc import ConfigurationModelABC
from cpl.console.console import Console
from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl_cli.configuration.version_settings import VersionSettings
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsName
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
class ProjectSettings(ConfigurationModelABC):
@ -81,19 +81,19 @@ class ProjectSettings(ConfigurationModelABC):
def from_dict(self, settings: dict):
try:
self._name = settings[ProjectSettingsName.name.value]
self._version.from_dict(settings[ProjectSettingsName.version.value])
self._author = settings[ProjectSettingsName.author.value]
self._author_email = settings[ProjectSettingsName.author_email.value]
self._description = settings[ProjectSettingsName.description.value]
self._long_description = settings[ProjectSettingsName.long_description.value]
self._url = settings[ProjectSettingsName.url.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._dependencies = settings[ProjectSettingsName.dependencies.value]
self._python_version = settings[ProjectSettingsName.python_version.value]
self._name = settings[ProjectSettingsNameEnum.name.value]
self._version.from_dict(settings[ProjectSettingsNameEnum.version.value])
self._author = settings[ProjectSettingsNameEnum.author.value]
self._author_email = settings[ProjectSettingsNameEnum.author_email.value]
self._description = settings[ProjectSettingsNameEnum.description.value]
self._long_description = settings[ProjectSettingsNameEnum.long_description.value]
self._url = settings[ProjectSettingsNameEnum.url.value]
self._copyright_date = settings[ProjectSettingsNameEnum.copyright_date.value]
self._copyright_name = settings[ProjectSettingsNameEnum.copyright_name.value]
self._license_name = settings[ProjectSettingsNameEnum.license_name.value]
self._license_description = settings[ProjectSettingsNameEnum.license_description.value]
self._dependencies = settings[ProjectSettingsNameEnum.dependencies.value]
self._python_version = settings[ProjectSettingsNameEnum.python_version.value]
except Exception as e:
Console.set_foreground_color(ForegroundColorEnum.red)
Console.write_line(

View File

@ -1,7 +1,7 @@
from enum import Enum
class ProjectSettingsName(Enum):
class ProjectSettingsNameEnum(Enum):
name = 'Name'
version = 'Version'

View File

@ -8,6 +8,7 @@ from cpl.configuration.configuration_abc import ConfigurationABC
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
from cpl_cli.command.build_service import BuildService
from cpl_cli.command.generate_service import GenerateService
from cpl_cli.command.install_service import InstallService
from cpl_cli.command.new_service import NewService
from cpl_cli.command.publish_service import PublishService
from cpl_cli.command.start_service import StartService
@ -46,7 +47,7 @@ class Startup(StartupABC):
self._configuration.add_environment_variables('CPL_')
self._configuration.add_json_file('cpl.json', optional=True, output=False)
self._configuration.add_console_argument(ConsoleArgument('', 'build', ['b', 'B'], ''))
self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', [
self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', console_arguments=[
ConsoleArgument('', 'abc', ['a', 'A'], ' '),
ConsoleArgument('', 'class', ['c', 'C'], ' '),
ConsoleArgument('', 'enum', ['e', 'E'], ' '),
@ -55,7 +56,10 @@ class Startup(StartupABC):
ConsoleArgument('', 'thread', ['t', 't'], ' ')
]))
self._configuration.add_console_argument(ConsoleArgument('', 'help', ['h', 'H'], ''))
self._configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', [
self._configuration.add_console_argument(
ConsoleArgument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True)
)
self._configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', console_arguments=[
ConsoleArgument('', 'console', ['c', 'C'], ' ')
]))
self._configuration.add_console_argument(ConsoleArgument('', 'publish', ['p', 'P'], ''))
@ -75,6 +79,7 @@ class Startup(StartupABC):
self._services.add_transient(BuildService)
self._services.add_transient(GenerateService)
self._services.add_transient(HelpService)
self._services.add_transient(InstallService)
self._services.add_transient(NewService)
self._services.add_transient(PublishService)
self._services.add_transient(StartService)

View File

@ -25,7 +25,7 @@
"OutputPath": "dist",
"Main": "main",
"EntryPoint": "cpl",
"IncludePackageData": "False",
"IncludePackageData": true,
"Included": [
"*/templates"
],