|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify codetracer recorder version parity across Python and Rust manifests.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +try: # Python 3.11+ |
| 10 | + import tomllib # type: ignore[attr-defined] |
| 11 | +except ModuleNotFoundError: # pragma: no cover - safety net for older interpreters |
| 12 | + tomllib = None # type: ignore[assignment] |
| 13 | + |
| 14 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 15 | +PYPROJECT = REPO_ROOT / "codetracer-python-recorder" / "pyproject.toml" |
| 16 | +CARGO = REPO_ROOT / "codetracer-python-recorder" / "Cargo.toml" |
| 17 | + |
| 18 | + |
| 19 | +def _read_version(path: Path, section: str) -> str: |
| 20 | + text = path.read_text(encoding="utf-8") |
| 21 | + if tomllib is not None: |
| 22 | + data = tomllib.loads(text) |
| 23 | + section_data = data.get(section) |
| 24 | + if not isinstance(section_data, dict): |
| 25 | + raise KeyError(f"Missing section [{section}] in {path}") |
| 26 | + version = section_data.get("version") |
| 27 | + if not isinstance(version, str): |
| 28 | + raise KeyError(f"Missing 'version' in [{section}] of {path}") |
| 29 | + return version |
| 30 | + |
| 31 | + # Minimal parser fallback for environments without tomllib/tomli. |
| 32 | + target_header = f"[{section}]" |
| 33 | + in_section = False |
| 34 | + for raw_line in text.splitlines(): |
| 35 | + line = raw_line.strip() |
| 36 | + if not line or line.startswith("#"): |
| 37 | + continue |
| 38 | + if line.startswith("[") and line.endswith("]"): |
| 39 | + in_section = line == target_header |
| 40 | + continue |
| 41 | + if in_section and line.startswith("version"): |
| 42 | + _, _, value = line.partition("=") |
| 43 | + version = value.strip().strip('"') |
| 44 | + if version: |
| 45 | + return version |
| 46 | + raise KeyError(f"Could not locate version in [{section}] of {path}") |
| 47 | + |
| 48 | + |
| 49 | +def main() -> int: |
| 50 | + python_version = _read_version(PYPROJECT, "project") |
| 51 | + rust_version = _read_version(CARGO, "package") |
| 52 | + if python_version != rust_version: |
| 53 | + sys.stderr.write( |
| 54 | + "Version mismatch detected:\n" |
| 55 | + f" pyproject.toml -> {python_version}\n" |
| 56 | + f" Cargo.toml -> {rust_version}\n" |
| 57 | + ) |
| 58 | + return 1 |
| 59 | + return 0 |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + raise SystemExit(main()) |
0 commit comments