2020-11-12 02:26:59 +00:00
|
|
|
"""Main hook definition module
|
|
|
|
|
|
|
|
All implementations of tox hooks are defined here, as well as any single-use helper functions
|
|
|
|
specifically related to implementing the hooks (to keep the size/readability of the hook functions
|
|
|
|
themselves manageable).
|
|
|
|
"""
|
2022-09-15 11:17:45 +00:00
|
|
|
from itertools import chain
|
2023-01-10 02:34:35 +00:00
|
|
|
from typing import List
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2023-05-19 22:03:44 +00:00
|
|
|
from tox.config.cli.parser import ToxParser
|
2023-01-10 02:34:35 +00:00
|
|
|
from tox.config.sets import EnvConfigSet
|
|
|
|
from tox.plugin import impl
|
|
|
|
from tox.tox_env.api import ToxEnv as ToxVirtualEnv
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2021-05-05 20:18:41 +00:00
|
|
|
from tox_poetry_installer import constants
|
2020-11-12 02:26:59 +00:00
|
|
|
from tox_poetry_installer import exceptions
|
2021-02-10 04:36:40 +00:00
|
|
|
from tox_poetry_installer import installer
|
2021-04-16 23:57:50 +00:00
|
|
|
from tox_poetry_installer import logger
|
2020-11-12 02:26:59 +00:00
|
|
|
from tox_poetry_installer import utilities
|
|
|
|
|
|
|
|
|
2023-05-19 22:03:44 +00:00
|
|
|
@impl
|
|
|
|
def tox_add_option(parser: ToxParser):
|
|
|
|
"""Add additional command line arguments to tox to configure plugin behavior"""
|
|
|
|
parser.add_argument(
|
|
|
|
"--require-poetry",
|
|
|
|
action="store_true",
|
|
|
|
dest="require_poetry",
|
|
|
|
help="(deprecated) Trigger a failure if Poetry is not available to Tox",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--parallel-install-threads",
|
|
|
|
type=int,
|
|
|
|
dest="parallel_install_threads",
|
|
|
|
default=constants.DEFAULT_INSTALL_THREADS,
|
|
|
|
help="Number of locked dependencies to install simultaneously; set to 0 to disable parallel installation",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
@impl
|
|
|
|
def tox_add_env_config(env_conf: EnvConfigSet):
|
|
|
|
"""Add required env configuration options to the tox INI file"""
|
|
|
|
env_conf.add_config(
|
|
|
|
"poetry_dep_groups",
|
|
|
|
of_type=List[str],
|
2022-09-15 11:17:45 +00:00
|
|
|
default=[],
|
2023-01-10 02:34:35 +00:00
|
|
|
desc="List of Poetry dependency groups to install to the environment",
|
2022-09-15 11:17:45 +00:00
|
|
|
)
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
env_conf.add_config(
|
|
|
|
"install_project_deps",
|
|
|
|
of_type=bool,
|
|
|
|
default=True,
|
|
|
|
desc="Automatically install all Poetry primary dependencies to the environment",
|
2021-05-05 20:35:46 +00:00
|
|
|
)
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
env_conf.add_config(
|
|
|
|
"require_locked_deps",
|
|
|
|
of_type=bool,
|
2020-11-12 02:26:59 +00:00
|
|
|
default=False,
|
2023-01-10 02:34:35 +00:00
|
|
|
desc="Require all dependencies in the environment be installed using the Poetry lockfile",
|
2020-11-12 02:26:59 +00:00
|
|
|
)
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
env_conf.add_config(
|
|
|
|
"require_poetry",
|
|
|
|
of_type=bool,
|
2021-04-20 03:11:04 +00:00
|
|
|
default=False,
|
2023-01-10 02:34:35 +00:00
|
|
|
desc="Trigger a failure if Poetry is not available to Tox",
|
2021-04-20 03:11:04 +00:00
|
|
|
)
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
env_conf.add_config(
|
|
|
|
"locked_deps",
|
|
|
|
of_type=List[str],
|
|
|
|
default=[],
|
|
|
|
desc="List of locked dependencies to install to the environment using the Poetry lockfile",
|
2020-11-12 05:24:15 +00:00
|
|
|
)
|
|
|
|
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
@impl
|
|
|
|
def tox_on_install(
|
|
|
|
tox_env: ToxVirtualEnv, section: str # pylint: disable=unused-argument
|
|
|
|
) -> None:
|
2020-11-12 02:26:59 +00:00
|
|
|
"""Install the dependencies for the current environment
|
|
|
|
|
|
|
|
Loads the local Poetry environment and the corresponding lockfile then pulls the dependencies
|
|
|
|
specified by the Tox environment. Finally these dependencies are installed into the Tox
|
|
|
|
environment using the Poetry ``PipInstaller`` backend.
|
|
|
|
|
|
|
|
:param venv: Tox virtual environment object with configuration for the local Tox environment.
|
|
|
|
:param action: Tox action object
|
|
|
|
"""
|
|
|
|
try:
|
2023-01-10 02:34:35 +00:00
|
|
|
poetry = utilities.check_preconditions(tox_env)
|
2020-11-13 03:14:49 +00:00
|
|
|
except exceptions.SkipEnvironment as err:
|
2023-07-06 12:33:29 +00:00
|
|
|
if (
|
|
|
|
isinstance(err, exceptions.PoetryNotInstalledError)
|
|
|
|
and tox_env.conf["require_poetry"]
|
2020-12-05 19:57:44 +00:00
|
|
|
):
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.error(str(err))
|
2023-01-10 02:34:35 +00:00
|
|
|
raise err
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(str(err))
|
2023-01-10 02:34:35 +00:00
|
|
|
return
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(f"Loaded project pyproject.toml from {poetry.file}")
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
virtualenv = utilities.convert_virtualenv(tox_env)
|
2021-02-16 02:01:44 +00:00
|
|
|
|
2021-02-10 05:06:33 +00:00
|
|
|
if not poetry.locker.is_fresh():
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.warning(
|
2021-02-10 05:06:33 +00:00
|
|
|
f"The Poetry lock file is not up to date with the latest changes in {poetry.file}"
|
|
|
|
)
|
|
|
|
|
2020-12-04 22:11:32 +00:00
|
|
|
try:
|
2023-01-10 02:34:35 +00:00
|
|
|
if tox_env.conf["require_locked_deps"] and tox_env.conf["deps"].lines():
|
2020-12-04 22:11:32 +00:00
|
|
|
raise exceptions.LockedDepsRequiredError(
|
2023-01-10 02:34:35 +00:00
|
|
|
f"Unlocked dependencies '{tox_env.conf['deps']}' specified for environment '{tox_env.name}' which requires locked dependencies"
|
2020-12-04 22:11:32 +00:00
|
|
|
)
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2022-04-07 04:03:47 +00:00
|
|
|
packages = utilities.build_package_map(poetry)
|
2020-12-04 22:11:32 +00:00
|
|
|
|
2022-09-15 11:17:45 +00:00
|
|
|
group_deps = utilities.dedupe_packages(
|
|
|
|
list(
|
|
|
|
chain(
|
|
|
|
*[
|
|
|
|
utilities.find_group_deps(group, packages, virtualenv, poetry)
|
2023-01-10 02:34:35 +00:00
|
|
|
for group in tox_env.conf["poetry_dep_groups"]
|
2022-09-15 11:17:45 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
logger.info(
|
|
|
|
f"Identified {len(group_deps)} group dependencies to install to env"
|
|
|
|
)
|
|
|
|
|
2021-02-10 04:53:56 +00:00
|
|
|
env_deps = utilities.find_additional_deps(
|
2023-01-10 02:34:35 +00:00
|
|
|
packages, virtualenv, poetry, tox_env.conf["locked_deps"]
|
2021-02-10 04:53:56 +00:00
|
|
|
)
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(
|
|
|
|
f"Identified {len(env_deps)} environment dependencies to install to env"
|
2020-11-12 02:26:59 +00:00
|
|
|
)
|
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
# extras are not set in a testenv if skip_install=true
|
|
|
|
try:
|
|
|
|
extras = tox_env.conf["extras"]
|
|
|
|
except KeyError:
|
|
|
|
extras = []
|
|
|
|
|
2023-07-06 12:32:26 +00:00
|
|
|
if tox_env.conf["install_project_deps"]:
|
2021-02-10 04:53:56 +00:00
|
|
|
project_deps = utilities.find_project_deps(
|
2023-01-10 02:34:35 +00:00
|
|
|
packages, virtualenv, poetry, extras
|
2020-11-13 03:14:49 +00:00
|
|
|
)
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(
|
|
|
|
f"Identified {len(project_deps)} project dependencies to install to env"
|
2021-02-10 05:06:33 +00:00
|
|
|
)
|
2020-11-13 03:14:49 +00:00
|
|
|
else:
|
2022-09-07 16:00:37 +00:00
|
|
|
project_deps = []
|
2021-05-05 20:35:46 +00:00
|
|
|
logger.info("Env does not install project package dependencies, skipping")
|
2020-11-13 03:14:49 +00:00
|
|
|
except exceptions.ToxPoetryInstallerException as err:
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.error(str(err))
|
2023-01-10 02:34:35 +00:00
|
|
|
raise err
|
2020-12-04 22:11:32 +00:00
|
|
|
except Exception as err:
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.error(f"Internal plugin error: {err}")
|
2020-11-13 03:14:49 +00:00
|
|
|
raise err
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2023-01-10 02:34:35 +00:00
|
|
|
dependencies = utilities.dedupe_packages(group_deps + env_deps + project_deps)
|
2023-06-23 23:35:38 +00:00
|
|
|
|
|
|
|
logger.info(f"Installing {len(dependencies)} dependencies from Poetry lock file")
|
2021-04-16 06:05:08 +00:00
|
|
|
installer.install(
|
|
|
|
poetry,
|
2023-01-10 02:34:35 +00:00
|
|
|
tox_env,
|
2021-04-16 06:05:08 +00:00
|
|
|
dependencies,
|
2023-05-19 22:03:44 +00:00
|
|
|
tox_env.options.parallel_install_threads,
|
2020-11-12 02:26:59 +00:00
|
|
|
)
|