2022.6.2 - Venv support #85

Merged
edraft merged 2 commits from 2022.6.2 into 2022.6 2022-06-27 11:52:18 +02:00
14 changed files with 226 additions and 112 deletions

View File

@ -1,5 +1,6 @@
import json import json
import os import os
import shutil
import subprocess import subprocess
import textwrap import textwrap
import time import time
@ -9,12 +10,12 @@ from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration.build_settings import BuildSettings from cpl_cli.configuration.build_settings import BuildSettings
from cpl_cli.configuration.project_settings import ProjectSettings from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.configuration.settings_helper import SettingsHelper from cpl_cli.configuration.settings_helper import SettingsHelper
from cpl_cli.configuration.venv_helper_service import VenvHelper
from cpl_cli.error import Error from cpl_cli.error import Error
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_core.environment.application_environment_abc import \ from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
ApplicationEnvironmentABC
from cpl_core.utils.pip import Pip from cpl_core.utils.pip import Pip
from packaging import version from packaging import version
@ -62,7 +63,6 @@ class InstallService(CommandABC):
Installs dependencies of CPl project Installs dependencies of CPl project
:return: :return:
""" """
if self._project_settings is None or self._build_settings is None: 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.') Error.error('The command requires to be run in an CPL project, but a project could not be found.')
return return
@ -71,8 +71,6 @@ class InstallService(CommandABC):
Error.error(f'Found invalid dependencies in {self._project_file}.') Error.error(f'Found invalid dependencies in {self._project_file}.')
return return
if not self._is_virtual:
Pip.set_executable(self._project_settings.python_executable)
for dependency in self._project_settings.dependencies: for dependency in self._project_settings.dependencies:
Console.spinner( Console.spinner(
f'Installing: {dependency}', f'Installing: {dependency}',
@ -94,9 +92,6 @@ class InstallService(CommandABC):
:return: :return:
""" """
is_already_in_project = False is_already_in_project = False
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: 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.') Error.error('The command requires to be run in an CPL project, but a project could not be found.')
return return
@ -200,7 +195,12 @@ class InstallService(CommandABC):
args.remove('simulate') args.remove('simulate')
Console.write_line('Running in simulation mode:') Console.write_line('Running in simulation mode:')
VenvHelper.init_venv(self._is_virtual, self._env, self._project_settings)
if len(args) == 0: if len(args) == 0:
self._install_project() self._install_project()
else: else:
self._install_package(args[0]) self._install_package(args[0])
if not self._is_virtual:
Pip.reset_executable()

View File

@ -6,6 +6,7 @@ from typing import Optional
from packaging import version from packaging import version
import cpl_core import cpl_core
from cpl_cli.configuration.venv_helper_service import VenvHelper
from cpl_cli.source_creator.unittest_builder import UnittestBuilder from cpl_cli.source_creator.unittest_builder import UnittestBuilder
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
@ -36,7 +37,7 @@ class NewService(CommandABC):
self._config = configuration self._config = configuration
self._env = self._config.environment self._env = self._config.environment
self._workspace = self._config.get_configuration(WorkspaceSettings) self._workspace: WorkspaceSettings = self._config.get_configuration(WorkspaceSettings)
self._project: ProjectSettings = ProjectSettings() self._project: ProjectSettings = ProjectSettings()
self._project_dict = {} self._project_dict = {}
self._build: BuildSettings = BuildSettings() self._build: BuildSettings = BuildSettings()
@ -51,6 +52,7 @@ class NewService(CommandABC):
self._use_startup: bool = False self._use_startup: bool = False
self._use_service_providing: bool = False self._use_service_providing: bool = False
self._use_async: bool = False self._use_async: bool = False
self._use_venv: bool = False
@property @property
def help_message(self) -> str: def help_message(self) -> str:
@ -107,7 +109,7 @@ class NewService(CommandABC):
], ],
ProjectSettingsNameEnum.python_version.value: f'>={sys.version.split(" ")[0]}', ProjectSettingsNameEnum.python_version.value: f'>={sys.version.split(" ")[0]}',
ProjectSettingsNameEnum.python_path.value: { ProjectSettingsNameEnum.python_path.value: {
sys.platform: '' sys.platform: '../../venv/bin/python' if self._use_venv else ''
}, },
ProjectSettingsNameEnum.classifiers.value: [] ProjectSettingsNameEnum.classifiers.value: []
} }
@ -275,6 +277,22 @@ class NewService(CommandABC):
except Exception as e: except Exception as e:
Console.error('Could not create project', str(e)) Console.error('Could not create project', str(e))
def _create_venv(self):
project = self._project.name
if self._workspace is not None:
project = self._workspace.default_project
if self._env.working_directory.endswith(project):
project = ''
VenvHelper.init_venv(
False,
self._env,
self._project,
explicit_path=os.path.join(self._env.working_directory, project, self._project.python_executable.replace('../', ''))
)
def execute(self, args: list[str]): def execute(self, args: list[str]):
""" """
Entry point of command Entry point of command
@ -308,6 +326,9 @@ class NewService(CommandABC):
if 'service-providing' in args: if 'service-providing' in args:
self._use_service_providing = True self._use_service_providing = True
args.remove('service-providing') args.remove('service-providing')
if 'venv' in args:
self._use_venv = True
args.remove('venv')
console = self._config.get_configuration(ProjectTypeEnum.console.value) console = self._config.get_configuration(ProjectTypeEnum.console.value)
library = self._config.get_configuration(ProjectTypeEnum.library.value) library = self._config.get_configuration(ProjectTypeEnum.library.value)
@ -316,16 +337,22 @@ class NewService(CommandABC):
self._name = console self._name = console
self._schematic = ProjectTypeEnum.console.value self._schematic = ProjectTypeEnum.console.value
self._console(args) self._console(args)
if self._use_venv:
self._create_venv()
elif console is None and library is not None and unittest is None: elif console is None and library is not None and unittest is None:
self._name = library self._name = library
self._schematic = ProjectTypeEnum.library.value self._schematic = ProjectTypeEnum.library.value
self._library(args) self._library(args)
if self._use_venv:
self._create_venv()
elif console is None and library is None and unittest is not None: elif console is None and library is None and unittest is not None:
self._name = unittest self._name = unittest
self._schematic = ProjectTypeEnum.unittest.value self._schematic = ProjectTypeEnum.unittest.value
self._unittest(args) self._unittest(args)
if self._use_venv:
self._create_venv()
else: else:
self._help('Usage: cpl new <schematic> [options]') self._help('Usage: cpl new <schematic> [options]')

