2021-03-13 21:39:59 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from cpl.application import ApplicationRuntimeABC
|
|
|
|
from cpl.console import ForegroundColorEnum
|
|
|
|
from cpl.console.console import Console
|
2021-03-13 22:53:28 +01:00
|
|
|
from cpl.utils.pip import Pip
|
2021-03-13 21:39:59 +01:00
|
|
|
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):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Service for the CLI command update
|
|
|
|
:param runtime:
|
|
|
|
:param project_settings:
|
|
|
|
"""
|
2021-03-13 21:39:59 +01:00
|
|
|
CommandABC.__init__(self)
|
|
|
|
|
|
|
|
self._runtime = runtime
|
|
|
|
self._project_settings = project_settings
|
|
|
|
|
2021-03-14 11:33:29 +01:00
|
|
|
def _update_project_dependencies(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Updates project dependencies
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-14 11:33:29 +01:00
|
|
|
for package in self._project_settings.dependencies:
|
|
|
|
name = package
|
|
|
|
if '==' in package:
|
|
|
|
name = package.split('==')[0]
|
|
|
|
|
2021-03-14 16:01:15 +01:00
|
|
|
Pip.install(
|
|
|
|
name,
|
|
|
|
'--upgrade',
|
|
|
|
'--upgrade-strategy',
|
|
|
|
'eager',
|
|
|
|
source='https://pip.sh-edraft.de' if 'sh_cpl' in name else None,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL
|
|
|
|
)
|
2021-03-14 11:33:29 +01:00
|
|
|
|
2021-03-14 11:54:03 +01:00
|
|
|
new_package = Pip.get_package(name)
|
|
|
|
if new_package is None:
|
2021-03-14 13:22:10 +01:00
|
|
|
Console.error(f'Update for package {package} failed')
|
2021-03-14 11:54:03 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
self._project_json_update_dependency(package, new_package)
|
2021-03-14 11:33:29 +01:00
|
|
|
|
2021-03-14 10:33:57 +01:00
|
|
|
def _check_project_dependencies(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Checks project dependencies for updates
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-14 10:33:57 +01:00
|
|
|
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.')
|
|
|
|
|
2021-03-14 13:22:10 +01:00
|
|
|
@staticmethod
|
|
|
|
def _check_outdated():
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Checks for outdated packages in project
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 21:39:59 +01:00
|
|
|
table_str: bytes = Console.spinner(
|
2021-03-14 13:22:10 +01:00
|
|
|
'Analyzing for available package updates', Pip.get_outdated,
|
2021-03-13 21:39:59 +01:00
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|
|
|
|
|
|
|
|
table = str(table_str, 'utf-8').split('\n')
|
2021-03-14 11:54:03 +01:00
|
|
|
if len(table) > 1 and table[0] != '':
|
|
|
|
Console.write_line('\tAvailable updates for packages:')
|
|
|
|
for row in table:
|
|
|
|
Console.write_line(f'\t{row}')
|
|
|
|
|
|
|
|
Console.set_foreground_color(ForegroundColorEnum.yellow)
|
2021-03-14 13:22:10 +01:00
|
|
|
Console.write_line(f'\tUpdate with {Pip.get_executable()} -m pip install --upgrade <package>')
|
2021-03-14 11:54:03 +01:00
|
|
|
Console.set_foreground_color(ForegroundColorEnum.default)
|
2021-03-13 21:39:59 +01:00
|
|
|
|
|
|
|
def _project_json_update_dependency(self, old_package: str, new_package: str):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Writes new package version to cpl.json
|
|
|
|
:param old_package:
|
|
|
|
:param new_package:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-13 21:39:59 +01:00
|
|
|
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 run(self, args: list[str]):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Entry point of command
|
|
|
|
:param args:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-14 13:22:10 +01:00
|
|
|
Pip.set_executable(self._project_settings.python_path)
|
2021-03-13 21:39:59 +01:00
|
|
|
self._check_project_dependencies()
|
|
|
|
self._check_outdated()
|
2021-03-14 13:22:10 +01:00
|
|
|
Pip.reset_executable()
|
2021-03-13 21:39:59 +01:00
|
|
|
|
|
|
|
Console.write('\n')
|