From 0de725c9076a4e7cb3ceff31292c83fa95273e5a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 30 Nov 2021 11:51:45 +0100 Subject: [PATCH] Added logic to simulate install command --- .vscode/launch.json | 6 ++- src/cpl_cli/command/install_service.py | 53 ++++++++++++++++++-------- src/cpl_cli/startup.py | 5 ++- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 76a55b2b..56980bf3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -201,7 +201,11 @@ "args": [ "install", "discord.py", - ] + ], + "env": { + "GISMO_ENVIRONMENT": "development", + "PYTHONPATH": "${workspaceFolder}/src/:$PYTHONPATH" + } }, { "name": "CLI: install local", diff --git a/src/cpl_cli/command/install_service.py b/src/cpl_cli/command/install_service.py index 61d2e989..047fbee0 100644 --- a/src/cpl_cli/command/install_service.py +++ b/src/cpl_cli/command/install_service.py @@ -2,20 +2,21 @@ import json import os import subprocess import textwrap +import time -from packaging import version - -from cpl_core.configuration.configuration_abc import ConfigurationABC -from cpl_core.console.console import Console -from cpl_core.console.foreground_color_enum import ForegroundColorEnum -from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC -from cpl_core.utils.pip import Pip from cpl_cli.cli_settings import CLISettings from cpl_cli.command_abc import CommandABC from cpl_cli.configuration.build_settings import BuildSettings from cpl_cli.configuration.project_settings import ProjectSettings from cpl_cli.configuration.settings_helper import SettingsHelper from cpl_cli.error import Error +from cpl_core.configuration.configuration_abc import ConfigurationABC +from cpl_core.console.console import Console +from cpl_core.console.foreground_color_enum import ForegroundColorEnum +from cpl_core.environment.application_environment_abc import \ + ApplicationEnvironmentABC +from cpl_core.utils.pip import Pip +from packaging import version class InstallService(CommandABC): @@ -37,6 +38,9 @@ class InstallService(CommandABC): self._build_settings = build_settings self._project_settings = project_settings self._cli_settings = cli_settings + + self._is_simulating = False + self._is_virtual = False self._project_file = f'{self._config.get_configuration("ProjectName")}.json' @@ -49,6 +53,9 @@ class InstallService(CommandABC): Arguments: package The package to install """) + + def _wait(self, t: int, *args, source: str = None, stdout=None, stderr=None): + time.sleep(t) def _install_project(self): """ @@ -64,11 +71,12 @@ class InstallService(CommandABC): Error.error(f'Found invalid dependencies in {self._project_file}.') return - Pip.set_executable(self._project_settings.python_executable) + if not self._is_virtual: + Pip.set_executable(self._project_settings.python_executable) for dependency in self._project_settings.dependencies: Console.spinner( f'Installing: {dependency}', - Pip.install, dependency, + Pip.install if not self._is_virtual else self._wait, dependency if not self._is_virtual else 2, source=self._cli_settings.pip_path if 'sh_cpl' in dependency else None, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, @@ -76,7 +84,8 @@ class InstallService(CommandABC): spinner_foreground_color=ForegroundColorEnum.cyan ) - Pip.reset_executable() + if not self._is_virtual: + Pip.reset_executable() def _install_package(self, package: str): """ @@ -85,7 +94,8 @@ class InstallService(CommandABC): :return: """ is_already_in_project = False - Pip.set_executable(self._project_settings.python_executable) + if not self._is_virtual: + Pip.set_executable(self._project_settings.python_executable) if self._project_settings is None or self._build_settings is None: Error.error('The command requires to be run in an CPL project, but a project could not be found.') @@ -129,15 +139,18 @@ class InstallService(CommandABC): Console.spinner( f'Installing: {package}', - Pip.install, package, + Pip.install if not self._is_virtual else self._wait, package if not self._is_virtual else 2, source=self._cli_settings.pip_path if 'sh_cpl' in package else None, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, text_foreground_color=ForegroundColorEnum.green, spinner_foreground_color=ForegroundColorEnum.cyan ) - new_package = Pip.get_package(name) - Console.write_line(new_package) + + if self._is_virtual: + new_package = name + else: + new_package = Pip.get_package(name) if new_package is None \ or '==' in package and \ version.parse(package.split('==')[1]) != version.parse(new_package.split('==')[1]): @@ -163,7 +176,7 @@ class InstallService(CommandABC): ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings), BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings) } - + with open(os.path.join(self._env.working_directory, self._project_file), 'w') as project_file: project_file.write(json.dumps(config, indent=2)) project_file.close() @@ -176,6 +189,16 @@ class InstallService(CommandABC): :param args: :return: """ + if '--virtual' in args: + self._is_virtual = True + args.remove('--virtual') + Console.write_line('Running in virtual mode:') + + if '--simulate' in args: + self._is_virtual = True + args.remove('--simulate') + Console.write_line('Running in simulation mode:') + if len(args) == 0: self._install_project() else: diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index 1148b66a..e3a67712 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -55,7 +55,10 @@ class Startup(StartupABC): ConsoleArgument('', 'help', ['h', 'H'], ' ', is_value_token_optional=True) ) configuration.add_console_argument( - ConsoleArgument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True) + ConsoleArgument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True, console_arguments= [ + ConsoleArgument('', '--virtual', ['--v', '--V'], ''), + ConsoleArgument('', '--simulate', ['--s', '--S'], ''), + ]) ) configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', console_arguments=[ ConsoleArgument('', 'console', ['c', 'C'], ' '),