View File

@ -4,6 +4,7 @@ import subprocess
import textwrap import textwrap
import time import time
from cpl_cli.configuration.venv_helper_service import VenvHelper
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
@ -71,8 +72,7 @@ class UninstallService(CommandABC):
args.remove('--simulate') args.remove('--simulate')
Console.write_line('Running in simulation mode:') Console.write_line('Running in simulation mode:')
if not self._is_virtual: VenvHelper.init_venv(self._is_virtual, self._env, self._project_settings)
Pip.set_executable(self._project_settings.python_executable)
package = args[0] package = args[0]
is_in_dependencies = False is_in_dependencies = False

View File

@ -3,6 +3,7 @@ import os
import subprocess import subprocess
import textwrap import textwrap
from cpl_cli.configuration.venv_helper_service import VenvHelper
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
@ -168,7 +169,8 @@ class UpdateService(CommandABC):
Console.write_line('Running in simulation mode:') Console.write_line('Running in simulation mode:')
self._is_simulation = True self._is_simulation = True
Pip.set_executable(self._project_settings.python_executable) VenvHelper.init_venv(False, self._env, self._project_settings)
self._check_project_dependencies() self._check_project_dependencies()
self._check_outdated() self._check_outdated()
Pip.reset_executable() Pip.reset_executable()

View File

