Skip to content

Commit

Permalink
Allow to install multiple requirements at once.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-rapp committed Dec 17, 2024
1 parent 9561113 commit 9c45ee0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
3 changes: 1 addition & 2 deletions build_system/targets/dependencies/python/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def install_all_packages(self):
"""
Installs all dependencies in the requirements file.
"""
for requirement in self.requirements.requirements:
Pip.install_requirement(requirement, dry_run=True)
Pip.install_requirements(*self.requirements.requirements, dry_run=True)

def list_outdated_dependencies(self) -> Set[Dependency]:
"""
Expand Down
62 changes: 32 additions & 30 deletions build_system/util/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,49 +324,53 @@ class InstallCommand(Command):
Allows to install requirements via the command `pip install`.
"""

def __init__(self, requirement: Requirement, dry_run: bool = False):
def __init__(self, *requirements: Requirement, dry_run: bool = False):
"""
:param requirement: The requirement be installed
:param requirement: The requirements to be installed
:param dry_run: True, if the --dry-run flag should be set, False otherwise
"""
super().__init__('install', str(requirement), '--upgrade', '--upgrade-strategy', 'eager', '--prefer-binary')
super().__init__('install', *[str(requirement) for requirement in requirements], '--upgrade',
'--upgrade-strategy', 'eager', '--prefer-binary')
self.add_conditional_arguments(dry_run, '--dry-run')

@staticmethod
def __would_install_requirement(requirement: Requirement, stdout: str) -> bool:
def __would_install_requirements(stdout: str, *requirements: Requirement) -> bool:
prefix = 'Would install'

for line in stdout.split('\n'):
if line.strip().startswith(prefix):
package = Package(line[len(prefix):].strip())

if package.normalized_name.find(requirement.package.normalized_name) >= 0:
return True
for requirement in requirements:
if package.normalized_name.find(requirement.package.normalized_name) >= 0:
return True

return False

@staticmethod
def install_requirement(requirement: Requirement, dry_run: bool = False):
"""
Installs a requirement.
:param requirement: The requirement to be installed
"""
try:
stdout = Pip.InstallCommand(requirement, dry_run=dry_run) \
.print_command(False) \
.exit_on_error(not dry_run) \
.capture_output()

if Pip.__would_install_requirement(requirement, stdout):
if dry_run:
Pip.InstallCommand(requirement) \
.print_arguments(True) \
.run()
else:
Log.info(stdout)
except RuntimeError:
Pip.install_requirement(requirement)
def install_requirements(*requirements: Requirement, dry_run: bool = False):
"""
Installs one or several requirements.
:param requirements: The requirements to be installed
:param dry_run: True, if pip's "--dry-run" option should be set, False otherwise
"""
if requirements:
try:
stdout = Pip.InstallCommand(*requirements, dry_run=dry_run) \
.print_command(False) \
.exit_on_error(not dry_run) \
.capture_output()

if Pip.__would_install_requirements(stdout, *requirements):
if dry_run:
Pip.InstallCommand(*requirements) \
.print_arguments(True) \
.run()
else:
Log.info(stdout)
except RuntimeError:
Pip.install_requirements(*requirements)

def __init__(self, *requirements_files: str):
"""
Expand Down Expand Up @@ -395,6 +399,4 @@ def install_packages(self, *package_names: str, accept_missing: bool = False):
"""
packages = [Package(package_name) for package_name in package_names]
requirements = self.requirements.lookup_requirements(*packages, accept_missing=accept_missing)

for requirement in requirements:
self.install_requirement(requirement, dry_run=True)
self.install_requirements(*requirements, dry_run=True)

0 comments on commit 9c45ee0

Please sign in to comment.