|
| 1 | +"""Rewrite the workspace version literal across pyproject, source, tests, and scripts. |
| 2 | +
|
| 3 | +The workspace keeps its version duplicated in several places: |
| 4 | +
|
| 5 | +- ``pyproject.toml`` (root + every publishable package) |
| 6 | +- ``__version__`` / ``_EXTENSION_VERSION`` constants in package ``__init__.py`` |
| 7 | +- Sphinx ``setup()`` return-dict ``"version"`` keys |
| 8 | +- ``tests/test_package_tools.py`` assertions |
| 9 | +- ``smoke_gp_sphinx`` template in ``scripts/ci/package_tools.py`` |
| 10 | +
|
| 11 | +``scripts/ci/package_tools.py check-versions`` catches drift between the |
| 12 | +pyproject version and whatever the runtime source says, so any literal missed |
| 13 | +by this bump will surface immediately after ``uv lock``. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import argparse |
| 19 | +import pathlib |
| 20 | +import sys |
| 21 | +import typing as t |
| 22 | + |
| 23 | +if sys.version_info >= (3, 11): |
| 24 | + import tomllib |
| 25 | +else: |
| 26 | + import tomli as tomllib # type: ignore[import-not-found] |
| 27 | + |
| 28 | +try: |
| 29 | + from packaging.version import InvalidVersion, Version |
| 30 | +except ImportError: # pragma: no cover - packaging is a runtime dep via uv |
| 31 | + InvalidVersion = ValueError # type: ignore[assignment,misc] |
| 32 | + Version = None # type: ignore[assignment,misc] |
| 33 | + |
| 34 | + |
| 35 | +#: Glob patterns (relative to the workspace root) that may contain version |
| 36 | +#: literals. Order is informational only; rewrites are idempotent. |
| 37 | +BUMP_GLOBS: t.Final[tuple[str, ...]] = ( |
| 38 | + "pyproject.toml", |
| 39 | + "packages/*/pyproject.toml", |
| 40 | + "packages/*/src/**/*.py", |
| 41 | + "tests/**/*.py", |
| 42 | + "scripts/**/*.py", |
| 43 | +) |
| 44 | + |
| 45 | +#: Path fragments to skip even if a glob matches them. |
| 46 | +EXCLUDE_FRAGMENTS: t.Final[tuple[str, ...]] = ( |
| 47 | + ".venv/", |
| 48 | + "/build/", |
| 49 | + "/dist/", |
| 50 | + "/.git/", |
| 51 | + "__pycache__/", |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +def _workspace_root() -> pathlib.Path: |
| 56 | + """Return the repository root.""" |
| 57 | + return pathlib.Path(__file__).resolve().parents[2] |
| 58 | + |
| 59 | + |
| 60 | +def _read_root_version(root: pathlib.Path) -> str: |
| 61 | + """Return the root ``pyproject.toml`` version string. |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + root : pathlib.Path |
| 66 | + Repository root containing the root ``pyproject.toml``. |
| 67 | +
|
| 68 | + Returns |
| 69 | + ------- |
| 70 | + str |
| 71 | + Version string for the workspace root package. |
| 72 | + """ |
| 73 | + with (root / "pyproject.toml").open("rb") as handle: |
| 74 | + data = tomllib.load(handle) |
| 75 | + return t.cast("str", data["project"]["version"]) |
| 76 | + |
| 77 | + |
| 78 | +def _validate_new_version(new_version: str, old_version: str) -> None: |
| 79 | + """Validate that ``new_version`` is PEP 440 and not the same as ``old_version``. |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + new_version : str |
| 84 | + Proposed new version. |
| 85 | + old_version : str |
| 86 | + Current workspace version. |
| 87 | + """ |
| 88 | + if new_version == old_version: |
| 89 | + message = f"new version {new_version!r} equals current version" |
| 90 | + raise SystemExit(message) |
| 91 | + if Version is not None: |
| 92 | + try: |
| 93 | + Version(new_version) |
| 94 | + except InvalidVersion as exc: |
| 95 | + message = f"invalid PEP 440 version {new_version!r}: {exc}" |
| 96 | + raise SystemExit(message) from exc |
| 97 | + |
| 98 | + |
| 99 | +def _iter_candidate_files(root: pathlib.Path) -> t.Iterator[pathlib.Path]: |
| 100 | + """Yield files matching any :data:`BUMP_GLOBS` pattern, deduplicated.""" |
| 101 | + seen: set[pathlib.Path] = set() |
| 102 | + for pattern in BUMP_GLOBS: |
| 103 | + for path in sorted(root.glob(pattern)): |
| 104 | + if not path.is_file(): |
| 105 | + continue |
| 106 | + resolved = path.resolve() |
| 107 | + if resolved in seen: |
| 108 | + continue |
| 109 | + as_posix = resolved.as_posix() |
| 110 | + if any(fragment in as_posix for fragment in EXCLUDE_FRAGMENTS): |
| 111 | + continue |
| 112 | + seen.add(resolved) |
| 113 | + yield path |
| 114 | + |
| 115 | + |
| 116 | +def _rewrite_file( |
| 117 | + path: pathlib.Path, |
| 118 | + old_version: str, |
| 119 | + new_version: str, |
| 120 | +) -> int: |
| 121 | + """Rewrite ``old_version`` -> ``new_version`` in ``path``; return replacement count. |
| 122 | +
|
| 123 | + Parameters |
| 124 | + ---------- |
| 125 | + path : pathlib.Path |
| 126 | + File to rewrite. |
| 127 | + old_version : str |
| 128 | + Literal to replace. Matched verbatim; no regex. |
| 129 | + new_version : str |
| 130 | + Replacement literal. |
| 131 | +
|
| 132 | + Returns |
| 133 | + ------- |
| 134 | + int |
| 135 | + Number of occurrences replaced. Zero if the file did not change. |
| 136 | + """ |
| 137 | + original = path.read_text() |
| 138 | + if old_version not in original: |
| 139 | + return 0 |
| 140 | + updated = original.replace(old_version, new_version) |
| 141 | + replacements = original.count(old_version) |
| 142 | + path.write_text(updated) |
| 143 | + return replacements |
| 144 | + |
| 145 | + |
| 146 | +def bump_workspace_version( |
| 147 | + new_version: str, |
| 148 | + *, |
| 149 | + root: pathlib.Path | None = None, |
| 150 | +) -> list[tuple[pathlib.Path, int]]: |
| 151 | + """Rewrite every workspace version literal to ``new_version``. |
| 152 | +
|
| 153 | + Parameters |
| 154 | + ---------- |
| 155 | + new_version : str |
| 156 | + Target version, validated as PEP 440. |
| 157 | + root : pathlib.Path | None |
| 158 | + Repository root. Defaults to the script's enclosing workspace. |
| 159 | +
|
| 160 | + Returns |
| 161 | + ------- |
| 162 | + list[tuple[pathlib.Path, int]] |
| 163 | + (path, replacement_count) pairs for every file touched. |
| 164 | + """ |
| 165 | + workspace_root = root if root is not None else _workspace_root() |
| 166 | + old_version = _read_root_version(workspace_root) |
| 167 | + _validate_new_version(new_version, old_version) |
| 168 | + |
| 169 | + changes: list[tuple[pathlib.Path, int]] = [] |
| 170 | + for path in _iter_candidate_files(workspace_root): |
| 171 | + count = _rewrite_file(path, old_version, new_version) |
| 172 | + if count: |
| 173 | + changes.append((path, count)) |
| 174 | + return changes |
| 175 | + |
| 176 | + |
| 177 | +def _build_parser() -> argparse.ArgumentParser: |
| 178 | + """Build the CLI argument parser.""" |
| 179 | + parser = argparse.ArgumentParser( |
| 180 | + description=( |
| 181 | + "Rewrite the shared workspace version across every " |
| 182 | + "pyproject.toml, __init__.py, and test file." |
| 183 | + ), |
| 184 | + ) |
| 185 | + parser.add_argument("new_version", help="Target version (PEP 440)") |
| 186 | + return parser |
| 187 | + |
| 188 | + |
| 189 | +def main(argv: t.Sequence[str] | None = None) -> int: |
| 190 | + """CLI entry point.""" |
| 191 | + args = _build_parser().parse_args(argv) |
| 192 | + root = _workspace_root() |
| 193 | + old_version = _read_root_version(root) |
| 194 | + changes = bump_workspace_version(args.new_version, root=root) |
| 195 | + |
| 196 | + total_replacements = sum(count for _, count in changes) |
| 197 | + print(f" {old_version} -> {args.new_version}") |
| 198 | + for path, count in changes: |
| 199 | + try: |
| 200 | + rel = path.relative_to(root) |
| 201 | + except ValueError: |
| 202 | + rel = path |
| 203 | + print(f" {rel} ({count})") |
| 204 | + print( |
| 205 | + f" {len(changes)} file(s) changed, {total_replacements} replacement(s)", |
| 206 | + ) |
| 207 | + return 0 |
| 208 | + |
| 209 | + |
| 210 | +if __name__ == "__main__": |
| 211 | + raise SystemExit(main()) |
0 commit comments