Improved install command
This commit is contained in:
parent
6094b11068
commit
e7796e0371
@ -1,12 +1,19 @@
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
from contextlib import suppress
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
class Pip:
|
class Pip:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_package(package: str) -> str:
|
def get_package(package: str) -> Optional[str]:
|
||||||
result = subprocess.check_output([sys.executable, "-m", "pip", "show", package])
|
result = None
|
||||||
|
with suppress(Exception):
|
||||||
|
result = subprocess.check_output([sys.executable, "-m", "pip", "show", package], stderr=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
return None
|
||||||
|
|
||||||
new_package: list[str] = str(result, 'utf-8').lower().split('\n')
|
new_package: list[str] = str(result, 'utf-8').lower().split('\n')
|
||||||
new_version = ''
|
new_version = ''
|
||||||
|
@ -25,7 +25,26 @@ class InstallService(CommandABC):
|
|||||||
self._config = configuration
|
self._config = configuration
|
||||||
|
|
||||||
def _install_project(self):
|
def _install_project(self):
|
||||||
pass
|
project: ProjectSettings = self._config.get_configuration(ProjectSettings)
|
||||||
|
build: BuildSettings = self._config.get_configuration(BuildSettings)
|
||||||
|
|
||||||
|
if project is None or build is None:
|
||||||
|
Error.error('The command requires to be run in an CPL project, but a project could not be found.')
|
||||||
|
return
|
||||||
|
|
||||||
|
if project.dependencies is None:
|
||||||
|
Error.error('Found invalid dependencies in cpl.json.')
|
||||||
|
return
|
||||||
|
|
||||||
|
for dependency in project.dependencies:
|
||||||
|
Console.spinner(
|
||||||
|
f'Installing: {dependency}',
|
||||||
|
Pip.install, dependency,
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL,
|
||||||
|
text_foreground_color=ForegroundColorEnum.green,
|
||||||
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_project_settings_dict(project: ProjectSettings) -> dict:
|
def _get_project_settings_dict(project: ProjectSettings) -> dict:
|
||||||
@ -63,6 +82,28 @@ class InstallService(CommandABC):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def _install_package(self, package: str):
|
def _install_package(self, package: str):
|
||||||
|
is_already_in_project = False
|
||||||
|
project: ProjectSettings = self._config.get_configuration(ProjectSettings)
|
||||||
|
build: BuildSettings = self._config.get_configuration(BuildSettings)
|
||||||
|
|
||||||
|
if project is None or build is None:
|
||||||
|
Error.error('The command requires to be run in an CPL project, but a project could not be found.')
|
||||||
|
return
|
||||||
|
|
||||||
|
if project.dependencies is None:
|
||||||
|
Error.error('Found invalid dependencies in cpl.json.')
|
||||||
|
return
|
||||||
|
|
||||||
|
old_package = Pip.get_package(package)
|
||||||
|
|
||||||
|
for dependency in project.dependencies:
|
||||||
|
if package in dependency:
|
||||||
|
is_already_in_project = True
|
||||||
|
|
||||||
|
if old_package is not None and old_package in project.dependencies or is_already_in_project:
|
||||||
|
Error.warn(f'Package {old_package} is already installed.')
|
||||||
|
return
|
||||||
|
|
||||||
Console.spinner(
|
Console.spinner(
|
||||||
f'Installing: {package}',
|
f'Installing: {package}',
|
||||||
Pip.install, package,
|
Pip.install, package,
|
||||||
@ -72,12 +113,10 @@ class InstallService(CommandABC):
|
|||||||
spinner_foreground_color=ForegroundColorEnum.cyan
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not is_already_in_project:
|
||||||
new_package = Pip.get_package(package)
|
new_package = Pip.get_package(package)
|
||||||
project: ProjectSettings = self._config.get_configuration(ProjectSettings)
|
if new_package is None:
|
||||||
build: BuildSettings = self._config.get_configuration(BuildSettings)
|
new_package = package
|
||||||
if project is None or build is None:
|
|
||||||
Error.error('The command requires to be run in an CPL project, but a project could not be found.')
|
|
||||||
return
|
|
||||||
|
|
||||||
project.dependencies.append(new_package)
|
project.dependencies.append(new_package)
|
||||||
|
|
||||||
@ -90,7 +129,6 @@ class InstallService(CommandABC):
|
|||||||
project_file.close()
|
project_file.close()
|
||||||
|
|
||||||
def run(self, args: list[str]):
|
def run(self, args: list[str]):
|
||||||
|
|
||||||
if len(args) == 0:
|
if len(args) == 0:
|
||||||
self._install_project()
|
self._install_project()
|
||||||
else:
|
else:
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
from cpl.console import ForegroundColorEnum
|
||||||
from cpl.console.console import Console
|
from cpl.console.console import Console
|
||||||
|
|
||||||
|
|
||||||
@ -7,3 +8,10 @@ class Error:
|
|||||||
def error(message: str):
|
def error(message: str):
|
||||||
Console.error(message)
|
Console.error(message)
|
||||||
Console.error('Run \'cpl help\'\n')
|
Console.error('Run \'cpl help\'\n')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def warn(message: str):
|
||||||
|
Console.set_foreground_color(ForegroundColorEnum.yellow)
|
||||||
|
Console.write_line(message)
|
||||||
|
Console.set_foreground_color(ForegroundColorEnum.default)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user