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).
|
|
|
|
"""
|
2020-11-13 00:39:22 +00:00
|
|
|
from typing import Optional
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2021-02-10 01:38:53 +00:00
|
|
|
import tox
|
2020-11-12 02:26:59 +00:00
|
|
|
from tox.action import Action as ToxAction
|
|
|
|
from tox.config import Parser as ToxParser
|
|
|
|
from tox.venv import VirtualEnv as ToxVirtualEnv
|
|
|
|
|
2020-12-04 22:11:32 +00:00
|
|
|
from tox_poetry_installer import __about__
|
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
|
|
|
|
from tox_poetry_installer.datatypes import PackageMap
|
|
|
|
|
|
|
|
|
2021-02-10 01:38:53 +00:00
|
|
|
@tox.hookimpl
|
2020-11-12 02:26:59 +00:00
|
|
|
def tox_addoption(parser: ToxParser):
|
|
|
|
"""Add required configuration options to the tox INI file
|
|
|
|
|
|
|
|
Adds the ``require_locked_deps`` configuration option to the venv to check whether all
|
|
|
|
dependencies should be treated as locked or not.
|
|
|
|
"""
|
|
|
|
|
2020-12-05 19:57:44 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--require-poetry",
|
|
|
|
action="store_true",
|
|
|
|
dest="require_poetry",
|
|
|
|
help="Trigger a failure if Poetry is not available to Tox",
|
|
|
|
)
|
|
|
|
|
2021-04-16 06:05:08 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--parallelize-locked-install",
|
|
|
|
type=int,
|
|
|
|
dest="parallelize_locked_install",
|
|
|
|
default=None,
|
|
|
|
help="Number of worker threads to use for installing dependencies from the Poetry lockfile in parallel",
|
|
|
|
)
|
|
|
|
|
2020-11-12 02:26:59 +00:00
|
|
|
parser.add_testenv_attribute(
|
|
|
|
name="install_dev_deps",
|
|
|
|
type="bool",
|
|
|
|
default=False,
|
|
|
|
help="Automatically install all Poetry development dependencies to the environment",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_testenv_attribute(
|
|
|
|
name="require_locked_deps",
|
|
|
|
type="bool",
|
|
|
|
default=False,
|
|
|
|
help="Require all dependencies in the environment be installed using the Poetry lockfile",
|
|
|
|
)
|
|
|
|
|
2020-11-12 05:24:15 +00:00
|
|
|
parser.add_testenv_attribute(
|
|
|
|
name="locked_deps",
|
|
|
|
type="line-list",
|
|
|
|
help="List of locked dependencies to install to the environment using the Poetry lockfile",
|
|
|
|
)
|
|
|
|
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2021-02-10 01:38:53 +00:00
|
|
|
@tox.hookimpl
|
2020-11-13 00:39:22 +00:00
|
|
|
def tox_testenv_install_deps(venv: ToxVirtualEnv, action: ToxAction) -> Optional[bool]:
|
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:
|
2020-11-13 03:14:49 +00:00
|
|
|
poetry = utilities.check_preconditions(venv, action)
|
|
|
|
except exceptions.SkipEnvironment as err:
|
2020-12-05 19:57:44 +00:00
|
|
|
if (
|
|
|
|
isinstance(err, exceptions.PoetryNotInstalledError)
|
|
|
|
and venv.envconfig.config.option.require_poetry
|
|
|
|
):
|
|
|
|
venv.status = err.__class__.__name__
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.error(str(err))
|
2020-12-05 19:57:44 +00:00
|
|
|
return False
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(str(err))
|
2020-11-13 00:39:22 +00:00
|
|
|
return None
|
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
|
|
|
|
2021-02-16 02:01:44 +00:00
|
|
|
virtualenv = utilities.convert_virtualenv(venv)
|
|
|
|
|
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:
|
|
|
|
if venv.envconfig.require_locked_deps and venv.envconfig.deps:
|
|
|
|
raise exceptions.LockedDepsRequiredError(
|
|
|
|
f"Unlocked dependencies '{venv.envconfig.deps}' specified for environment '{venv.name}' which requires locked dependencies"
|
|
|
|
)
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2021-02-10 04:53:56 +00:00
|
|
|
packages: PackageMap = {
|
2020-12-04 22:11:32 +00:00
|
|
|
package.name: package
|
|
|
|
for package in poetry.locker.locked_repository(True).packages
|
|
|
|
}
|
|
|
|
|
|
|
|
if venv.envconfig.install_dev_deps:
|
2021-02-16 02:01:44 +00:00
|
|
|
dev_deps = utilities.find_dev_deps(packages, virtualenv, poetry)
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info(
|
|
|
|
f"Identified {len(dev_deps)} development dependencies to install to env"
|
2021-02-10 04:25:46 +00:00
|
|
|
)
|
2020-12-04 22:11:32 +00:00
|
|
|
else:
|
|
|
|
dev_deps = []
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info("Env does not install development dependencies, skipping")
|
2020-11-13 03:14:49 +00:00
|
|
|
|
2021-02-10 04:53:56 +00:00
|
|
|
env_deps = utilities.find_additional_deps(
|
2021-02-16 02:01:44 +00:00
|
|
|
packages, virtualenv, poetry, venv.envconfig.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
|
|
|
)
|
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
if not venv.envconfig.skip_install and not venv.envconfig.config.skipsdist:
|
2021-02-10 04:53:56 +00:00
|
|
|
project_deps = utilities.find_project_deps(
|
2021-02-16 02:01:44 +00:00
|
|
|
packages, virtualenv, poetry, venv.envconfig.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:
|
2020-11-26 18:43:33 +00:00
|
|
|
project_deps = []
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.info("Env does not install project package, skipping")
|
2020-11-13 03:14:49 +00:00
|
|
|
except exceptions.ToxPoetryInstallerException as err:
|
2020-12-04 22:11:32 +00:00
|
|
|
venv.status = err.__class__.__name__
|
2021-04-16 23:57:50 +00:00
|
|
|
logger.error(str(err))
|
2020-12-04 22:11:32 +00:00
|
|
|
return False
|
|
|
|
except Exception as err:
|
|
|
|
venv.status = "InternalError"
|
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
|
|
|
|
2021-02-10 04:25:46 +00:00
|
|
|
dependencies = dev_deps + env_deps + project_deps
|
2021-04-16 06:05:08 +00:00
|
|
|
log_parallel = (
|
|
|
|
f" (using {venv.envconfig.config.option.parallelize_locked_install} threads)"
|
|
|
|
if venv.envconfig.config.option.parallelize_locked_install
|
|
|
|
else ""
|
|
|
|
)
|
|
|
|
|
2020-12-04 22:11:32 +00:00
|
|
|
action.setactivity(
|
|
|
|
__about__.__title__,
|
2021-04-16 06:05:08 +00:00
|
|
|
f"Installing {len(dependencies)} dependencies from Poetry lock file{log_parallel}",
|
|
|
|
)
|
|
|
|
installer.install(
|
|
|
|
poetry,
|
|
|
|
venv,
|
|
|
|
dependencies,
|
|
|
|
venv.envconfig.config.option.parallelize_locked_install,
|
2020-11-12 02:26:59 +00:00
|
|
|
)
|
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
return venv.envconfig.require_locked_deps or None
|