Skip to content

Commit

Permalink
Put the 'get_python_requires' venv cache in .tox/
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
sirosen committed Aug 16, 2023
1 parent e872141 commit 7556856
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
20 changes: 16 additions & 4 deletions scripts/ensure_min_python_is_tested.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
23 changes: 15 additions & 8 deletions scripts/get_python_requires.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7556856

Please sign in to comment.