Skip to content

Commit

Permalink
Add tox_on_install plugin hook
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Dec 12, 2022
1 parent 08d8924 commit cb90536
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2687.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`tox_on_install <tox.plugin.spec.tox_on_install>` plugin hook - by :user:`gaborbernat`.
4 changes: 4 additions & 0 deletions src/tox/plugin/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pathlib import Path
from types import ModuleType
from typing import Any

import pluggy

Expand Down Expand Up @@ -76,6 +77,9 @@ def tox_before_run_commands(self, tox_env: ToxEnv) -> None:
def tox_after_run_commands(self, tox_env: ToxEnv, exit_code: int, outcomes: list[Outcome]) -> None:
self.manager.hook.tox_after_run_commands(tox_env=tox_env, exit_code=exit_code, outcomes=outcomes)

def tox_on_install(self, tox_env: ToxEnv, arguments: Any, section: str, of_type: str) -> None:
self.manager.hook.tox_on_install(tox_env=tox_env, arguments=arguments, section=section, of_type=of_type)

def load_plugins(self, path: Path) -> None:
for _plugin in self.manager.get_plugins(): # make sure we start with a clean state, repeated in memory run
self.manager.unregister(_plugin)
Expand Down
13 changes: 13 additions & 0 deletions src/tox/plugin/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ def tox_after_run_commands(tox_env: ToxEnv, exit_code: int, outcomes: list[Outco
"""


@_spec
def tox_on_install(tox_env: ToxEnv, arguments: Any, section: str, of_type: str) -> None: # noqa: U100
"""
Called before executing an installation command.
:param tox_env: the tox environment where the command runs in
:param arguments: installation arguments
:param section: section of the installation
:param of_type: type of the installation
"""


__all__ = [
"NAME",
"tox_register_tox_env",
Expand All @@ -91,4 +103,5 @@ def tox_after_run_commands(tox_env: ToxEnv, exit_code: int, outcomes: list[Outco
"tox_add_env_config",
"tox_before_run_commands",
"tox_after_run_commands",
"tox_on_install",
]
6 changes: 6 additions & 0 deletions src/tox/tox_env/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def executor(self) -> Execute:
def installer(self) -> Installer[Any]:
raise NotImplementedError

def _install(self, arguments: Any, section: str, of_type: str) -> None:
from tox.plugin.manager import MANAGER

MANAGER.tox_on_install(self, arguments, section, of_type)
self.installer.install(arguments, section, of_type)

def __repr__(self) -> str:
return f"{self.__class__.__name__}(name={self.conf['env_name']})"

Expand Down
2 changes: 1 addition & 1 deletion src/tox/tox_env/python/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, create_args: ToxEnvCreateArgs) -> None:
def _setup_env(self) -> None:
"""setup the tox environment"""
super()._setup_env()
self.installer.install(self.requires(), PythonPackageToxEnv.__name__, "requires")
self._install(self.requires(), PythonPackageToxEnv.__name__, "requires")

@abstractmethod
def requires(self) -> tuple[Requirement, ...] | PythonDeps:
Expand Down
2 changes: 1 addition & 1 deletion src/tox/tox_env/python/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _setup_env(self) -> None:

def _install_deps(self) -> None:
requirements_file: PythonDeps = self.conf["deps"]
self.installer.install(requirements_file, PythonRun.__name__, "deps")
self._install(requirements_file, PythonRun.__name__, "deps")

def _build_packages(self) -> list[Package]:
package_env = self.package_env
Expand Down
6 changes: 3 additions & 3 deletions src/tox/tox_env/python/virtual_env/package/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ def _setup_env(self) -> None:
if not self._frontend.optional_hooks["build_editable"]:
raise BuildEditableNotSupported
build_requires = self._frontend.get_requires_for_build_editable().requires
self.installer.install(build_requires, PythonPackageToxEnv.__name__, "requires_for_build_editable")
self._install(build_requires, PythonPackageToxEnv.__name__, "requires_for_build_editable")
if "wheel" in self.builds:
build_requires = self._frontend.get_requires_for_build_wheel().requires
self.installer.install(build_requires, PythonPackageToxEnv.__name__, "requires_for_build_wheel")
self._install(build_requires, PythonPackageToxEnv.__name__, "requires_for_build_wheel")
if "sdist" in self.builds or "external" in self.builds:
build_requires = self._frontend.get_requires_for_build_sdist().requires
self.installer.install(build_requires, PythonPackageToxEnv.__name__, "requires_for_build_sdist")
self._install(build_requires, PythonPackageToxEnv.__name__, "requires_for_build_sdist")

def _teardown(self) -> None:
executor = self._frontend.backend_executor
Expand Down
2 changes: 1 addition & 1 deletion src/tox/tox_env/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _register_package_conf(self) -> bool:

def _setup_pkg(self) -> None:
self._packages = self._build_packages()
self.installer.install(self._packages, RunToxEnv.__name__, "package")
self._install(self._packages, RunToxEnv.__name__, "package")
self._handle_journal_package(self.journal, self._packages)

@staticmethod
Expand Down

0 comments on commit cb90536

Please sign in to comment.