2021-02-10 04:36:40 +00:00
|
|
|
"""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
|
2021-04-16 05:55:30 +00:00
|
|
|
import concurrent.futures
|
|
|
|
import contextlib
|
2021-02-10 04:36:40 +00:00
|
|
|
import typing
|
2021-04-16 05:55:30 +00:00
|
|
|
from typing import Optional
|
2021-02-10 04:36:40 +00:00
|
|
|
from typing import Sequence
|
|
|
|
from typing import Set
|
|
|
|
|
|
|
|
from poetry.core.packages import Package as PoetryPackage
|
|
|
|
from tox.venv import VirtualEnv as ToxVirtualEnv
|
|
|
|
|
2021-04-16 23:57:50 +00:00
|
|
|
from tox_poetry_installer import logger
|
2021-02-16 02:01:44 +00:00
|
|
|
from tox_poetry_installer import utilities
|
2021-02-10 04:36:40 +00:00
|
|
|
|
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
from tox_poetry_installer import _poetry
|
|
|
|
|
|
|
|
|
|
|
|
def install(
|
2021-04-16 05:55:30 +00:00
|
|
|
poetry: "_poetry.Poetry",
|
|
|
|
venv: ToxVirtualEnv,
|
|
|
|
packages: Sequence[PoetryPackage],
|
|
|
|
parallels: Optional[int] = None,
|
2021-02-10 04:36:40 +00:00
|
|
|
):
|
|
|
|
"""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
|
2021-04-16 05:55:30 +00:00
|
|
|
:param parallels: Number of parallel processes to use for installing dependency packages, or
|
|
|
|
``None`` to disable parallelization.
|
2021-02-10 04:36:40 +00:00
|
|
|
"""
|
|
|
|
from tox_poetry_installer import _poetry
|
|
|
|
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(
|
|
|
|
f"Installing {len(packages)} packages to environment at {venv.envconfig.envdir}"
|
2021-02-10 04:36:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
pip = _poetry.PipInstaller(
|
2021-02-16 02:01:44 +00:00
|
|
|
env=utilities.convert_virtualenv(venv),
|
2021-02-10 04:36:40 +00:00
|
|
|
io=_poetry.NullIO(),
|
|
|
|
pool=poetry.pool,
|
|
|
|
)
|
|
|
|
|
|
|
|
installed: Set[PoetryPackage] = set()
|
|
|
|
|
2021-04-16 05:55:30 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def _optional_parallelize():
|
|
|
|
"""A bit of cheat, really
|
|
|
|
|
|
|
|
A context manager that exposes a common interface for the caller that optionally
|
|
|
|
enables/disables the usage of the parallel thread pooler depending on the value of
|
|
|
|
the ``parallels`` parameter.
|
|
|
|
"""
|
|
|
|
if parallels:
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(
|
|
|
|
max_workers=parallels
|
|
|
|
) as executor:
|
|
|
|
yield executor.submit
|
2021-02-10 04:36:40 +00:00
|
|
|
else:
|
2021-04-16 05:55:30 +00:00
|
|
|
yield lambda func, arg: func(arg)
|
|
|
|
|
|
|
|
with _optional_parallelize() as executor:
|
|
|
|
for dependency in packages:
|
|
|
|
if dependency not in installed:
|
|
|
|
installed.add(dependency)
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.debug(f"Installing {dependency}")
|
2021-04-16 05:55:30 +00:00
|
|
|
executor(pip.install, dependency)
|
|
|
|
else:
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.debug(f"Skipping {dependency}, already installed")
|
|
|
|
logger.debug("Waiting for installs to finish...")
|