Skip to content

Commit 963339d

Browse files
committed
MNT: Add update_requirements tool to copy from setup.cfg
1 parent bf9cd97 commit 963339d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tools/update_requirements.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
from copy import copy
4+
from configparser import ConfigParser
5+
from pathlib import Path
6+
from packaging.requirements import Requirement, SpecifierSet
7+
8+
repo_root = Path(__file__).parent.parent
9+
setup_cfg = repo_root / "setup.cfg"
10+
reqs = repo_root / "requirements.txt"
11+
min_reqs = repo_root / "min-requirements.txt"
12+
13+
config = ConfigParser()
14+
config.read(setup_cfg)
15+
requirements = [Requirement(req)
16+
for req in config.get("options", "install_requires").strip().splitlines()]
17+
18+
script_name = Path(__file__).relative_to(repo_root)
19+
20+
def to_min(req):
21+
if req.specifier:
22+
req = copy(req)
23+
min_spec = [spec for spec in req.specifier if spec.operator in ('>=', '~=')][0]
24+
min_spec._spec = ('==', ) + min_spec._spec[1:]
25+
req.specifier = SpecifierSet(str(min_spec))
26+
return req
27+
28+
lines = [f"# Auto-generated by {script_name}", ""]
29+
30+
# Write requirements
31+
lines[1:-1] = [str(req) for req in requirements]
32+
reqs.write_text("\n".join(lines))
33+
34+
# Write minimum requirements
35+
lines[1:-1] = [str(to_min(req)) for req in requirements]
36+
min_reqs.write_text("\n".join(lines))

0 commit comments

Comments
 (0)