2021-03-14 10:58:59 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import subprocess
|
2021-04-11 16:36:59 +02:00
|
|
|
import textwrap
|
2021-03-14 10:58:59 +01:00
|
|
|
|
2021-08-05 14:21:42 +02:00
|
|
|
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
|
2021-03-14 10:58:59 +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-14 10:58:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UninstallService(CommandABC):
|
|
|
|
|
2021-04-08 21:14:38 +02:00
|
|
|
def __init__(self, config: ConfigurationABC, env: ApplicationEnvironmentABC, build_settings: BuildSettings,
|
2021-03-14 10:58:59 +01:00
|
|
|
project_settings: ProjectSettings):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Service for the CLI command uninstall
|
2021-04-08 21:14:38 +02:00
|
|
|
:param config:
|
2021-03-29 08:56:18 +02:00
|
|
|
:param env:
|
2021-03-14 16:01:15 +01:00
|
|
|
:param build_settings:
|
|
|
|
:param project_settings:
|
|
|
|
"""
|
2021-03-14 10:58:59 +01:00
|
|
|
CommandABC.__init__(self)
|
|
|
|
|
2021-04-08 21:14:38 +02:00
|
|
|
self._config = config
|
2021-03-29 08:56:18 +02:00
|
|
|
self._env = env
|
2021-03-14 10:58:59 +01:00
|
|
|
self._build_settings = build_settings
|
|
|
|
self._project_settings = project_settings
|
|
|
|
|
2021-04-11 16:36:59 +02:00
|
|
|
@property
|
|
|
|
def help_message(self) -> str:
|
|
|
|
return textwrap.dedent("""\
|
2021-04-12 20:02:45 +02:00
|
|
|
Uninstalls given package via pip
|
2021-04-12 20:05:55 +02:00
|
|
|
Usage: cpl uninstall <package>
|
2021-04-12 20:02:45 +02:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
package The package to uninstall
|
2021-04-11 16:36:59 +02:00
|
|
|
""")
|
|
|
|
|
2021-03-14 10:58:59 +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-14 10:58:59 +01:00
|
|
|
if len(args) == 0:
|
|
|
|
Console.error(f'Expected package')
|
|
|
|
Console.error(f'Usage: cpl uninstall <package>')
|
|
|
|
return
|
|
|
|
|
2021-03-17 08:55:23 +01:00
|
|
|
Pip.set_executable(self._project_settings.python_executable)
|
2021-03-14 13:22:10 +01:00
|
|
|
|
2021-03-14 10:58:59 +01:00
|
|
|
package = args[0]
|
|
|
|
is_in_dependencies = False
|
|
|
|
|
|
|
|
pip_package = Pip.get_package(package)
|
|
|
|
|
|
|
|
for dependency in self._project_settings.dependencies:
|
|
|
|
if package in dependency:
|
|
|
|
is_in_dependencies = True
|
|
|
|
package = dependency
|
|
|
|
|
|
|
|
if not is_in_dependencies and pip_package is None:
|
|
|
|
Console.error(f'Package {package} not found')
|
|
|
|
return
|
|
|
|
|
|
|
|
elif not is_in_dependencies and pip_package is not None:
|
|
|
|
package = pip_package
|
|
|
|
|
|
|
|
Console.spinner(
|
|
|
|
f'Uninstalling: {package}',
|
|
|
|
Pip.uninstall, package,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|
|
|
|
|
|
|
|
if package in self._project_settings.dependencies:
|
|
|
|
self._project_settings.dependencies.remove(package)
|
|
|
|
config = {
|
2021-03-16 21:50:16 +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:58:59 +01:00
|
|
|
}
|
2021-04-08 21:14:38 +02:00
|
|
|
with open(os.path.join(self._env.working_directory, f'{self._config.get_configuration("ProjectName")}.json'), 'w') as project_file:
|
2021-03-14 10:58:59 +01:00
|
|
|
project_file.write(json.dumps(config, indent=2))
|
|
|
|
project_file.close()
|
|
|
|
|
|
|
|
Console.write_line(f'Removed {package}')
|
2021-03-14 13:22:10 +01:00
|
|
|
Pip.reset_executable()
|