|
| 1 | +import re |
| 2 | +import sys |
| 3 | +import argparse |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +SEMVER_REGEX = re.compile( |
| 7 | + r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)" |
| 8 | + r"(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?" |
| 9 | + r"(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def get_current_version(version_file: Path) -> str: |
| 14 | + content = version_file.read_text(encoding="utf-8") |
| 15 | + match = re.search(r'__version__\s*=\s*"(.*)"', content) |
| 16 | + if match: |
| 17 | + return match.group(1) |
| 18 | + raise ValueError(f"Version not found in {version_file}") |
| 19 | + |
| 20 | + |
| 21 | +def update_version_in_src( |
| 22 | + version_file: Path, |
| 23 | + pyproject_file: Path, |
| 24 | + new_version: str, |
| 25 | +): |
| 26 | + content = version_file.read_text(encoding="utf-8") |
| 27 | + new_content = re.sub( |
| 28 | + r'(__version__\s*=\s*").*(")', |
| 29 | + rf'\g<1>{new_version}\g<2>', |
| 30 | + content |
| 31 | + ) |
| 32 | + version_file.write_text(new_content, encoding="utf-8") |
| 33 | + |
| 34 | + # Update pyproject.toml |
| 35 | + content = pyproject_file.read_text(encoding="utf-8") |
| 36 | + new_lines = [] |
| 37 | + for line in content.splitlines(): |
| 38 | + if line.startswith("version"): |
| 39 | + line = f"version = \"{new_version}\"" |
| 40 | + new_lines.append(line) |
| 41 | + |
| 42 | + pyproject_file.write_text("\n".join(new_lines), encoding="utf-8") |
| 43 | + |
| 44 | + |
| 45 | +def bump_to_dev_version(version: str) -> str: |
| 46 | + version_parts = SEMVER_REGEX.match(version).groups() |
| 47 | + major, minor, patch = version_parts[0:3] |
| 48 | + patch = str(int(patch) + 1) |
| 49 | + return f"{major}.{minor}.{patch}-dev" |
| 50 | + |
| 51 | + |
| 52 | +def bump_to_release_version(version: str, bump_minor: bool = False) -> str: |
| 53 | + version_parts = SEMVER_REGEX.match(version).groups() |
| 54 | + major, minor, patch = version_parts[0:3] |
| 55 | + if bump_minor: |
| 56 | + minor = str(int(minor) + 1) |
| 57 | + patch = "0" |
| 58 | + return f"{major}.{minor}.{patch}" |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + parser = argparse.ArgumentParser() |
| 63 | + parser.add_argument( |
| 64 | + "command", |
| 65 | + choices=["get-release-version", "update", "get-dev-version"] |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--version", |
| 69 | + help="Version to set for update command", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--bump-minor", |
| 73 | + action="store_true", |
| 74 | + help="Bump minor version for get-release-version command", |
| 75 | + ) |
| 76 | + args = parser.parse_args() |
| 77 | + |
| 78 | + repo_root = Path(__file__).parent |
| 79 | + version_file = repo_root / "ayon_api" / "version.py" |
| 80 | + pyproject_file = repo_root / "pyproject.toml" |
| 81 | + |
| 82 | + current_version = get_current_version(version_file) |
| 83 | + |
| 84 | + if args.command == "get-release-version": |
| 85 | + if current_version: |
| 86 | + print(bump_to_release_version(current_version, args.bump_minor)) |
| 87 | + else: |
| 88 | + sys.exit(1) |
| 89 | + elif args.command == "get-dev-version": |
| 90 | + if current_version: |
| 91 | + print(bump_to_dev_version(current_version)) |
| 92 | + else: |
| 93 | + sys.exit(1) |
| 94 | + elif args.command == "update": |
| 95 | + if not args.version: |
| 96 | + print("Error: --version is required for update command") |
| 97 | + sys.exit(1) |
| 98 | + update_version_in_src(version_file, pyproject_file, args.version) |
| 99 | + print(f"Updated version to {args.version}") |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments