From 0504ae3a0289366fa9eb95e88238ee1344c7715f Mon Sep 17 00:00:00 2001 From: Enno Hermann Date: Tue, 7 May 2024 19:53:06 +0200 Subject: [PATCH] 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: https://github.com/numpy/numpydoc/blob/e7c6baf00f5f73a4a8f8318d0cb4e04949c9a5d1/tools/generate_requirements.py --- .pre-commit-config.yaml | 7 ++++++ Makefile | 3 ++- pyproject.toml | 2 ++ requirements.dev.txt | 4 ++++ scripts/generate_requirements.py | 39 ++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 scripts/generate_requirements.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 21e48987..f96f6f38 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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" diff --git a/Makefile b/Makefile index 4379b556..e3372d30 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pyproject.toml b/pyproject.toml index ef8554f8..ed64714e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/requirements.dev.txt b/requirements.dev.txt index 7f76b240..0095dae3 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -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' diff --git a/scripts/generate_requirements.py b/scripts/generate_requirements.py new file mode 100644 index 00000000..bbd32baf --- /dev/null +++ b/scripts/generate_requirements.py @@ -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()