2024-08-13 17:06:12 +00:00
|
|
|
"""Install the dependencies for the current environment
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2024-08-13 17:06:12 +00:00
|
|
|
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.
|
2020-11-12 02:26:59 +00:00
|
|
|
"""
|
2024-08-16 18:27:46 +00:00
|
|
|
|
2024-08-20 17:56:17 +00:00
|
|
|
import itertools
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2024-08-20 17:56:17 +00:00
|
|
|
import tox.plugin
|
|
|
|
import tox.tox_env.api
|
2020-11-12 02:26:59 +00:00
|
|
|
|
|
|
|
from tox_poetry_installer import exceptions
|
2021-04-16 23:57:50 +00:00
|
|
|
from tox_poetry_installer import logger
|
2024-08-13 17:44:25 +00:00
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import build_package_map
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import check_preconditions
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import convert_virtualenv
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import dedupe_packages
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import find_additional_deps
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import find_group_deps
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import find_project_deps
|
|
|
|
from tox_poetry_installer.hooks._tox_on_install_helpers import install_package
|
2020-11-12 02:26:59 +00:00
|
|
|
|
|
|
|
|
2024-08-15 18:41:34 +00:00
|
|
|
# pylint: disable=missing-function-docstring,unused-argument
|
2024-08-20 17:56:17 +00:00
|
|
|
@tox.plugin.impl
|
|
|
|
def tox_on_install(tox_env: tox.tox_env.api.ToxEnv, *args) -> None:
|
2020-11-12 02:26:59 +00:00
|
|
|
try:
|
2024-08-13 17:44:25 +00:00
|
|
|
poetry = 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
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
virtualenv = convert_virtualenv(tox_env)
|
2021-02-16 02:01:44 +00:00
|
|
|
|
2024-08-15 15:59:54 +00:00
|
|
|
try:
|
|
|
|
if not poetry.locker.is_fresh():
|
|
|
|
logger.warning(
|
|
|
|
f"The Poetry lock file is not up to date with the latest changes in {poetry.file}"
|
|
|
|
)
|
|
|
|
except FileNotFoundError as err:
|
|
|
|
logger.error(f"Could not parse lockfile: {err}")
|
|
|
|
raise exceptions.LockfileParsingError(
|
|
|
|
f"Could not parse lockfile: {err}"
|
|
|
|
) from err
|
2021-02-10 05:06:33 +00:00
|
|
|
|
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
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
packages = build_package_map(poetry)
|
2020-12-04 22:11:32 +00:00
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
group_deps = dedupe_packages(
|
2022-09-15 11:17:45 +00:00
|
|
|
list(
|
2024-08-20 17:56:17 +00:00
|
|
|
itertools.chain(
|
2022-09-15 11:17:45 +00:00
|
|
|
*[
|
2024-08-13 17:44:25 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
env_deps = 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"]:
|
2024-08-13 17:44:25 +00:00
|
|
|
project_deps = find_project_deps(packages, virtualenv, poetry, extras)
|
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
|
|
|
|
2024-08-13 17:44:25 +00:00
|
|
|
dependencies = 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")
|
2024-08-13 17:44:25 +00:00
|
|
|
install_package(
|
2021-04-16 06:05:08 +00:00
|
|
|
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
|
|
|
)
|