diff --git a/tox_poetry_installer/hooks.py b/tox_poetry_installer/hooks.py index b7748da..a42b7ee 100644 --- a/tox_poetry_installer/hooks.py +++ b/tox_poetry_installer/hooks.py @@ -16,6 +16,7 @@ from tox.venv import VirtualEnv as ToxVirtualEnv from tox_poetry_installer import __about__ from tox_poetry_installer import constants from tox_poetry_installer import exceptions +from tox_poetry_installer import installer from tox_poetry_installer import utilities from tox_poetry_installer.datatypes import PackageMap @@ -139,6 +140,6 @@ def tox_testenv_install_deps(venv: ToxVirtualEnv, action: ToxAction) -> Optional __about__.__title__, f"Installing {len(dependencies)} dependencies from Poetry lock file", ) - utilities.install_to_venv(poetry, venv, dependencies) + installer.install(poetry, venv, dependencies) return venv.envconfig.require_locked_deps or None diff --git a/tox_poetry_installer/installer.py b/tox_poetry_installer/installer.py new file mode 100644 index 0000000..3f13f93 --- /dev/null +++ b/tox_poetry_installer/installer.py @@ -0,0 +1,53 @@ +"""Funcationality for performing virtualenv installation""" +# Silence this one globally to support the internal function imports for the proxied poetry module. +# See the docstring in 'tox_poetry_installer._poetry' for more context. +# pylint: disable=import-outside-toplevel +import typing +from pathlib import Path +from typing import Sequence +from typing import Set + +import tox +from poetry.core.packages import Package as PoetryPackage +from tox.venv import VirtualEnv as ToxVirtualEnv + +from tox_poetry_installer import constants + +if typing.TYPE_CHECKING: + from tox_poetry_installer import _poetry + + +def install( + poetry: "_poetry.Poetry", venv: ToxVirtualEnv, packages: Sequence[PoetryPackage] +): + """Install a bunch of packages to a virtualenv + + :param poetry: Poetry object the packages were sourced from + :param venv: Tox virtual environment to install the packages to + :param packages: List of packages to install to the virtual environment + """ + from tox_poetry_installer import _poetry + + tox.reporter.verbosity1( + f"{constants.REPORTER_PREFIX} Installing {len(packages)} packages to environment at {venv.envconfig.envdir}" + ) + + pip = _poetry.PipInstaller( + env=_poetry.VirtualEnv(path=Path(venv.envconfig.envdir)), + io=_poetry.NullIO(), + pool=poetry.pool, + ) + + installed: Set[PoetryPackage] = set() + + for dependency in packages: + if dependency not in installed: + tox.reporter.verbosity1( + f"{constants.REPORTER_PREFIX} Installing {dependency}" + ) + pip.install(dependency) + installed.add(dependency) + else: + tox.reporter.verbosity1( + f"{constants.REPORTER_PREFIX} Already installed {dependency}, skipping" + ) diff --git a/tox_poetry_installer/utilities.py b/tox_poetry_installer/utilities.py index 2dad49f..c477945 100644 --- a/tox_poetry_installer/utilities.py +++ b/tox_poetry_installer/utilities.py @@ -4,7 +4,6 @@ # pylint: disable=import-outside-toplevel import sys import typing -from pathlib import Path from typing import List from typing import Sequence from typing import Set @@ -22,32 +21,6 @@ if typing.TYPE_CHECKING: from tox_poetry_installer import _poetry -def install_to_venv( - poetry: "_poetry.Poetry", venv: ToxVirtualEnv, packages: Sequence[PoetryPackage] -): - """Install a bunch of packages to a virtualenv - - :param poetry: Poetry object the packages were sourced from - :param venv: Tox virtual environment to install the packages to - :param packages: List of packages to install to the virtual environment - """ - from tox_poetry_installer import _poetry - - tox.reporter.verbosity1( - f"{constants.REPORTER_PREFIX} Installing {len(packages)} packages to environment at {venv.envconfig.envdir}" - ) - - installer = _poetry.PipInstaller( - env=_poetry.VirtualEnv(path=Path(venv.envconfig.envdir)), - io=_poetry.NullIO(), - pool=poetry.pool, - ) - - for dependency in packages: - tox.reporter.verbosity1(f"{constants.REPORTER_PREFIX} Installing {dependency}") - installer.install(dependency) - - def identify_transients( packages: PackageMap, dependency_name: str, allow_missing: Sequence[str] = () ) -> List[PoetryPackage]: