diff --git a/.config/requirements.in b/.config/requirements.in index 35bed9a..38c8f19 100644 --- a/.config/requirements.in +++ b/.config/requirements.in @@ -5,6 +5,7 @@ click >= 7.1.2 colorama # for Windows diskcache >= 5.2.1 importlib-metadata +packaging pip>=21.0.1 # py_package pluggy pygments diff --git a/pyproject.toml b/pyproject.toml index 58547fd..fd3b17b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,7 @@ pre-commit = "mk.tools.pre_commit:PreCommitTool" shell = "mk.tools.shell:ShellTool" taskfile = "mk.tools.taskfile:TaskfileTool" tox = "mk.tools.tox:ToxTool" +nox = "mk.tools.nox:NoxTool" [tool.black] diff --git a/src/mk/tools/nox.py b/src/mk/tools/nox.py new file mode 100644 index 0000000..e81fd42 --- /dev/null +++ b/src/mk/tools/nox.py @@ -0,0 +1,47 @@ +""" +Implementation of the nox tool support. +""" + +from __future__ import annotations + +import json +import logging +from pathlib import Path + +from packaging.version import Version + +from mk.exec import run_or_fail +from mk.tools import Action, Tool + +# This is the first version to support --json +_MINIMUM_NOX_VERSION = Version("2023.04.22") + + +class NoxTool(Tool): + name = "nox" + + def is_present(self, path: Path) -> bool: + return (path / "noxfile.py").is_file() + + def actions(self) -> list[Action]: + actions: list[Action] = [] + version: str = run_or_fail(["nox", "--version"], tee=False).stderr.strip() + if Version(version) < _MINIMUM_NOX_VERSION: + logging.warning( + "Failed to retrieve nox sessions: " + "nox version %s is too old. Minimum supported version is %s.", + version, + _MINIMUM_NOX_VERSION, + ) + return [] + results = run_or_fail(["nox", "--list", "--json"], tee=False) + data = json.loads(results.stdout) + actions = [ + Action(session["session"], tool=self, description=session["description"]) + for session in data + ] + return actions + + def run(self, action: Action | None = None) -> None: + cmd = ["nox"] if not action else ["nox", "-s", action.name] + run_or_fail(cmd, tee=True)