@ -3,12 +3,12 @@ import sys
import traceback import traceback
from typing import Optional from typing import Optional
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
from cpl_cli.configuration.version_settings import VersionSettings
from cpl_cli.error import Error
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_cli.configuration.version_settings import VersionSettings
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
from cpl_cli.error import Error
class ProjectSettings(ConfigurationModelABC): class ProjectSettings(ConfigurationModelABC):
@ -114,13 +114,10 @@ class ProjectSettings(ConfigurationModelABC):
self._python_version = settings[ProjectSettingsNameEnum.python_version.value] self._python_version = settings[ProjectSettingsNameEnum.python_version.value]
self._python_path = settings[ProjectSettingsNameEnum.python_path.value] self._python_path = settings[ProjectSettingsNameEnum.python_path.value]
if ProjectSettingsNameEnum.python_path.value in settings and \ if ProjectSettingsNameEnum.python_path.value in settings and sys.platform in settings[ProjectSettingsNameEnum.python_path.value]:
sys.platform in settings[ProjectSettingsNameEnum.python_path.value]:
path = settings[ProjectSettingsNameEnum.python_path.value][sys.platform] path = settings[ProjectSettingsNameEnum.python_path.value][sys.platform]
if not os.path.isfile(path) and not os.path.islink(path): if path == '' or path is None:
if path != '' and path is not None:
Error.warn(f'{ProjectSettingsNameEnum.python_path.value} not found') Error.warn(f'{ProjectSettingsNameEnum.python_path.value} not found')
path = sys.executable path = sys.executable
else: else:
path = sys.executable path = sys.executable
@ -134,7 +131,6 @@ class ProjectSettings(ConfigurationModelABC):
except Exception as e: except Exception as e:
Console.set_foreground_color(ForegroundColorEnum.red) Console.set_foreground_color(ForegroundColorEnum.red)
Console.write_line( Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {ProjectSettings.__name__} settings')
f'[ ERROR ] [ {__name__} ]: Reading error in {ProjectSettings.__name__} settings')
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)

View File

@ -0,0 +1,43 @@
import os
import subprocess
import sys
from cpl_cli.configuration import ProjectSettings
from cpl_core.environment import ApplicationEnvironmentABC
from cpl_core.utils import Pip
from cpl_core.console import Console, ForegroundColorEnum
class VenvHelper:
@staticmethod
def init_venv(is_virtual: bool, env: ApplicationEnvironmentABC, project_settings: ProjectSettings, explicit_path=None):
if is_virtual:
return
venv_path = os.path.abspath(os.path.join(env.working_directory, project_settings.python_executable))
if explicit_path is not None:
venv_path = os.path.abspath(explicit_path)
if not os.path.exists(venv_path):
Console.spinner(
f'Creating venv: {venv_path}',
VenvHelper.create_venv,
venv_path,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
)
Pip.set_executable(venv_path)
@staticmethod
def create_venv(path):
subprocess.run(
[sys.executable, '-m', 'venv', os.path.abspath(os.path.join(path, '../../'))],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL
)

View File

