|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check that the version is consistent across the repo. |
| 3 | +
|
| 4 | +Fails if metapang.__version__ and the pixi.toml [workspace] version disagree. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +ROOT = Path(__file__).resolve().parent.parent |
| 13 | + |
| 14 | + |
| 15 | +def _read(pattern: str, path: Path) -> str | None: |
| 16 | + match = re.search(pattern, path.read_text(), re.MULTILINE) |
| 17 | + return match.group(1) if match else None |
| 18 | + |
| 19 | + |
| 20 | +def main() -> int: |
| 21 | + parser = argparse.ArgumentParser() |
| 22 | + parser.add_argument("--tag", help="Expected version, e.g. v0.1.0 or 0.1.0") |
| 23 | + parser.add_argument( |
| 24 | + "--pre-push", |
| 25 | + action="store_true", |
| 26 | + help="Read git pre-push refs on stdin and validate any pushed version tags", |
| 27 | + ) |
| 28 | + args = parser.parse_args() |
| 29 | + |
| 30 | + init_v = _read(r'^__version__\s*=\s*"([^"]+)"', ROOT / "metapang" / "__init__.py") |
| 31 | + pixi_v = _read(r'^version\s*=\s*"([^"]+)"', ROOT / "pixi.toml") |
| 32 | + |
| 33 | + if init_v is None or pixi_v is None: |
| 34 | + print("check_version: could not read the version", file=sys.stderr) |
| 35 | + return 1 |
| 36 | + |
| 37 | + if init_v != pixi_v: |
| 38 | + print( |
| 39 | + f"check_version: mismatch between metapang.__version__ ({init_v}) and " |
| 40 | + f"pixi.toml ({pixi_v}).", |
| 41 | + file=sys.stderr, |
| 42 | + ) |
| 43 | + return 1 |
| 44 | + |
| 45 | + tags: list[str] = [] |
| 46 | + if args.tag is not None: |
| 47 | + tags.append(args.tag) |
| 48 | + if args.pre_push: |
| 49 | + for line in sys.stdin: |
| 50 | + local_ref = line.split()[0] if line.split() else "" |
| 51 | + match = re.match(r"^refs/tags/(v?\d+\.\d+\.\d+)$", local_ref) |
| 52 | + if match: |
| 53 | + tags.append(match.group(1)) |
| 54 | + |
| 55 | + for tag in tags: |
| 56 | + if tag.lstrip("v") != init_v: |
| 57 | + print( |
| 58 | + f"check_version: tag {tag} does not match the repo version ({init_v}).", |
| 59 | + file=sys.stderr, |
| 60 | + ) |
| 61 | + return 1 |
| 62 | + |
| 63 | + return 0 |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + sys.exit(main()) |
0 commit comments