Skip to content

Commit 0e7eb0a

Browse files
committed
Generate requirements files from pyproject.toml
This PR is based on numpy/numpydoc#483 and scikit-image/scikit-image#7085, which make pyproject.toml a primary file without any template, and adds a pre-commit script to write requirements files. It doesn't really change anything, but I would like to keep everything consistent.
1 parent 669f7b0 commit 0e7eb0a

11 files changed

+55
-232
lines changed

.pre-commit-config.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ repos:
2424
- --fix
2525
- repo: local
2626
hooks:
27-
- id: pyproject.toml
28-
name: pyproject.toml
27+
- id: generate_requirements.py
28+
name: generate_requirements.py
2929
language: system
30-
entry: python tools/generate_pyproject.toml.py
31-
files: "pyproject.toml|requirements/.*\\.txt|tools/.*pyproject.*"
30+
entry: python tools/generate_requirements.py
31+
files: "pyproject.toml|requirements/.*\\.txt|tools/generate_requirements.py"

pyproject.toml

-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
################################################################################
2-
# DO NOT EDIT
3-
# AUTOGENERATED BY
4-
#
5-
# $ python tools/generate_pyproject.toml.py
6-
#
7-
# EDIT tools/pyproject.toml.in AND RUN THAT SCRIPT.
8-
#
9-
################################################################################
10-
111
[build-system]
122
build-backend = 'setuptools.build_meta'
133
requires = ['setuptools>=61.2']

requirements/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Index
44

5+
These files are generated by `tools/generate_requirements.py` from `pyproject.toml`.
6+
57
- [`default.txt`](default.txt)
68
Default requirements
79
- [`extra.txt`](extra.txt)

requirements/default.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Generated via tools/generate_requirements.py and pre-commit hook.
2+
# Do not edit this file; modify pyproject.toml instead.
13
numpy>=1.22
24
scipy>=1.9,!=1.11.0,!=1.11.1
35
matplotlib>=3.5

requirements/developer.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Generated via tools/generate_requirements.py and pre-commit hook.
2+
# Do not edit this file; modify pyproject.toml instead.
13
pre-commit>=3.2
24
mypy>=1.1
35
rtoml

requirements/doc.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Generated via tools/generate_requirements.py and pre-commit hook.
2+
# Do not edit this file; modify pyproject.toml instead.
13
sphinx>=7
24
pydata-sphinx-theme>=0.14
35
sphinx-gallery>=0.14

requirements/extra.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Generated via tools/generate_requirements.py and pre-commit hook.
2+
# Do not edit this file; modify pyproject.toml instead.
13
lxml>=4.6
24
pygraphviz>=1.11
35
pydot>=1.4.2

requirements/test.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# Generated via tools/generate_requirements.py and pre-commit hook.
2+
# Do not edit this file; modify pyproject.toml instead.
13
pytest>=7.2
24
pytest-cov>=4.0

tools/generate_pyproject.toml.py

-50
This file was deleted.

tools/generate_requirements.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
"""Generate requirements/*.txt files from pyproject.toml."""
3+
4+
import sys
5+
from pathlib import Path
6+
7+
try: # standard module since Python 3.11
8+
import tomllib as toml
9+
except ImportError:
10+
try: # available for older Python via pip
11+
import tomli as toml
12+
except ImportError:
13+
sys.exit("Please install `tomli` first: `pip install tomli`")
14+
15+
script_pth = Path(__file__)
16+
repo_dir = script_pth.parent.parent
17+
script_relpth = script_pth.relative_to(repo_dir)
18+
header = [
19+
f"# Generated via {script_relpth.as_posix()} and pre-commit hook.",
20+
"# Do not edit this file; modify pyproject.toml instead.",
21+
]
22+
23+
24+
def generate_requirement_file(name: str, req_list: list[str]) -> None:
25+
req_fname = repo_dir / "requirements" / f"{name}.txt"
26+
req_fname.write_text("\n".join(header + req_list) + "\n")
27+
28+
29+
def main() -> None:
30+
pyproject = toml.loads((repo_dir / "pyproject.toml").read_text())
31+
32+
generate_requirement_file("default", pyproject["project"]["dependencies"])
33+
34+
for key, opt_list in pyproject["project"]["optional-dependencies"].items():
35+
generate_requirement_file(key, opt_list)
36+
37+
38+
if __name__ == "__main__":
39+
main()

tools/pyproject.toml.in

-168
This file was deleted.

0 commit comments

Comments
 (0)