@ -19,7 +19,9 @@
"cpl-core>=2022.6.17.dev8" "cpl-core>=2022.6.17.dev8"
], ],
"PythonVersion": ">=3.10", "PythonVersion": ">=3.10",
"PythonPath": {}, "PythonPath": {
"linux": "../../venv"
},
"Classifiers": [] "Classifiers": []
}, },
"BuildSettings": { "BuildSettings": {

View File

@ -5,6 +5,7 @@ import pkg_resources
from cpl_cli.cli import CLI from cpl_cli.cli import CLI
from cpl_cli.startup import Startup from cpl_cli.startup import Startup
from cpl_cli.startup_argument_extension import StartupArgumentExtension from cpl_cli.startup_argument_extension import StartupArgumentExtension
from cpl_cli.startup_workspace_extension import StartupWorkspaceExtension
from cpl_core.application.application_builder import ApplicationBuilder from cpl_core.application.application_builder import ApplicationBuilder
from cpl_core.application.startup_extension_abc import StartupExtensionABC from cpl_core.application.startup_extension_abc import StartupExtensionABC
@ -31,6 +32,7 @@ def get_startup_extensions() -> list[Type[StartupExtensionABC]]:
def main(): def main():
app_builder = ApplicationBuilder(CLI) app_builder = ApplicationBuilder(CLI)
app_builder.use_startup(Startup) app_builder.use_startup(Startup)
app_builder.use_extension(StartupWorkspaceExtension)
app_builder.use_extension(StartupArgumentExtension) app_builder.use_extension(StartupArgumentExtension)
for extension in get_startup_extensions(): for extension in get_startup_extensions():
app_builder.use_extension(extension) app_builder.use_extension(extension)

View File

@ -1,5 +1,23 @@
import os import os
from cpl_cli.command.add_service import AddService
from cpl_cli.command.build_service import BuildService
from cpl_cli.command.custom_script_service import CustomScriptService
from cpl_cli.command.generate_service import GenerateService
from cpl_cli.command.help_service import HelpService
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.remove_service import RemoveService
from cpl_cli.command.run_service import RunService
from cpl_cli.command.start_service import StartService
from cpl_cli.command.uninstall_service import UninstallService
from cpl_cli.command.update_service import UpdateService
from cpl_cli.command.version_service import VersionService
from cpl_cli.validators.project_validator import ProjectValidator
from cpl_cli.validators.workspace_validator import WorkspaceValidator
from cpl_core.console import Console from cpl_core.console import Console
from cpl_cli.error import Error from cpl_cli.error import Error
@ -37,4 +55,22 @@ class Startup(StartupABC):
services.add_transient(PublisherABC, PublisherService) services.add_transient(PublisherABC, PublisherService)
services.add_transient(LiveServerService) services.add_transient(LiveServerService)
services.add_transient(WorkspaceValidator)
services.add_transient(ProjectValidator)
services.add_transient(AddService)
services.add_transient(BuildService)
services.add_transient(CustomScriptService)
services.add_transient(GenerateService)
services.add_transient(HelpService)
services.add_transient(InstallService)
services.add_transient(NewService)
services.add_transient(PublishService)
services.add_transient(RemoveService)
services.add_transient(RunService)
services.add_transient(StartService)
services.add_transient(UninstallService)
services.add_transient(UpdateService)
services.add_transient(VersionService)
return services.build_service_provider() return services.build_service_provider()

View File

@ -1,11 +1,5 @@
import os
from typing import Optional
from cpl_core.console import Console
from cpl_cli.command.add_service import AddService from cpl_cli.command.add_service import AddService
from cpl_cli.command.build_service import BuildService from cpl_cli.command.build_service import BuildService
from cpl_cli.command.custom_script_service import CustomScriptService
from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.generate_service import GenerateService
from cpl_cli.command.help_service import HelpService from cpl_cli.command.help_service import HelpService
from cpl_cli.command.install_service import InstallService from cpl_cli.command.install_service import InstallService
@ -17,7 +11,6 @@ from cpl_cli.command.start_service import StartService
from cpl_cli.command.uninstall_service import UninstallService from cpl_cli.command.uninstall_service import UninstallService
from cpl_cli.command.update_service import UpdateService from cpl_cli.command.update_service import UpdateService
from cpl_cli.command.version_service import VersionService from cpl_cli.command.version_service import VersionService
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
from cpl_cli.validators.project_validator import ProjectValidator from cpl_cli.validators.project_validator import ProjectValidator
from cpl_cli.validators.workspace_validator import WorkspaceValidator from cpl_cli.validators.workspace_validator import WorkspaceValidator
from cpl_core.application.startup_extension_abc import StartupExtensionABC from cpl_core.application.startup_extension_abc import StartupExtensionABC
@ -25,7 +18,6 @@ from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
from cpl_core.utils.string import String
class StartupArgumentExtension(StartupExtensionABC): class StartupArgumentExtension(StartupExtensionABC):
@ -33,40 +25,7 @@ class StartupArgumentExtension(StartupExtensionABC):
def __init__(self): def __init__(self):
pass pass
@staticmethod
def _search_project_json(working_directory: str) -> Optional[str]:
project_name = None
name = os.path.basename(working_directory)
for r, d, f in os.walk(working_directory):
for file in f:
if file.endswith('.json'):
f_name = file.split('.json')[0]
if f_name == name or String.convert_to_camel_case(f_name).lower() == String.convert_to_camel_case(name).lower():
project_name = f_name
break
return project_name
def _read_cpl_environment(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
workspace: Optional[WorkspaceSettings] = config.get_configuration(WorkspaceSettings)
config.add_configuration('PATH_WORKSPACE', env.working_directory)
if workspace is not None:
for script in workspace.scripts:
config.create_console_argument(ArgumentTypeEnum.Executable, '', script, [], CustomScriptService)
return
project = self._search_project_json(env.working_directory)
if project is not None:
project = f'{project}.json'
if project is None:
return
config.add_json_file(project, optional=True, output=False)
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
config.add_json_file('cpl-workspace.json', path=env.working_directory, optional=True, output=False)
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'add', ['a', 'A'], AddService, True, validators=[WorkspaceValidator]) \ config.create_console_argument(ArgumentTypeEnum.Executable, '', 'add', ['a', 'A'], AddService, True, validators=[WorkspaceValidator]) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S']) .add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'build', ['b', 'B'], BuildService, True, validators=[ProjectValidator]) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'build', ['b', 'B'], BuildService, True, validators=[ProjectValidator])
@ -91,7 +50,8 @@ class StartupArgumentExtension(StartupExtensionABC):
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'application-base', ['ab', 'AB']) \ .add_console_argument(ArgumentTypeEnum.Flag, '--', 'application-base', ['ab', 'AB']) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'startup', ['s', 'S']) \ .add_console_argument(ArgumentTypeEnum.Flag, '--', 'startup', ['s', 'S']) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'service-providing', ['sp', 'SP']) \ .add_console_argument(ArgumentTypeEnum.Flag, '--', 'service-providing', ['sp', 'SP']) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'nothing', ['n', 'N']) .add_console_argument(ArgumentTypeEnum.Flag, '--', 'nothing', ['n', 'N']) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'venv', ['v', 'V'])
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'publish', ['p', 'P'], PublishService, True, validators=[ProjectValidator]) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'publish', ['p', 'P'], PublishService, True, validators=[ProjectValidator])
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'remove', ['r', 'R'], RemoveService, True, validators=[WorkspaceValidator]) \ config.create_console_argument(ArgumentTypeEnum.Executable, '', 'remove', ['r', 'R'], RemoveService, True, validators=[WorkspaceValidator]) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S']) .add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
@ -107,23 +67,5 @@ class StartupArgumentExtension(StartupExtensionABC):
config.for_each_argument(lambda a: a.add_console_argument(ArgumentTypeEnum.Flag, '--', 'help', ['h', 'H'])) config.for_each_argument(lambda a: a.add_console_argument(ArgumentTypeEnum.Flag, '--', 'help', ['h', 'H']))
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'help', ['h', 'H'], HelpService) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'help', ['h', 'H'], HelpService)
self._read_cpl_environment(config, env)
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services.add_transient(WorkspaceValidator) pass
services.add_transient(ProjectValidator)
services.add_transient(AddService)
services.add_transient(BuildService)
services.add_transient(CustomScriptService)
services.add_transient(GenerateService)
services.add_transient(HelpService)
services.add_transient(InstallService)
services.add_transient(NewService)
services.add_transient(PublishService)
services.add_transient(RemoveService)
services.add_transient(RunService)
services.add_transient(StartService)
services.add_transient(UninstallService)
services.add_transient(UpdateService)
services.add_transient(VersionService)

