|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Bump version in package.xml and conda.recipe/recipe.yaml, then commit.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +REPO = Path(__file__).resolve().parent |
| 11 | + |
| 12 | +VERSION_FILES = { |
| 13 | + REPO / "package.xml": ( |
| 14 | + re.compile(r"(<version>)([\d.]+)(</version>)"), |
| 15 | + r"\g<1>{version}\g<3>", |
| 16 | + ), |
| 17 | + REPO / "conda.recipe/recipe.yaml": ( |
| 18 | + re.compile(r'(version:\s*")[\d.]+(")'), |
| 19 | + r'\g<1>{version}\2', |
| 20 | + ), |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +def current_version() -> str: |
| 25 | + text = (REPO / "package.xml").read_text() |
| 26 | + m = re.search(r"<version>([\d.]+)</version>", text) |
| 27 | + if not m: |
| 28 | + sys.exit("Cannot read current version from package.xml") |
| 29 | + return m.group(1) |
| 30 | + |
| 31 | + |
| 32 | +def update_file(path: Path, pattern: re.Pattern, replacement: str, version: str): |
| 33 | + text = path.read_text() |
| 34 | + new_text, n = pattern.subn(replacement.format(version=version), text) |
| 35 | + if n == 0: |
| 36 | + sys.exit(f"Version pattern not found in {path.relative_to(REPO)}") |
| 37 | + path.write_text(new_text) |
| 38 | + print(f" {path.relative_to(REPO)}: updated to {version}") |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + cur = current_version() |
| 43 | + parser = argparse.ArgumentParser(description="Bump project version and commit.") |
| 44 | + parser.add_argument("version", help=f"New version (current: {cur})") |
| 45 | + parser.add_argument( |
| 46 | + "--no-commit", action="store_true", help="Update files without committing" |
| 47 | + ) |
| 48 | + args = parser.parse_args() |
| 49 | + |
| 50 | + new = args.version |
| 51 | + if new == cur: |
| 52 | + sys.exit(f"Version is already {cur}") |
| 53 | + |
| 54 | + print(f"Bumping {cur} → {new}") |
| 55 | + for path, (pattern, repl) in VERSION_FILES.items(): |
| 56 | + update_file(path, pattern, repl, new) |
| 57 | + |
| 58 | + if args.no_commit: |
| 59 | + return |
| 60 | + |
| 61 | + files = [str(p) for p in VERSION_FILES] |
| 62 | + subprocess.run(["git", "add", *files], check=True, cwd=REPO) |
| 63 | + subprocess.run( |
| 64 | + ["git", "commit", "-m", new], |
| 65 | + check=True, |
| 66 | + cwd=REPO, |
| 67 | + ) |
| 68 | + print(f"\nCommitted as '{new}'. Review with: git log --oneline -1") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments