Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abhängigkeiten mittels pip installieren und aktualisieren #794

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion scons/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def __build_python_wheel(package_dir: str):


def __install_python_wheels(wheels: List[str]):
run_python_program('pip', 'install', '--force-reinstall', '--no-deps', *wheels, print_args=True)
run_python_program('pip',
'install',
'--force-reinstall',
'--no-deps',
'--disable-pip-version-check',
*wheels,
print_args=True)


# pylint: disable=unused-argument
Expand Down
84 changes: 33 additions & 51 deletions scons/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,65 @@

from functools import reduce
from os import path
from typing import List, Optional, Tuple
from typing import List, Optional

from modules import BUILD_MODULE, CPP_MODULE, PYTHON_MODULE
from pkg_resources import DistributionNotFound, VersionConflict, parse_requirements, require


def __run_command(cmd: str, *args, print_args: bool = False):
cmd_formatted = cmd + (reduce(lambda aggr, argument: aggr + ' ' + argument, args, '') if print_args else '')
print('Running external command "' + cmd_formatted + '"...')
def __format_command(cmd: str, *args, format_args: bool) -> str:
return cmd + (reduce(lambda aggr, argument: aggr + ' ' + argument, args, '') if format_args else '')


def __run_command(cmd: str, *args, print_cmd: bool = True, print_args: bool = False, capture_output: bool = False):
if print_cmd:
print('Running external command "' + __format_command(cmd, *args, format_args=print_args) + '"...')

cmd_args = [cmd]

for arg in args:
cmd_args.append(str(arg))

out = subprocess.run(cmd_args, check=False)
out = subprocess.run(cmd_args, check=False, text=capture_output, capture_output=capture_output)
exit_code = out.returncode

if exit_code != 0:
print('External command "' + cmd_formatted + '" terminated with non-zero exit code ' + str(exit_code))
print('External command "' + __format_command(cmd, *args, format_args=print_args)
+ '" terminated with non-zero exit code ' + str(exit_code))
sys.exit(exit_code)

return out

def __is_dependency_missing(dependency: str) -> bool:
try:
require(dependency)
return False
except DistributionNotFound:
return True
except VersionConflict:
return False

def __pip_install(requirement: str):
out = __run_command('python',
'-m',
'pip',
'install',
'--upgrade',
'--prefer-binary',
'--disable-pip-version-check',
requirement,
print_cmd=False,
capture_output=True)
stdout = str(out.stdout).strip()

def __is_dependency_outdated(dependency: str) -> bool:
try:
require(dependency)
return False
except DistributionNotFound:
return False
except VersionConflict:
return True
if not reduce(lambda aggr, line: aggr & line.startswith('Requirement already satisfied'), stdout.split('\n'), True):
print(stdout)


def __find_dependencies(requirements_file: str, *dependencies: str) -> List[str]:
def __find_requirements(requirements_file: str, *dependencies: str) -> List[str]:
with open(requirements_file, mode='r', encoding='utf-8') as file:
dependency_dict = {dependency.key: str(dependency) for dependency in parse_requirements(file.read())}
requirements = {line.split(' ')[0]: line.strip() for line in file.readlines()}

if dependencies:
return [dependency_dict[dependency] for dependency in dependencies if dependency in dependency_dict]

return list(dependency_dict.values())


def __find_missing_and_outdated_dependencies(requirements_file: str, *dependencies: str) -> Tuple[List[str], List[str]]:
dependencies = __find_dependencies(requirements_file, *dependencies)
missing_dependencies = [dependency for dependency in dependencies if __is_dependency_missing(dependency)]
outdated_dependencies = [dependency for dependency in dependencies if __is_dependency_outdated(dependency)]
return missing_dependencies, outdated_dependencies
return [requirements[dependency] for dependency in dependencies if dependency in requirements]


def __pip_install(dependencies: List[str], force_reinstall: bool = False):
args = ['--prefer-binary']

if force_reinstall:
args.append('--force-reinstall')

__run_command('python', '-m', 'pip', 'install', *args, *dependencies, print_args=True)
return list(requirements.values())


def __install_dependencies(requirements_file: str, *dependencies: str):
missing_dependencies, outdated_dependencies = __find_missing_and_outdated_dependencies(
requirements_file, *dependencies)

if missing_dependencies:
__pip_install(missing_dependencies)

if outdated_dependencies:
__pip_install(outdated_dependencies, force_reinstall=True)
for requirement in __find_requirements(requirements_file, *dependencies):
__pip_install(requirement)


def install_build_dependencies(*dependencies: str):
Expand Down