ci: add script to automatically generate requirements.dev.txt

Having this file is still useful to allow installing *only* dev requirements
(e.g. in CI) with:
  pip install -r requirements.dev.txt

Generate that file automatically from the pyproject.toml based on:
e7c6baf00f/tools/generate_requirements.py
This commit is contained in:
Enno Hermann 2024-05-07 19:53:06 +02:00
parent ec50006855
commit 0504ae3a02
5 changed files with 54 additions and 1 deletions

View File

@ -15,3 +15,10 @@ repos:
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: local
hooks:
- id: generate_requirements.py
name: generate_requirements.py
language: system
entry: python scripts/generate_requirements.py
files: "pyproject.toml|requirements.*\\.txt|tools/generate_requirements.py"

View File

@ -66,7 +66,8 @@ build-docs: ## build the docs
cd docs && make clean && make build
install: ## install 🐸 TTS for development.
pip install -e .[all]
pip install -e .[all,dev]
pre-commit install
docs: ## build the docs
$(MAKE) -C docs clean && $(MAKE) -C docs html

View File

@ -94,7 +94,9 @@ dev = [
"black==24.2.0",
"coverage[toml]",
"nose2",
"pre-commit",
"ruff==0.3.0",
"tomli; python_version < '3.11'",
]
# Dependencies for building the documentation
docs = [

View File

@ -1,4 +1,8 @@
# Generated via scripts/generate_requirements.py and pre-commit hook.
# Do not edit this file; modify pyproject.toml instead.
black==24.2.0
coverage[toml]
nose2
pre-commit
ruff==0.3.0
tomli; python_version < '3.11'

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python
"""Generate requirements/*.txt files from pyproject.toml.
Adapted from:
https://github.com/numpy/numpydoc/blob/e7c6baf00f5f73a4a8f8318d0cb4e04949c9a5d1/tools/generate_requirements.py
"""
import sys
from pathlib import Path
try: # standard module since Python 3.11
import tomllib as toml
except ImportError:
try: # available for older Python via pip
import tomli as toml
except ImportError:
sys.exit("Please install `tomli` first: `pip install tomli`")
script_pth = Path(__file__)
repo_dir = script_pth.parent.parent
script_relpth = script_pth.relative_to(repo_dir)
header = [
f"# Generated via {script_relpth.as_posix()} and pre-commit hook.",
"# Do not edit this file; modify pyproject.toml instead.",
]
def generate_requirement_file(name: str, req_list: list[str]) -> None:
req_fname = repo_dir / f"requirements.{name}.txt"
req_fname.write_text("\n".join(header + req_list) + "\n")
def main() -> None:
pyproject = toml.loads((repo_dir / "pyproject.toml").read_text())
generate_requirement_file("dev", pyproject["project"]["optional-dependencies"]["dev"])
if __name__ == "__main__":
main()