|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check that version numbers are consistent across its definitions.""" |
| 3 | + |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +def normalize_version(version: str) -> str: |
| 10 | + """Normalize version strings to handle different pre-release conventions.""" |
| 11 | + version = version.strip().strip('"').strip("'") |
| 12 | + version = re.sub(r"\+.*$", "", version) |
| 13 | + version = re.sub(r"-rc\.", "rc", version) |
| 14 | + version = re.sub(r"\.rc\.", "rc", version) |
| 15 | + version = re.sub(r"-pre\.", "rc", version) |
| 16 | + version = re.sub(r"\.pre\.", "rc", version) |
| 17 | + version = re.sub(r"-rc", "rc", version) |
| 18 | + version = re.sub(r"-pre", "rc", version) |
| 19 | + return version |
| 20 | + |
| 21 | + |
| 22 | +def extract_version(path: Path, pattern: str) -> str: |
| 23 | + """Extract version from file.""" |
| 24 | + content = path.read_text() |
| 25 | + match = re.search(pattern, content, re.MULTILINE) |
| 26 | + if not match: |
| 27 | + raise ValueError(f"Could not find version in {path}") |
| 28 | + return match.group(1) |
| 29 | + |
| 30 | + |
| 31 | +def main() -> int: |
| 32 | + """Main entry point.""" |
| 33 | + root = Path(__file__).parent.parent |
| 34 | + |
| 35 | + files = { |
| 36 | + "Cargo.toml": (root / "Cargo.toml", r'^version\s*=\s*["\']([^"\']+)["\']'), |
| 37 | + "pyproject.toml": (root / "pyproject.toml", r'^version\s*=\s*["\']([^"\']+)["\']'), |
| 38 | + "__init__.py": (root / "python" / "evalica" / "__init__.py", r'__version__\s*=\s*["\']([^"\']+)["\']'), |
| 39 | + } |
| 40 | + |
| 41 | + versions = {} |
| 42 | + for name, (path, pattern) in files.items(): |
| 43 | + if not path.exists(): |
| 44 | + print(f"Error: {path} not found", file=sys.stderr) |
| 45 | + return 1 |
| 46 | + versions[name] = normalize_version(extract_version(path, pattern)) |
| 47 | + |
| 48 | + for name, version in versions.items(): |
| 49 | + print(f"{name}: {version}") |
| 50 | + |
| 51 | + if len(set(versions.values())) != 1: |
| 52 | + return 1 |
| 53 | + |
| 54 | + return 0 |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + sys.exit(main()) |
0 commit comments