2021-03-13 22:53:28 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2021-03-14 14:47:53 +01:00
|
|
|
from packaging import version
|
|
|
|
|
2021-03-16 19:04:30 +01:00
|
|
|
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
|
2021-03-13 22:53:28 +01:00
|
|
|
from cpl.console.console import Console
|
|
|
|
from cpl.console.foreground_color_enum import ForegroundColorEnum
|
|
|
|
from cpl.utils.pip import Pip
|
2021-03-16 18:28:31 +01:00
|
|
|
from cpl_cli.cli_settings import CLISettings
|
2021-03-13 22:53:28 +01:00
|
|
|
from cpl_cli.command_abc import CommandABC
|
|
|
|
from cpl_cli.configuration.build_settings import BuildSettings
|
|
|
|
from cpl_cli.configuration.project_settings import ProjectSettings
|
2021-03-16 21:50:16 +01:00
|
|
|
from cpl_cli.configuration.settings_helper import SettingsHelper
|
2021-03-13 22:53:28 +01:00
|
|
|
from cpl_cli.error import Error
|
|
|
|
|
|
|
|
|
|
|
|
class InstallService(CommandABC):
|
|
|
|
|
2021-03-16 22:39:18 +01:00
|
|
|
def __init__(self, runtime: ApplicationRuntimeABC, build_settings: BuildSettings, project_settings: ProjectSettings,
|
|
|
|
cli_settings: CLISettings):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Service for the CLI command install
|
|
|
|
:param runtime:
|
2021-03-16 22:39:18 +01:00
|
|
|
:param build_settings:
|
|
|
|
:param project_settings:
|
|
|
|
:param cli_settings:
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
2021-03-13 22:53:28 +01:00
|
|
|
CommandABC.__init__(self)
|
|
|
|
|
|
|
|
self._runtime = runtime
|
2021-03-16 22:39:18 +01:00
|
|
|
self._build_settings = build_settings
|
|
|
|
self._project_settings = project_settings
|
2021-03-16 18:28:31 +01:00
|
|
|
self._cli_settings = cli_settings
|
2021-03-13 22:53:28 +01:00
|
|
|
|
|
|
|
def _install_project(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Installs dependencies of CPl project
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-14 10:31:45 +01:00
|
|
|
|
2021-03-16 22:39:18 +01:00
|
|
|
if self._project_settings is None or self._build_settings is None:
|
2021-03-14 10:31:45 +01:00
|
|
|
Error.error('The command requires to be run in an CPL project, but a project could not be found.')
|
|
|
|
return
|
|
|
|
|
2021-03-16 22:39:18 +01:00
|
|
|
if self._project_settings.dependencies is None:
|
2021-03-14 10:31:45 +01:00
|
|
|
Error.error('Found invalid dependencies in cpl.json.')
|
|
|
|
return
|
|
|
|
|
2021-03-16 22:39:18 +01:00
|
|
|
Pip.set_executable(self._project_settings.python_path)
|
|
|
|
for dependency in self._project_settings.dependencies:
|
2021-03-14 10:31:45 +01:00
|
|
|
Console.spinner(
|
|
|
|
f'Installing: {dependency}',
|
|
|
|
Pip.install, dependency,
|
2021-03-16 18:28:31 +01:00
|
|
|
source=self._cli_settings.pip_path if 'sh_cpl' in dependency else None,
|
2021-03-14 10:31:45 +01:00
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|
2021-03-13 22:53:28 +01:00
|
|
|
|
2021-03-14 14:59:17 +01:00
|
|
|
Pip.reset_executable()
|
|
|
|
|
2021-03-13 22:53:28 +01:00
|
|
|
def _install_package(self, package: str):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Installs given package
|
|
|
|
:param package:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-14 10:31:45 +01:00
|
|
|
is_already_in_project = False
|
2021-03-16 22:39:18 +01:00
|
|
|
Pip.set_executable(self._project_settings.python_path)
|
2021-03-14 10:31:45 +01:00
|
|
|
|
2021-03-16 22:39:18 +01:00
|
|
|
if self._project_settings is None or self._build_settings is None:
|
2021-03-14 10:31:45 +01:00
|
|
|
Error.error('The command requires to be run in an CPL project, but a project could not be found.')
|
|
|
|
return
|
|
|
|
|
2021-03-16 22:39:18 +01:00
|
|
|
if self._project_settings.dependencies is None:
|
2021-03-14 10:31:45 +01:00
|
|
|
Error.error('Found invalid dependencies in cpl.json.')
|
|
|
|
return
|
|
|
|
|
2021-03-14 14:47:53 +01:00
|
|
|
package_version = ''
|
2021-03-16 22:55:00 +01:00
|
|
|
name = package
|
2021-03-14 14:47:53 +01:00
|
|
|
if '==' in package:
|
|
|
|
name = package.split('==')[0]
|
|
|
|
package_version = package.split('==')[1]
|
2021-03-14 10:31:45 +01:00
|
|
|
|
2021-03-14 14:47:53 +01:00
|
|
|
to_remove_list = []
|
2021-03-16 22:39:18 +01:00
|
|
|
for dependency in self._project_settings.dependencies:
|
2021-03-14 14:47:53 +01:00
|
|
|
dependency_version = ''
|
|
|
|
|
|
|
|
if '==' in dependency:
|
|
|
|
dependency_version = dependency.split('==')[1]
|
|
|
|
|
2021-03-17 08:12:56 +01:00
|
|
|
if name in dependency:
|
|
|
|
if package_version != '' and version.parse(package_version) != version.parse(dependency_version):
|
2021-03-16 22:58:00 +01:00
|
|
|
to_remove_list.append(dependency)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
is_already_in_project = True
|
2021-03-14 10:31:45 +01:00
|
|
|
|
2021-03-14 14:47:53 +01:00
|
|
|
for to_remove in to_remove_list:
|
2021-03-16 22:39:18 +01:00
|
|
|
self._project_settings.dependencies.remove(to_remove)
|
2021-03-14 14:47:53 +01:00
|
|
|
|
|
|
|
local_package = Pip.get_package(package)
|
2021-03-16 22:39:18 +01:00
|
|
|
if local_package is not None and local_package in self._project_settings.dependencies:
|
2021-03-14 14:47:53 +01:00
|
|
|
Error.warn(f'Package {local_package} is already installed.')
|
|
|
|
return
|
|
|
|
|
|
|
|
elif is_already_in_project:
|
|
|
|
Error.warn(f'Package {package} is already installed.')
|
2021-03-14 10:31:45 +01:00
|
|
|
return
|
|
|
|
|
2021-03-13 22:53:28 +01:00
|
|
|
Console.spinner(
|
|
|
|
f'Installing: {package}',
|
|
|
|
Pip.install, package,
|
2021-03-16 18:28:31 +01:00
|
|
|
source=self._cli_settings.pip_path if 'sh_cpl' in package else None,
|
2021-03-13 22:53:28 +01:00
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|
2021-03-14 14:47:53 +01:00
|
|
|
new_package = Pip.get_package(name)
|
2021-03-17 08:37:50 +01:00
|
|
|
if new_package is None \
|
|
|
|
or '==' in package and \
|
|
|
|
version.parse(package.split('==')[1]) != version.parse(new_package.split('==')[1]):
|
2021-03-14 14:47:53 +01:00
|
|
|
Console.error(f'Installation of package {package} failed')
|
|
|
|
return
|
2021-03-13 22:53:28 +01:00
|
|
|
|
2021-03-14 10:31:45 +01:00
|
|
|
if not is_already_in_project:
|
2021-03-17 07:59:12 +01:00
|
|
|
new_name = package
|
|
|
|
if '==' in new_package:
|
|
|
|
new_name = new_package
|
|
|
|
elif '==' in name:
|
|
|
|
new_name = name
|
2021-03-13 22:53:28 +01:00
|
|
|
|
2021-03-17 07:59:12 +01:00
|
|
|
if '/' in new_name:
|
|
|
|
new_name = new_name.split('/')[0]
|
2021-03-16 22:46:04 +01:00
|
|
|
|
2021-03-17 07:59:12 +01:00
|
|
|
if '\r' in new_name:
|
|
|
|
new_name = new_name.replace('\r', '')
|
2021-03-16 22:46:04 +01:00
|
|
|
|
2021-03-17 07:59:12 +01:00
|
|
|
self._project_settings.dependencies.append(new_name)
|
2021-03-13 22:53:28 +01:00
|
|
|
|
2021-03-14 10:31:45 +01:00
|
|
|
config = {
|
2021-03-16 22:39:18 +01:00
|
|
|
ProjectSettings.__name__: SettingsHelper.get_project_settings_dict(self._project_settings),
|
|
|
|
BuildSettings.__name__: SettingsHelper.get_build_settings_dict(self._build_settings)
|
2021-03-14 10:31:45 +01:00
|
|
|
}
|
2021-03-17 07:59:12 +01:00
|
|
|
|
2021-03-17 08:12:56 +01:00
|
|
|
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()
|
2021-03-13 22:53:28 +01:00
|
|
|
|
2021-03-14 13:22:10 +01:00
|
|
|
Pip.reset_executable()
|
|
|
|
|
2021-03-13 22:53:28 +01:00
|
|
|
def run(self, args: list[str]):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Entry point of command
|
|
|
|
:param args:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 22:53:28 +01:00
|
|
|
if len(args) == 0:
|
|
|
|
self._install_project()
|
|
|
|
else:
|
|
|
|
self._install_package(args[0])
|
|
|
|
|
|
|
|
Console.write('\n')
|