|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Compute and commit the next release version. |
| 3 | +
|
| 4 | +Reads the current version from pyproject.toml, computes the next version per |
| 5 | +the requested mode, writes it back, refreshes uv.lock, and commits. The |
| 6 | +computed version is printed to stdout for callers to capture. |
| 7 | +
|
| 8 | +Modes: |
| 9 | + rc — X.Y.ZrcN -> X.Y.Zrc(N+1) |
| 10 | + final — X.Y.0rcN -> X.Y.0 (first final of the minor) |
| 11 | + patch-rc — X.Y.Z -> X.Y.(Z+1)rc0 | X.Y.(Z+1)rcN -> X.Y.(Z+1)rc(N+1) |
| 12 | + patch-final — X.Y.ZrcN (Z>0) -> X.Y.Z (promote patch rc to final) |
| 13 | + dev — X.Y.Z.devN -> X.Y.Z.dev(N+1) (main-only) |
| 14 | +
|
| 15 | +`dev` mode runs on `main` and iterates its .devN counter. All other modes |
| 16 | +run on `release/v*` branches. |
| 17 | +
|
| 18 | +With --dry-run the script prints the proposed version and exits without |
| 19 | +writing or committing. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import argparse |
| 25 | +import re |
| 26 | +import subprocess |
| 27 | +import sys |
| 28 | +import tomllib |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +from packaging.version import Version |
| 32 | + |
| 33 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 34 | +PYPROJECT = REPO_ROOT / "pyproject.toml" |
| 35 | + |
| 36 | + |
| 37 | +def read_current_version() -> Version: |
| 38 | + with PYPROJECT.open("rb") as f: |
| 39 | + data = tomllib.load(f) |
| 40 | + raw = data["project"]["version"] |
| 41 | + return Version(raw) |
| 42 | + |
| 43 | + |
| 44 | +def existing_tags() -> set[str]: |
| 45 | + out = subprocess.run( |
| 46 | + ["git", "tag", "--list", "v*"], |
| 47 | + cwd=REPO_ROOT, |
| 48 | + capture_output=True, |
| 49 | + text=True, |
| 50 | + check=True, |
| 51 | + ) |
| 52 | + return {line.strip() for line in out.stdout.splitlines() if line.strip()} |
| 53 | + |
| 54 | + |
| 55 | +def current_branch() -> str: |
| 56 | + out = subprocess.run( |
| 57 | + ["git", "rev-parse", "--abbrev-ref", "HEAD"], |
| 58 | + cwd=REPO_ROOT, |
| 59 | + capture_output=True, |
| 60 | + text=True, |
| 61 | + check=True, |
| 62 | + ) |
| 63 | + return out.stdout.strip() |
| 64 | + |
| 65 | + |
| 66 | +def compute_next(current: Version, mode: str) -> Version: |
| 67 | + """Compute the next version per mode. Raises ValueError on disallowed transitions.""" |
| 68 | + major, minor, patch = ( |
| 69 | + current.release[0], |
| 70 | + current.release[1], |
| 71 | + (current.release[2] if len(current.release) > 2 else 0), |
| 72 | + ) |
| 73 | + |
| 74 | + if mode == "dev": |
| 75 | + if current.dev is None: |
| 76 | + raise ValueError( |
| 77 | + f"mode=dev requires current version to be a .dev release; got {current}." |
| 78 | + ) |
| 79 | + if current.pre is not None: |
| 80 | + raise ValueError( |
| 81 | + f"mode=dev does not support .devN combined with a pre-release " |
| 82 | + f"segment; got {current}." |
| 83 | + ) |
| 84 | + return Version(f"{major}.{minor}.{patch}.dev{current.dev + 1}") |
| 85 | + |
| 86 | + if current.dev is not None: |
| 87 | + raise ValueError( |
| 88 | + f"Current version {current} is a .dev release; mode {mode!r} only " |
| 89 | + "operates on release branches (rc/final). Ran on the wrong branch?" |
| 90 | + ) |
| 91 | + |
| 92 | + if mode == "rc": |
| 93 | + if current.pre is None or current.pre[0] != "rc": |
| 94 | + raise ValueError( |
| 95 | + f"mode=rc requires current version to be an rc; got {current}. " |
| 96 | + "If this is a final, use mode=patch-rc to start a patch cycle." |
| 97 | + ) |
| 98 | + return Version(f"{major}.{minor}.{patch}rc{current.pre[1] + 1}") |
| 99 | + |
| 100 | + if mode == "final": |
| 101 | + if current.pre is None or current.pre[0] != "rc": |
| 102 | + raise ValueError( |
| 103 | + f"mode=final requires current version to be an rc; got {current}." |
| 104 | + ) |
| 105 | + if patch != 0: |
| 106 | + raise ValueError( |
| 107 | + f"mode=final is for promoting minor rcs (X.Y.0rcN -> X.Y.0); " |
| 108 | + f"got patch version {current}. Use mode=patch-final for patches." |
| 109 | + ) |
| 110 | + return Version(f"{major}.{minor}.{patch}") |
| 111 | + |
| 112 | + if mode == "patch-rc": |
| 113 | + if current.pre is None: |
| 114 | + return Version(f"{major}.{minor}.{patch + 1}rc0") |
| 115 | + if current.pre[0] != "rc": |
| 116 | + raise ValueError(f"Unexpected pre-release segment in {current}") |
| 117 | + if patch == 0: |
| 118 | + raise ValueError( |
| 119 | + f"mode=patch-rc requires an existing final or patch-rc; got " |
| 120 | + f"{current} which is a minor rc. Use mode=rc to iterate minor rcs." |
| 121 | + ) |
| 122 | + return Version(f"{major}.{minor}.{patch}rc{current.pre[1] + 1}") |
| 123 | + |
| 124 | + if mode == "patch-final": |
| 125 | + if current.pre is None or current.pre[0] != "rc": |
| 126 | + raise ValueError( |
| 127 | + f"mode=patch-final requires current to be a patch rc; got {current}." |
| 128 | + ) |
| 129 | + if patch == 0: |
| 130 | + raise ValueError( |
| 131 | + f"mode=patch-final is for patches (Z>0); got {current}. " |
| 132 | + "Use mode=final to promote a minor rc." |
| 133 | + ) |
| 134 | + return Version(f"{major}.{minor}.{patch}") |
| 135 | + |
| 136 | + raise ValueError(f"Unknown mode: {mode!r}") |
| 137 | + |
| 138 | + |
| 139 | +def write_pyproject(new_version: Version) -> None: |
| 140 | + content = PYPROJECT.read_text() |
| 141 | + pattern = re.compile(r'^(version\s*=\s*")[^"]+(")', re.MULTILINE) |
| 142 | + new_content, n = pattern.subn(rf"\g<1>{new_version}\g<2>", content, count=1) |
| 143 | + if n != 1: |
| 144 | + raise RuntimeError("Failed to locate version line in pyproject.toml") |
| 145 | + PYPROJECT.write_text(new_content) |
| 146 | + |
| 147 | + |
| 148 | +def run(cmd: list[str]) -> None: |
| 149 | + subprocess.run(cmd, cwd=REPO_ROOT, check=True) |
| 150 | + |
| 151 | + |
| 152 | +def main() -> int: |
| 153 | + parser = argparse.ArgumentParser(description=__doc__) |
| 154 | + parser.add_argument( |
| 155 | + "--mode", |
| 156 | + required=True, |
| 157 | + choices=["rc", "final", "patch-rc", "patch-final", "dev"], |
| 158 | + ) |
| 159 | + parser.add_argument( |
| 160 | + "--dry-run", |
| 161 | + action="store_true", |
| 162 | + help="Print the proposed next version and exit without writing or committing.", |
| 163 | + ) |
| 164 | + parser.add_argument( |
| 165 | + "--skip-branch-check", |
| 166 | + action="store_true", |
| 167 | + help="Skip the branch assertion. For local testing only.", |
| 168 | + ) |
| 169 | + args = parser.parse_args() |
| 170 | + |
| 171 | + if not args.skip_branch_check: |
| 172 | + branch = current_branch() |
| 173 | + if args.mode == "dev": |
| 174 | + if branch != "main": |
| 175 | + print( |
| 176 | + f"error: mode=dev must run on main; current branch is {branch!r}", |
| 177 | + file=sys.stderr, |
| 178 | + ) |
| 179 | + return 2 |
| 180 | + elif not branch.startswith("release/v"): |
| 181 | + print( |
| 182 | + f"error: mode={args.mode} must run on a release/v* branch; " |
| 183 | + f"current is {branch!r}", |
| 184 | + file=sys.stderr, |
| 185 | + ) |
| 186 | + return 2 |
| 187 | + |
| 188 | + current = read_current_version() |
| 189 | + try: |
| 190 | + next_version = compute_next(current, args.mode) |
| 191 | + except ValueError as e: |
| 192 | + print(f"error: {e}", file=sys.stderr) |
| 193 | + return 2 |
| 194 | + |
| 195 | + tag = f"v{next_version}" |
| 196 | + if tag in existing_tags(): |
| 197 | + print( |
| 198 | + f"error: tag {tag} already exists; refusing to overwrite", file=sys.stderr |
| 199 | + ) |
| 200 | + return 2 |
| 201 | + |
| 202 | + if args.dry_run: |
| 203 | + print(next_version) |
| 204 | + return 0 |
| 205 | + |
| 206 | + write_pyproject(next_version) |
| 207 | + run(["uv", "lock", "--upgrade-package", "mellea"]) |
| 208 | + run(["git", "add", "pyproject.toml", "uv.lock"]) |
| 209 | + run(["git", "commit", "-m", f"release: bump version to {next_version} [skip ci]"]) |
| 210 | + |
| 211 | + print(next_version) |
| 212 | + return 0 |
| 213 | + |
| 214 | + |
| 215 | +if __name__ == "__main__": |
| 216 | + sys.exit(main()) |
0 commit comments