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).
|
|
|
|
"""
|
|
|
|
from typing import List
|
2020-11-13 00:39:22 +00:00
|
|
|
from typing import Optional
|
2020-11-12 02:26:59 +00:00
|
|
|
|
|
|
|
from poetry.core.packages import Package as PoetryPackage
|
|
|
|
from poetry.poetry import Poetry
|
|
|
|
from tox import hookimpl
|
|
|
|
from tox import reporter
|
|
|
|
from tox.action import Action as ToxAction
|
|
|
|
from tox.config import Parser as ToxParser
|
|
|
|
from tox.venv import VirtualEnv as ToxVirtualEnv
|
|
|
|
|
|
|
|
from tox_poetry_installer import constants
|
|
|
|
from tox_poetry_installer import exceptions
|
|
|
|
from tox_poetry_installer import utilities
|
|
|
|
from tox_poetry_installer.datatypes import PackageMap
|
|
|
|
|
|
|
|
|
|
|
|
@hookimpl
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
@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:
|
|
|
|
reporter.verbosity1(str(err))
|
2020-11-13 00:39:22 +00:00
|
|
|
return None
|
2020-11-12 02:26:59 +00:00
|
|
|
|
|
|
|
reporter.verbosity1(
|
2020-11-13 03:14:49 +00:00
|
|
|
f"{constants.REPORTER_PREFIX} Loaded project pyproject.toml from {poetry.file}"
|
2020-11-12 02:26:59 +00:00
|
|
|
)
|
|
|
|
|
2020-11-12 05:24:15 +00:00
|
|
|
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-13 03:14:49 +00:00
|
|
|
package_map: PackageMap = {
|
|
|
|
package.name: package
|
|
|
|
for package in poetry.locker.locked_repository(True).packages
|
|
|
|
}
|
2020-11-12 02:26:59 +00:00
|
|
|
|
|
|
|
if venv.envconfig.install_dev_deps:
|
2020-11-13 03:14:49 +00:00
|
|
|
dev_deps: List[PoetryPackage] = [
|
2020-11-12 02:26:59 +00:00
|
|
|
dep
|
2020-11-13 03:14:49 +00:00
|
|
|
for dep in package_map.values()
|
2020-11-12 02:26:59 +00:00
|
|
|
if dep not in poetry.locker.locked_repository(False).packages
|
|
|
|
]
|
2020-11-13 03:14:49 +00:00
|
|
|
else:
|
|
|
|
dev_deps = []
|
|
|
|
|
|
|
|
reporter.verbosity1(
|
|
|
|
f"{constants.REPORTER_PREFIX} Identified {len(dev_deps)} development dependencies to install to env"
|
|
|
|
)
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
try:
|
|
|
|
env_deps: List[PoetryPackage] = []
|
|
|
|
for dep in venv.envconfig.locked_deps:
|
|
|
|
env_deps += utilities.find_transients(package_map, dep.lower())
|
2020-11-12 02:26:59 +00:00
|
|
|
reporter.verbosity1(
|
2020-11-13 03:14:49 +00:00
|
|
|
f"{constants.REPORTER_PREFIX} 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:
|
|
|
|
project_deps: List[PoetryPackage] = _find_project_dependencies(
|
|
|
|
venv, poetry, package_map
|
|
|
|
)
|
|
|
|
else:
|
2020-11-26 18:43:33 +00:00
|
|
|
project_deps = []
|
2020-11-13 03:14:49 +00:00
|
|
|
reporter.verbosity1(
|
|
|
|
f"{constants.REPORTER_PREFIX} Skipping installation of project dependencies, env does not install project package"
|
|
|
|
)
|
|
|
|
reporter.verbosity1(
|
|
|
|
f"{constants.REPORTER_PREFIX} Identified {len(project_deps)} project dependencies to install to env"
|
|
|
|
)
|
|
|
|
except exceptions.ToxPoetryInstallerException as err:
|
|
|
|
venv.status = "lockfile installation failed"
|
|
|
|
reporter.error(f"{constants.REPORTER_PREFIX} {err}")
|
|
|
|
raise err
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
dependencies = list(set(dev_deps + env_deps + project_deps))
|
2020-11-12 02:26:59 +00:00
|
|
|
reporter.verbosity0(
|
2020-11-13 03:14:49 +00:00
|
|
|
f"{constants.REPORTER_PREFIX} Installing {len(dependencies)} dependencies to env '{action.name}'"
|
2020-11-12 02:26:59 +00:00
|
|
|
)
|
|
|
|
utilities.install_to_venv(poetry, venv, dependencies)
|
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
return venv.envconfig.require_locked_deps or None
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
|
|
|
|
def _find_project_dependencies(
|
2020-11-12 02:26:59 +00:00
|
|
|
venv: ToxVirtualEnv, poetry: Poetry, packages: PackageMap
|
2020-11-13 03:14:49 +00:00
|
|
|
) -> List[PoetryPackage]:
|
2020-11-12 02:26:59 +00:00
|
|
|
"""Install the dependencies of the project package
|
|
|
|
|
|
|
|
Install all primary dependencies of the project package.
|
|
|
|
|
|
|
|
:param venv: Tox virtual environment to install the packages to
|
|
|
|
:param poetry: Poetry object the packages were sourced from
|
|
|
|
:param packages: Mapping of package names to the corresponding package object
|
|
|
|
"""
|
|
|
|
|
|
|
|
base_dependencies: List[PoetryPackage] = [
|
|
|
|
packages[item.name]
|
|
|
|
for item in poetry.package.requires
|
|
|
|
if not item.is_optional()
|
|
|
|
]
|
|
|
|
|
|
|
|
extra_dependencies: List[PoetryPackage] = []
|
|
|
|
for extra in venv.envconfig.extras:
|
|
|
|
try:
|
|
|
|
extra_dependencies += [
|
|
|
|
packages[item.name] for item in poetry.package.extras[extra]
|
|
|
|
]
|
|
|
|
except KeyError:
|
|
|
|
raise exceptions.ExtraNotFoundError(
|
|
|
|
f"Environment '{venv.name}' specifies project extra '{extra}' which was not found in the lockfile"
|
|
|
|
) from None
|
|
|
|
|
|
|
|
dependencies: List[PoetryPackage] = []
|
|
|
|
for dep in base_dependencies + extra_dependencies:
|
2020-11-13 03:14:49 +00:00
|
|
|
dependencies += utilities.find_transients(packages, dep.name.lower())
|
2020-11-12 02:26:59 +00:00
|
|
|
|
2020-11-13 03:14:49 +00:00
|
|
|
return dependencies
|