From 40d4b1f4fe62b9c07c8a7bb7b817d535398a7d6c Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Wed, 8 Jan 2025 14:16:04 -0600 Subject: [PATCH] Add `globus_cli_rmtree` via toxfile plugin This imitates our prior art in globus-sdk to move `allowlist_externals = rm` usage into a plugin which can take care of this for us. It also sets up `toxfile.py` for us in case we want to put any other customizations in there. --- tox.ini | 3 +-- toxfile.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 toxfile.py diff --git a/tox.ini b/tox.ini index 62f2a561..ee5f4498 100644 --- a/tox.ini +++ b/tox.ini @@ -78,8 +78,7 @@ skip_install = true deps = build twine -allowlist_externals = rm -commands_pre = rm -rf dist/ +globus_cli_rmtree = dist # check that twine validating package data works commands = python -m build diff --git a/toxfile.py b/toxfile.py new file mode 100644 index 00000000..4c3f26fd --- /dev/null +++ b/toxfile.py @@ -0,0 +1,46 @@ +""" +This is a very small 'tox' plugin. +'toxfile.py' is a special name for auto-loading a plugin without defining package +metadata. + +For full doc, see: https://tox.wiki/en/latest/plugins.html + +Methods decorated below with `tox.plugin.impl` are hook implementations. +We only implement hooks which we need. +""" + +from __future__ import annotations + +import logging +import pathlib +import shutil +import typing as t + +from tox.plugin import impl + +if t.TYPE_CHECKING: + from tox.config.sets import EnvConfigSet + from tox.session.state import State + from tox.tox_env.api import ToxEnv + +log = logging.getLogger(__name__) + + +@impl +def tox_add_env_config(env_conf: EnvConfigSet, state: State) -> None: + env_conf.add_config( + keys=["globus_cli_rmtree"], + of_type=list[str], + default=[], + desc="A dir tree to remove before running the environment commands", + ) + + +@impl +def tox_before_run_commands(tox_env: ToxEnv) -> None: + cli_rmtree = tox_env.conf.load("globus_cli_rmtree") + for name in cli_rmtree: + path = pathlib.Path(name) + if path.exists(): + log.warning(f"globus_cli_rmtree: {path}") + shutil.rmtree(path)