From 75568562c947b2cfa5acf490d21b74d8fca341bc Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Wed, 16 Aug 2023 12:44:09 -0500 Subject: [PATCH] Put the 'get_python_requires' venv cache in .tox/ Allow passing a cache directory explicitly to `get_python_requires.py`. Subsequently, the wrapping script, `ensure_min_python_is_tested.py` can insist on an env var which provides the cache dir path. Finally, `tox.ini` gets a final tweak to set this value via setenv to the `envdir` (the virtualenv dir which also contains the tmp and log dirs). The choice of an env var over parsing an argument is merely that it is marginally simpler to read an env var and fail on its absence than it is to do the same with an argument. (At the very least, *any* argument parsing opens the question of whether or not argparse should be used.) --- scripts/ensure_min_python_is_tested.py | 20 ++++++++++++++++---- scripts/get_python_requires.py | 23 +++++++++++++++-------- tox.ini | 2 ++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/scripts/ensure_min_python_is_tested.py b/scripts/ensure_min_python_is_tested.py index 279e839b8..67f5bb312 100644 --- a/scripts/ensure_min_python_is_tested.py +++ b/scripts/ensure_min_python_is_tested.py @@ -1,6 +1,8 @@ -#!/usr/bin/env python -# requires that a YAML implementation is available in order to extract github -# workflows data +# this script can only be called via +# tox -e check-min-python-is-tested +# +# no other usages are supported +import os import pathlib import subprocess import sys @@ -17,8 +19,18 @@ YAML = ruamel.yaml.YAML(typ="safe") REPO_ROOT = pathlib.Path(__file__).parent.parent +try: + VENV_CACHE_DIR = os.environ["VENV_CACHE_DIR"] +except KeyError: + raise RuntimeError( + "Cannot run ensure_min_python_is_tested.py without explicitly " + "setting VENV_CACHE dir. " + "Please ensure that you are invoking it with " + "'tox r -e check-min-python-is-tested'." + ) + proc = subprocess.run( - ["python", "scripts/get_python_requires.py"], + ["python", "scripts/get_python_requires.py", "--venv-cache-dir", VENV_CACHE_DIR], check=True, capture_output=True, cwd=REPO_ROOT, diff --git a/scripts/get_python_requires.py b/scripts/get_python_requires.py index df9709387..bdb615557 100644 --- a/scripts/get_python_requires.py +++ b/scripts/get_python_requires.py @@ -33,19 +33,26 @@ help="How to output the python_requires data. " "The default ('lower-bound') mode will print only the lower bound version number.", ) +parser.add_argument( + "--venv-cache-dir", + default=None, + help="A directory in which the 'build' virtualenv will be kept for reuse.", +) args = parser.parse_args() -if platform.system() == "Windows": - win_cache_dir = os.getenv("LOCALAPPDATA", os.getenv("APPDATA")) - assert win_cache_dir, "cannot find APPDATA dir, cannot cache, abort!" - cache_dir = pathlib.Path(win_cache_dir) +if args.venv_cache_dir: + VENV_CACHE_DIR = pathlib.Path(args.venv_cache_dir) else: - cache_dir = pathlib.Path.home() / ".cache" - -VENV_CACHE_DIR = cache_dir / "get_python_requires" + if platform.system() == "Windows": + win_cache_dir = os.getenv("LOCALAPPDATA", os.getenv("APPDATA")) + assert win_cache_dir, "cannot find APPDATA dir, cannot cache, abort!" + cache_dir = pathlib.Path(win_cache_dir) + else: + cache_dir = pathlib.Path.home() / ".cache" + VENV_CACHE_DIR = cache_dir / "get_python_requires" VENV_CACHE_DIR.mkdir(exist_ok=True) -BUILD_VENV = VENV_CACHE_DIR / "venv-build" +BUILD_VENV = VENV_CACHE_DIR / "get-python-requires-build" BUILD_PYTHON = str(BUILD_VENV / "bin" / "python") if BUILD_VENV.exists(): if args.verbose: diff --git a/tox.ini b/tox.ini index a534188e3..6502782d9 100644 --- a/tox.ini +++ b/tox.ini @@ -105,6 +105,8 @@ description = Check the Requires-Python metadata against CI config skip_install = true deps = ruamel.yaml<0.18 commands = python scripts/ensure_min_python_is_tested.py +setenv = + VENV_CACHE_DIR={envdir} [testenv:prepare-release] skip_install = true