|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Simple script to bump the package version in setup.py and pyproject.toml.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def get_current_version(content: str) -> str | None: |
| 11 | + """Extract version from file content.""" |
| 12 | + match = re.search(r'version\s*=\s*["\']([^"\']+)["\']', content) |
| 13 | + return match.group(1) if match else None |
| 14 | + |
| 15 | + |
| 16 | +def bump_version(version: str, part: str) -> str: |
| 17 | + """Bump the specified part of the version.""" |
| 18 | + parts = version.split(".") |
| 19 | + if len(parts) != 3: |
| 20 | + raise ValueError(f"Expected semantic version (x.y.z), got: {version}") |
| 21 | + |
| 22 | + major, minor, patch = map(int, parts) |
| 23 | + |
| 24 | + if part == "major": |
| 25 | + major += 1 |
| 26 | + minor = 0 |
| 27 | + patch = 0 |
| 28 | + elif part == "minor": |
| 29 | + minor += 1 |
| 30 | + patch = 0 |
| 31 | + elif part == "patch": |
| 32 | + patch += 1 |
| 33 | + else: |
| 34 | + raise ValueError(f"Unknown version part: {part}") |
| 35 | + |
| 36 | + return f"{major}.{minor}.{patch}" |
| 37 | + |
| 38 | + |
| 39 | +def update_file(filepath: Path, old_version: str, new_version: str) -> bool: |
| 40 | + """Update version in a file. Returns True if file was modified.""" |
| 41 | + if not filepath.exists(): |
| 42 | + return False |
| 43 | + |
| 44 | + content = filepath.read_text() |
| 45 | + new_content = re.sub( |
| 46 | + rf'(version\s*=\s*["\']){re.escape(old_version)}(["\'])', |
| 47 | + rf"\g<1>{new_version}\g<2>", |
| 48 | + content, |
| 49 | + ) |
| 50 | + |
| 51 | + if content != new_content: |
| 52 | + filepath.write_text(new_content) |
| 53 | + return True |
| 54 | + return False |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + parser = argparse.ArgumentParser(description="Bump package version") |
| 59 | + parser.add_argument( |
| 60 | + "part", |
| 61 | + choices=["major", "minor", "patch"], |
| 62 | + nargs="?", |
| 63 | + default="patch", |
| 64 | + help="Version part to bump (default: patch)", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--set", |
| 68 | + dest="set_version", |
| 69 | + metavar="VERSION", |
| 70 | + help="Set a specific version instead of bumping", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--dry-run", |
| 74 | + action="store_true", |
| 75 | + help="Show what would be changed without modifying files", |
| 76 | + ) |
| 77 | + args = parser.parse_args() |
| 78 | + |
| 79 | + root = Path(__file__).parent.parent |
| 80 | + files = [root / "setup.py", root / "pyproject.toml"] |
| 81 | + |
| 82 | + # Get current version from pyproject.toml |
| 83 | + pyproject = root / "pyproject.toml" |
| 84 | + if not pyproject.exists(): |
| 85 | + print("Error: pyproject.toml not found", file=sys.stderr) |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + current_version = get_current_version(pyproject.read_text()) |
| 89 | + if not current_version: |
| 90 | + print("Error: Could not find version in pyproject.toml", file=sys.stderr) |
| 91 | + sys.exit(1) |
| 92 | + |
| 93 | + # Determine new version |
| 94 | + if args.set_version: |
| 95 | + new_version = args.set_version |
| 96 | + else: |
| 97 | + new_version = bump_version(current_version, args.part) |
| 98 | + |
| 99 | + print(f"Version: {current_version} -> {new_version}") |
| 100 | + |
| 101 | + if args.dry_run: |
| 102 | + print("Dry run - no files modified") |
| 103 | + return |
| 104 | + |
| 105 | + # Update files |
| 106 | + for filepath in files: |
| 107 | + if update_file(filepath, current_version, new_version): |
| 108 | + print(f"Updated: {filepath.name}") |
| 109 | + else: |
| 110 | + print(f"Skipped: {filepath.name} (not found or no changes)") |
| 111 | + |
| 112 | + print(f"\nDone! Don't forget to commit and tag: git tag v{new_version}") |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments