Skip to content

container

dev_tool.services.python.package.container

log = logging.getLogger(__name__) module-attribute

ContainerPackageManager

Bases: BaseService

A class for managing Python packages in a containerized environment.

This class provides methods for installing, uninstalling, and listing packages within Docker containers using uv pip.

install_dependencies

A method that installs dependencies in the container.

Parameters:

Source code in dev_tool/services/python/package/container.py
def install_dependencies(self, sources: list[DependencySource]) -> None:
    """
    A method that installs dependencies in the container.

    :param sources: The dependency sources to install from.
    """

    strategy = ExecutionStrategyProvider.get()

    for source in sources:
        if source.source == SourceType.PYPROJECT:
            extra = source.extra

            target = f'-e .[{extra}]' if extra else '-e .'

            message = f'Installing from pyproject.toml ({source.name})...'
            self.notification.normal_text(message)

            strategy.run_pip_command(['install', '--system', target], tty=False)

        elif source.source == SourceType.REQUIREMENTS:
            if source.path is None:
                continue

            message = f'Installing from {source.path.name}...'
            self.notification.normal_text(message)

            container_path = f'/app/{source.path.name}'
            strategy.run_pip_command(['install', '--system', '-r', container_path], tty=False)

install_package

A method that installs a package in the container.

Parameters:

  • package (str) –

    The name of the package to install.

Returns:

  • bool

    True if successful, False otherwise.

Source code in dev_tool/services/python/package/container.py
def install_package(self, package: str) -> bool:
    """
    A method that installs a package in the container.

    :param package: The name of the package to install.
    :return: True if successful, False otherwise.
    """

    result = ExecutionStrategyProvider.get().run_pip_command(
        ['install', '--system', package],
        tty=False
    )

    return result.returncode == 0

list_packages

A method that lists installed packages in the container.

Returns:

  • list[str]

    A list of installed package names.

Source code in dev_tool/services/python/package/container.py
def list_packages(self) -> list[str]:
    """
    A method that lists installed packages in the container.

    :return: A list of installed package names.
    """

    result = ExecutionStrategyProvider.get().run_pip_command(
        ['list', '--format=freeze'],
        tty=False
    )

    if result.returncode != 0:
        return []

    output = result.stdout.decode() if result.stdout else ''

    return [
        line.split('==')[0].lower()
        for line in output.strip().split('\n')
        if '==' in line
    ]

uninstall_package

A method that uninstalls a package from the container.

Parameters:

  • package (str) –

    The name of the package to uninstall.

Returns:

  • bool

    True if successful, False otherwise.

Source code in dev_tool/services/python/package/container.py
def uninstall_package(self, package: str) -> bool:
    """
    A method that uninstalls a package from the container.

    :param package: The name of the package to uninstall.
    :return: True if successful, False otherwise.
    """

    if self._is_editable_from_mount(package):
        message = (
            f'The "{package}" package is installed in editable mode from a mounted path. '
            f'Remove it from PYTHONPATH_APPEND in development.env to stop using it.'
        )

        self.notification.warning_text(message)

        log.debug(message)

        normalized = package.replace('-', '_')

        script = (
            f'import shutil, glob; '
            f'[shutil.rmtree(p) for p in glob.glob("/usr/local/lib/python3.11/site-packages/{normalized}*.dist-info")]'
        )

        result = ExecutionStrategyProvider.get().run_python_command(
            ['-c', script],
            tty=False
        )

        return result.returncode == 0

    result = ExecutionStrategyProvider.get().run_pip_command(
        ['uninstall', '--system', package],
        tty=False
    )

    return result.returncode == 0