Move install function to dedicated submodule

Fix duplicate package installs caused by using list for ordering
This commit is contained in:
Ethan Paul 2021-02-09 23:36:40 -05:00
parent ea8bc3887e
commit b8ea98b3ad
No known key found for this signature in database
GPG Key ID: C5F5542B54A4D9C6
3 changed files with 55 additions and 28 deletions

View File

@ -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

View File

@ -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"
)

View File

@ -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]: