Skip to content

Commit

Permalink
ci: add script to automatically generate requirements.dev.txt
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eginhard committed May 7, 2024
1 parent 65e6bf1 commit 1ec6a5b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ deps: ## install 🐸 requirements.
pip install -r requirements.txt

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
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,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 = [
Expand Down
4 changes: 4 additions & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -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'
39 changes: 39 additions & 0 deletions scripts/generate_requirements.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1ec6a5b

Please sign in to comment.