|
| 1 | +"""Rewrite >= lower bounds for a workspace package across pyproject files. |
| 2 | +
|
| 3 | +Used by the release workflow after ``uv version`` so root extras (and |
| 4 | +downstream lib pins) stay aligned with the member version that |
| 5 | +``check_extra_pins PACKAGE VERSION`` enforces. |
| 6 | +""" |
| 7 | + |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from packaging.utils import canonicalize_name |
| 13 | +from packaging.version import InvalidVersion, Version |
| 14 | + |
| 15 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 16 | + |
| 17 | +# PEP 508 name + optional extras + >= lower bound. Trailing upper bounds, |
| 18 | +# markers, and comments are left untouched. |
| 19 | +_PIN_RE = re.compile( |
| 20 | + r"(?P<name>[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?)" |
| 21 | + r"(?P<extra>\[[^\]]+\])?" |
| 22 | + r"(?P<op>\s*>=\s*)" |
| 23 | + r"(?P<version>[^,<\"'\s]+)" |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def rewrite_text(text: str, package: str, new_version: str) -> tuple[str, int]: |
| 28 | + """Replace ``>=`` pins for ``package`` in a pyproject document. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + text : str |
| 33 | + Raw ``pyproject.toml`` contents. |
| 34 | + package : str |
| 35 | + Distribution name to update (hyphen or underscore form). |
| 36 | + new_version : str |
| 37 | + Replacement lower bound (PEP 440). |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + tuple[str, int] |
| 42 | + Rewritten text and the number of pins that named ``package``. |
| 43 | + """ |
| 44 | + target = canonicalize_name(package) |
| 45 | + count = 0 |
| 46 | + |
| 47 | + def repl(match: re.Match[str]) -> str: |
| 48 | + nonlocal count |
| 49 | + if canonicalize_name(match.group("name")) != target: |
| 50 | + return match.group(0) |
| 51 | + count += 1 |
| 52 | + extra = match.group("extra") or "" |
| 53 | + return f"{match.group('name')}{extra}{match.group('op')}{new_version}" |
| 54 | + |
| 55 | + return _PIN_RE.sub(repl, text), count |
| 56 | + |
| 57 | + |
| 58 | +def pyproject_paths(repo_root: Path) -> list[Path]: |
| 59 | + """Return root and ``libs/*/pyproject.toml`` paths that exist.""" |
| 60 | + paths = [repo_root / "pyproject.toml"] |
| 61 | + libs = repo_root / "libs" |
| 62 | + if libs.is_dir(): |
| 63 | + paths.extend(sorted(libs.glob("*/pyproject.toml"))) |
| 64 | + return [path for path in paths if path.is_file()] |
| 65 | + |
| 66 | + |
| 67 | +def bump_pins( |
| 68 | + repo_root: Path, package: str, new_version: str |
| 69 | +) -> list[tuple[Path, int]]: |
| 70 | + """Write updated pins for ``package`` under ``repo_root``. |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + repo_root : Path |
| 75 | + Monorepo root containing ``pyproject.toml`` and ``libs/``. |
| 76 | + package : str |
| 77 | + Distribution name whose ``>=`` pins should move. |
| 78 | + new_version : str |
| 79 | + Replacement lower bound (PEP 440). |
| 80 | +
|
| 81 | + Returns |
| 82 | + ------- |
| 83 | + list[tuple[Path, int]] |
| 84 | + Files that changed, with the pin count in each file. |
| 85 | + """ |
| 86 | + try: |
| 87 | + Version(new_version) |
| 88 | + except InvalidVersion as exc: |
| 89 | + raise SystemExit(f"invalid version {new_version!r}: {exc}") from exc |
| 90 | + |
| 91 | + results: list[tuple[Path, int]] = [] |
| 92 | + for path in pyproject_paths(repo_root): |
| 93 | + original = path.read_text(encoding="utf-8") |
| 94 | + updated, count = rewrite_text(original, package, new_version) |
| 95 | + if updated == original: |
| 96 | + continue |
| 97 | + path.write_text(updated, encoding="utf-8") |
| 98 | + results.append((path, count)) |
| 99 | + return results |
| 100 | + |
| 101 | + |
| 102 | +def main(argv: list[str] | None = None) -> int: |
| 103 | + """Rewrite pins for ``PACKAGE VERSION``. Returns a process exit code.""" |
| 104 | + args = sys.argv[1:] if argv is None else argv |
| 105 | + if len(args) != 2: |
| 106 | + print("usage: bump_workspace_pins.py PACKAGE VERSION", file=sys.stderr) |
| 107 | + return 2 |
| 108 | + |
| 109 | + package, version = args |
| 110 | + results = bump_pins(REPO_ROOT, package, version) |
| 111 | + if not results: |
| 112 | + print(f"No {package} pins to update.") |
| 113 | + return 0 |
| 114 | + for path, count in results: |
| 115 | + print(f"Updated {count} pin(s) in {path.relative_to(REPO_ROOT)}") |
| 116 | + return 0 |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + raise SystemExit(main()) |
0 commit comments