From 62f90de297af3d5a714657df882f2a9295135f71 Mon Sep 17 00:00:00 2001 From: Ethan Paul <24588726+enpaul@users.noreply.github.com> Date: Tue, 13 Aug 2024 13:06:12 -0400 Subject: [PATCH] Reorganize hooks into dedicated submodule Fixes #93 --- tox_poetry_installer/hooks/__init__.py | 4 ++ .../hooks/tox_add_env_config.py | 43 +++++++++++ tox_poetry_installer/hooks/tox_add_option.py | 16 +++++ .../{hooks.py => hooks/tox_on_install.py} | 72 ++----------------- 4 files changed, 67 insertions(+), 68 deletions(-) create mode 100644 tox_poetry_installer/hooks/__init__.py create mode 100644 tox_poetry_installer/hooks/tox_add_env_config.py create mode 100644 tox_poetry_installer/hooks/tox_add_option.py rename tox_poetry_installer/{hooks.py => hooks/tox_on_install.py} (57%) diff --git a/tox_poetry_installer/hooks/__init__.py b/tox_poetry_installer/hooks/__init__.py new file mode 100644 index 0000000..837f035 --- /dev/null +++ b/tox_poetry_installer/hooks/__init__.py @@ -0,0 +1,4 @@ +# pylint: disable=missing-module-docstring +from tox_poetry_installer.hooks.tox_add_env_config import tox_add_env_config +from tox_poetry_installer.hooks.tox_add_option import tox_add_option +from tox_poetry_installer.hooks.tox_on_install import tox_on_install diff --git a/tox_poetry_installer/hooks/tox_add_env_config.py b/tox_poetry_installer/hooks/tox_add_env_config.py new file mode 100644 index 0000000..301f4b0 --- /dev/null +++ b/tox_poetry_installer/hooks/tox_add_env_config.py @@ -0,0 +1,43 @@ +"""Add required env configuration options to the tox INI file""" +from typing import List + +from tox.config.sets import EnvConfigSet +from tox.plugin import impl + + +@impl +def tox_add_env_config(env_conf: EnvConfigSet): + env_conf.add_config( + "poetry_dep_groups", + of_type=List[str], + default=[], + desc="List of Poetry dependency groups to install to the environment", + ) + + env_conf.add_config( + "install_project_deps", + of_type=bool, + default=True, + desc="Automatically install all Poetry primary dependencies to the environment", + ) + + env_conf.add_config( + "require_locked_deps", + of_type=bool, + default=False, + desc="Require all dependencies in the environment be installed using the Poetry lockfile", + ) + + env_conf.add_config( + "require_poetry", + of_type=bool, + default=False, + desc="Trigger a failure if Poetry is not available to Tox", + ) + + 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", + ) diff --git a/tox_poetry_installer/hooks/tox_add_option.py b/tox_poetry_installer/hooks/tox_add_option.py new file mode 100644 index 0000000..9e71fdf --- /dev/null +++ b/tox_poetry_installer/hooks/tox_add_option.py @@ -0,0 +1,16 @@ +"""Add additional command line arguments to tox to configure plugin behavior""" +from tox.config.cli.parser import ToxParser +from tox.plugin import impl + +from tox_poetry_installer import constants + + +@impl +def tox_add_option(parser: ToxParser): + 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", + ) diff --git a/tox_poetry_installer/hooks.py b/tox_poetry_installer/hooks/tox_on_install.py similarity index 57% rename from tox_poetry_installer/hooks.py rename to tox_poetry_installer/hooks/tox_on_install.py index 312d97a..cf13a94 100644 --- a/tox_poetry_installer/hooks.py +++ b/tox_poetry_installer/hooks/tox_on_install.py @@ -1,88 +1,24 @@ -"""Main hook definition module +"""Install the dependencies for the current environment -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). +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. """ from itertools import chain -from typing import List -from tox.config.cli.parser import ToxParser -from tox.config.sets import EnvConfigSet from tox.plugin import impl from tox.tox_env.api import ToxEnv as ToxVirtualEnv -from tox_poetry_installer import constants from tox_poetry_installer import exceptions from tox_poetry_installer import installer from tox_poetry_installer import logger from tox_poetry_installer import utilities -@impl -def tox_add_option(parser: ToxParser): - """Add additional command line arguments to tox to configure plugin behavior""" - 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", - ) - - -@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], - default=[], - desc="List of Poetry dependency groups to install to the environment", - ) - - env_conf.add_config( - "install_project_deps", - of_type=bool, - default=True, - desc="Automatically install all Poetry primary dependencies to the environment", - ) - - env_conf.add_config( - "require_locked_deps", - of_type=bool, - default=False, - desc="Require all dependencies in the environment be installed using the Poetry lockfile", - ) - - env_conf.add_config( - "require_poetry", - of_type=bool, - default=False, - desc="Trigger a failure if Poetry is not available to Tox", - ) - - 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", - ) - - @impl def tox_on_install( tox_env: ToxVirtualEnv, section: str # pylint: disable=unused-argument ) -> None: - """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: poetry = utilities.check_preconditions(tox_env) except exceptions.SkipEnvironment as err: