From 534982b18d4f6cdb0b711f36b06753d1afa6e1fd Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 13 Mar 2021 21:39:59 +0100 Subject: [PATCH] Added update command --- cpl.json | 14 +-- src/cpl_cli/cli.py | 2 + src/cpl_cli/command/update_service.py | 119 ++++++++++++++++++++++++++ src/cpl_cli/startup.py | 3 + src/tests/custom/general/cpl.json | 7 +- 5 files changed, 133 insertions(+), 12 deletions(-) diff --git a/cpl.json b/cpl.json index 6816cfba..41b1effb 100644 --- a/cpl.json +++ b/cpl.json @@ -16,12 +16,14 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "mysql-connector", - "SQLAlchemy", - "termcolor", - "pyfiglet", - "tabulate", - "pynput" + "mysql-connector==2.2.9", + "psutil==5.8.0", + "pyfiglet==0.8.post1", + "pynput==1.7.3", + "SQLAlchemy==1.3.23", + "tabulate==0.8.9", + "termcolor==1.1.0", + "watchdog==2.0.2" ], "PythonVersion": ">=3.8" }, diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index 004916c3..d59d4805 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -6,6 +6,7 @@ from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.new_service import NewService from cpl_cli.command.publish_service import PublishService from cpl_cli.command.start_service import StartService +from cpl_cli.command.update_service import UpdateService from cpl_cli.command_handler_service import CommandHandler from cpl_cli.command_model import CommandModel from cpl_cli.error import Error @@ -29,6 +30,7 @@ class CLI(ApplicationABC): 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)) + self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, True)) self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False)) def main(self): diff --git a/src/cpl_cli/command/update_service.py b/src/cpl_cli/command/update_service.py index e69de29b..4a83adc7 100644 --- a/src/cpl_cli/command/update_service.py +++ b/src/cpl_cli/command/update_service.py @@ -0,0 +1,119 @@ +import json +import os +import subprocess +import sys + +from cpl.application import ApplicationRuntimeABC +from cpl.console import ForegroundColorEnum +from cpl.console.console import Console +from cpl_cli.command_abc import CommandABC +from cpl_cli.configuration.project_settings import ProjectSettings + + +class UpdateService(CommandABC): + + def __init__(self, runtime: ApplicationRuntimeABC, project_settings: ProjectSettings): + CommandABC.__init__(self) + + 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, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + + Console.write_line('\tAvailable updates for packages:') + table = str(table_str, 'utf-8').split('\n') + for row in table: + Console.write_line(f'\t{row}') + + Console.write_line(f'\tUpdate with {sys.executable} -m pip install --upgrade ') + + def _project_json_update_dependency(self, old_package: str, new_package: str): + content = '' + with open(os.path.join(self._runtime.working_directory, 'cpl.json'), 'r') as project: + content = project.read() + project.close() + + if content == '' or content == '{}': + return + + if old_package in content: + content = content.replace(old_package, new_package) + + with open(os.path.join(self._runtime.working_directory, 'cpl.json'), 'w') as project: + project.write(json.dumps(json.loads(content), indent=2)) + project.close() + + def _update_project_dependencies(self): + for package in self._project_settings.dependencies: + name = package + if '==' in package: + name = package.split('==')[0] + self._install( + name, + '--upgrade', + '--upgrade-strategy', + 'eager', + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL + ) + + self._project_json_update_dependency(package, self._get_package(name)) + + def _check_project_dependencies(self): + Console.spinner( + 'Collecting installed dependencies', self._update_project_dependencies, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + Console.write_line(f'Found {len(self._project_settings.dependencies)} dependencies.') + + def run(self, args: list[str]): + # target update discord 1.5.1 to discord 1.6.0 + self._check_project_dependencies() + + Console.spinner( + 'Checking update for sh_cpl', + self._install, 'sh_cpl', source='https://pip.sh-edraft.de', stdout=subprocess.DEVNULL, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + + self._check_outdated() + + Console.write('\n') diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index 6133065c..9abd9294 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -11,6 +11,7 @@ from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.new_service import NewService from cpl_cli.command.publish_service import PublishService from cpl_cli.command.start_service import StartService +from cpl_cli.command.update_service import UpdateService from cpl_cli.command_handler_service import CommandHandler from cpl_cli.command.help_service import HelpService from cpl_cli.command.version_service import VersionService @@ -59,6 +60,7 @@ class Startup(StartupABC): ])) self._configuration.add_console_argument(ConsoleArgument('', 'publish', ['p', 'P'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'start', ['s', 'S'], '')) + self._configuration.add_console_argument(ConsoleArgument('', 'update', ['u', 'U'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'version', ['v', 'V'], '')) self._configuration.add_console_arguments() @@ -76,6 +78,7 @@ class Startup(StartupABC): self._services.add_transient(NewService) self._services.add_transient(PublishService) self._services.add_transient(StartService) + self._services.add_transient(UpdateService) self._services.add_transient(VersionService) return self._services diff --git a/src/tests/custom/general/cpl.json b/src/tests/custom/general/cpl.json index f7b312d2..bedf2d26 100644 --- a/src/tests/custom/general/cpl.json +++ b/src/tests/custom/general/cpl.json @@ -16,12 +16,7 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "mysql-connector", - "SQLAlchemy", - "termcolor", - "pyfiglet", - "tabulate", - "pynput" + "discord.py==1.6.0" ], "PythonVersion": ">=3.8" },