aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/bump_reqs.py
blob: 5d253e719ab2f7ef511b25370dd4feee465dd898 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
"""Bump all pinned versions in requirements*.txt to latest from PyPI."""

import json
import re
import urllib.request
from pathlib import Path

REQS = sorted(Path().glob("requirements*.txt"))
PYPI = "https://pypi.org/pypi/{}/json"


def latest_version(package: str) -> str | None:
    url = PYPI.format(package)
    try:
        with urllib.request.urlopen(url, timeout=10) as resp:
            data = json.load(resp)
            return data["info"]["version"]
    except Exception:
        return None


def bump_file(path: Path):
    lines = path.read_text().splitlines()
    changed = []
    for line in lines:
        m = re.match(r"^([\w-]+(?:\[[\w-]+])?)==[\w.]+$", line)
        if m:
            pkg = m.group(1)
            latest = latest_version(pkg)
            if latest:
                changed.append(f"{pkg}=={latest}")
                if f"{pkg}=={latest}" != line:
                    print(f"  {pkg}: {line.split('==')[1]} -> {latest}")
            else:
                changed.append(line)
                print(f"  {pkg}: failed to fetch, keeping current")
        else:
            changed.append(line)
    path.write_text("\n".join(changed) + "\n")


def main():
    for req in REQS:
        print(f"\n{req}:")
        bump_file(req)


if __name__ == "__main__":
    main()