Skip to content

Commit

Permalink
MNT: Add update_requirements tool to copy from setup.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Mar 30, 2020
1 parent bf9cd97 commit 963339d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tools/update_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
import sys
from copy import copy
from configparser import ConfigParser
from pathlib import Path
from packaging.requirements import Requirement, SpecifierSet

repo_root = Path(__file__).parent.parent
setup_cfg = repo_root / "setup.cfg"
reqs = repo_root / "requirements.txt"
min_reqs = repo_root / "min-requirements.txt"

config = ConfigParser()
config.read(setup_cfg)
requirements = [Requirement(req)
for req in config.get("options", "install_requires").strip().splitlines()]

script_name = Path(__file__).relative_to(repo_root)

def to_min(req):
if req.specifier:
req = copy(req)
min_spec = [spec for spec in req.specifier if spec.operator in ('>=', '~=')][0]
min_spec._spec = ('==', ) + min_spec._spec[1:]
req.specifier = SpecifierSet(str(min_spec))
return req

lines = [f"# Auto-generated by {script_name}", ""]

# Write requirements
lines[1:-1] = [str(req) for req in requirements]
reqs.write_text("\n".join(lines))

# Write minimum requirements
lines[1:-1] = [str(to_min(req)) for req in requirements]
min_reqs.write_text("\n".join(lines))

0 comments on commit 963339d

Please sign in to comment.