View File

@ -0,0 +1,55 @@
import os
from typing import Optional
from cpl_cli.command.custom_script_service import CustomScriptService
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
from cpl_core.application.startup_extension_abc import StartupExtensionABC
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
from cpl_core.utils.string import String
class StartupWorkspaceExtension(StartupExtensionABC):
def __init__(self):
pass
@staticmethod
def _search_project_json(working_directory: str) -> Optional[str]:
project_name = None
name = os.path.basename(working_directory)
for r, d, f in os.walk(working_directory):
for file in f:
if file.endswith('.json'):
f_name = file.split('.json')[0]
if f_name == name or String.convert_to_camel_case(f_name).lower() == String.convert_to_camel_case(name).lower():
project_name = f_name
break
return project_name
def _read_cpl_environment(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
workspace: Optional[WorkspaceSettings] = config.get_configuration(WorkspaceSettings)
config.add_configuration('PATH_WORKSPACE', env.working_directory)
if workspace is not None:
for script in workspace.scripts:
config.create_console_argument(ArgumentTypeEnum.Executable, '', script, [], CustomScriptService)
return
project = self._search_project_json(env.working_directory)
if project is not None:
project = f'{project}.json'
if project is None:
return
config.add_json_file(project, optional=True, output=False)
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
config.add_json_file('cpl-workspace.json', path=env.working_directory, optional=True, output=False)
self._read_cpl_environment(config, env)
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
pass

View File

@ -4,19 +4,22 @@ import sys
from contextlib import suppress from contextlib import suppress
from typing import Optional from typing import Optional
from cpl_core.console import Console
class Pip: class Pip:
r"""Executes pip commands""" r"""Executes pip commands"""
_executable = sys.executable _executable = sys.executable
_env = os.environ _env = os.environ
_is_venv = False
"""Getter""" """Getter"""
@classmethod @classmethod
def get_executable(cls) -> str: def get_executable(cls) -> str:
return cls._executable return cls._executable
"""Setter""" """Setter"""
@classmethod @classmethod
def set_executable(cls, executable: str): def set_executable(cls, executable: str):
r"""Sets the executable r"""Sets the executable
@ -26,10 +29,13 @@ class Pip:
executable: :class:`str` executable: :class:`str`
The python command The python command
""" """
if executable is not None and executable != sys.executable: if executable is None or executable == sys.executable:
return
cls._executable = executable cls._executable = executable
if os.path.islink(cls._executable): if not os.path.islink(cls._executable):
cls._is_venv = True return
path = os.path.dirname(os.path.dirname(cls._executable)) path = os.path.dirname(os.path.dirname(cls._executable))
cls._env = os.environ cls._env = os.environ
if sys.platform == 'win32': if sys.platform == 'win32':
@ -42,9 +48,9 @@ class Pip:
def reset_executable(cls): def reset_executable(cls):
r"""Resets the executable to system standard""" r"""Resets the executable to system standard"""
cls._executable = sys.executable cls._executable = sys.executable
cls._is_venv = False
"""Public utils functions""" """Public utils functions"""
@classmethod @classmethod
def get_package(cls, package: str) -> Optional[str]: def get_package(cls, package: str) -> Optional[str]:
r"""Gets given package py local pip list r"""Gets given package py local pip list
@ -60,8 +66,6 @@ class Pip:
result = None result = None
with suppress(Exception): with suppress(Exception):
args = [cls._executable, "-m", "pip", "show", package] args = [cls._executable, "-m", "pip", "show", package]
if cls._is_venv:
args = ["pip", "show", package]
result = subprocess.check_output( result = subprocess.check_output(
args, args,
@ -92,8 +96,6 @@ class Pip:
Bytes string of the command result Bytes string of the command result
""" """
args = [cls._executable, "-m", "pip", "list", "--outdated"] args = [cls._executable, "-m", "pip", "list", "--outdated"]
if cls._is_venv:
args = ["pip", "list", "--outdated"]
return subprocess.check_output(args, env=cls._env) return subprocess.check_output(args, env=cls._env)
@ -115,8 +117,6 @@ class Pip:
Stderr of subprocess.run Stderr of subprocess.run
""" """
pip_args = [cls._executable, "-m", "pip", "install"] pip_args = [cls._executable, "-m", "pip", "install"]
if cls._is_venv:
pip_args = ["pip", "install"]
for arg in args: for arg in args:
pip_args.append(arg) pip_args.append(arg)
@ -126,6 +126,7 @@ class Pip:
pip_args.append(source) pip_args.append(source)
pip_args.append(package) pip_args.append(package)
print(pip_args)
subprocess.run(pip_args, stdout=stdout, stderr=stderr, env=cls._env) subprocess.run(pip_args, stdout=stdout, stderr=stderr, env=cls._env)
@classmethod @classmethod
@ -142,8 +143,6 @@ class Pip:
Stderr of subprocess.run Stderr of subprocess.run
""" """
args = [cls._executable, "-m", "pip", "uninstall", "--yes", package] args = [cls._executable, "-m", "pip", "uninstall", "--yes", package]
if cls._is_venv:
args = ["pip", "uninstall", "--yes", package]
subprocess.run( subprocess.run(
args, args,

View File

@ -20,7 +20,7 @@
], ],
"PythonVersion": ">=3.10", "PythonVersion": ">=3.10",
"PythonPath": { "PythonPath": {
"linux": "../../../../../../cpl-env/bin/python3.9", "linux": "../../venv/bin/python",
"win32": "" "win32": ""
}, },
"Classifiers": [] "Classifiers": []

View File

@ -12,10 +12,15 @@ class NewTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
os.chdir(os.path.abspath(PLAYGROUND_PATH)) os.chdir(os.path.abspath(PLAYGROUND_PATH))
def _test_project(self, project_type: str, name: str, *args): def _test_project(self, project_type: str, name: str, *args, test_venv=False):
CLICommands.new(project_type, name, *args) CLICommands.new(project_type, name, *args)
workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name)) workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name))
self.assertTrue(os.path.exists(workspace_path)) self.assertTrue(os.path.exists(workspace_path))
if test_venv:
self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv')))
self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv/bin')))
self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv/bin/python')))
self.assertTrue(os.path.islink(os.path.join(workspace_path, 'venv/bin/python')))
project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name, 'src', String.convert_to_snake_case(name))) project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, name, 'src', String.convert_to_snake_case(name)))
self.assertTrue(os.path.exists(project_path)) self.assertTrue(os.path.exists(project_path))
@ -38,11 +43,16 @@ class NewTestCase(unittest.TestCase):
else: else:
self.assertFalse(os.path.isfile(os.path.join(project_path, f'test_case.py'))) self.assertFalse(os.path.isfile(os.path.join(project_path, f'test_case.py')))
def _test_sub_project(self, project_type: str, name: str, workspace_name: str, *args): def _test_sub_project(self, project_type: str, name: str, workspace_name: str, *args, test_venv=False):
os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name))) os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name)))
CLICommands.new(project_type, name, *args) CLICommands.new(project_type, name, *args)
workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name)) workspace_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name))
self.assertTrue(os.path.exists(workspace_path)) self.assertTrue(os.path.exists(workspace_path))
if test_venv:
self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv')))
self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv/bin')))
self.assertTrue(os.path.exists(os.path.join(workspace_path, 'venv/bin/python')))
self.assertTrue(os.path.islink(os.path.join(workspace_path, 'venv/bin/python')))
project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name, 'src', String.convert_to_snake_case(name))) project_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, workspace_name, 'src', String.convert_to_snake_case(name)))
self.assertTrue(os.path.exists(project_path)) self.assertTrue(os.path.exists(project_path))
@ -76,7 +86,7 @@ class NewTestCase(unittest.TestCase):
os.chdir(os.path.abspath(os.path.join(os.getcwd(), '../'))) os.chdir(os.path.abspath(os.path.join(os.getcwd(), '../')))
def test_console(self): def test_console(self):
self._test_project('console', 'test-console', '--ab', '--s') self._test_project('console', 'test-console', '--ab', '--s', '--venv', test_venv=True)
def test_console_without_s(self): def test_console_without_s(self):
self._test_project('console', 'test-console-without-s', '--ab') self._test_project('console', 'test-console-without-s', '--ab')
@ -88,7 +98,7 @@ class NewTestCase(unittest.TestCase):
self._test_project('console', 'test-console-without-anything', '--n') self._test_project('console', 'test-console-without-anything', '--n')
def test_sub_console(self): def test_sub_console(self):
self._test_sub_project('console', 'test-sub-console', 'test-console', '--ab', '--s', '--sp') self._test_sub_project('console', 'test-sub-console', 'test-console', '--ab', '--s', '--sp', '--venv', test_venv=True)
def test_library(self): def test_library(self):
self._test_project('library', 'test-library', '--ab', '--s', '--sp') self._test_project('library', 'test-library', '--ab', '--s', '--sp')