|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Sync oddish/src/oddish/harbor-pin.toml into both pyproject harbor pins. |
| 3 | +
|
| 4 | +The TOML file is the single source of truth. Both oddish/pyproject.toml and |
| 5 | +backend/pyproject.toml duplicate the pin because uv cannot inherit |
| 6 | +[tool.uv.sources] across the backend worker image boundary. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import sys |
| 13 | +import tomllib |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import tomlkit |
| 17 | + |
| 18 | +_ODDISH_ROOT = Path(__file__).resolve().parents[1] |
| 19 | +_REPO_ROOT = _ODDISH_ROOT.parent |
| 20 | +_PIN_FILE = _ODDISH_ROOT / "src/oddish/harbor-pin.toml" |
| 21 | +_TARGETS = ( |
| 22 | + _ODDISH_ROOT / "pyproject.toml", |
| 23 | + _REPO_ROOT / "backend" / "pyproject.toml", |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _load_pin() -> dict[str, str]: |
| 28 | + with _PIN_FILE.open("rb") as fh: |
| 29 | + raw = tomllib.load(fh) |
| 30 | + git = raw.get("git") |
| 31 | + rev = raw.get("rev") |
| 32 | + if not isinstance(git, str) or not isinstance(rev, str): |
| 33 | + raise SystemExit(f"invalid harbor pin in {_PIN_FILE}") |
| 34 | + return {"git": git, "rev": rev} |
| 35 | + |
| 36 | + |
| 37 | +def _sync_file(path: Path, pin: dict[str, str], *, check: bool) -> bool: |
| 38 | + text = path.read_text(encoding="utf-8") |
| 39 | + doc = tomlkit.parse(text) |
| 40 | + sources = doc.get("tool", {}).get("uv", {}).get("sources") |
| 41 | + if not isinstance(sources, dict) or "harbor" not in sources: |
| 42 | + raise SystemExit(f"missing [tool.uv.sources].harbor in {path}") |
| 43 | + harbor = sources["harbor"] |
| 44 | + if not isinstance(harbor, dict): |
| 45 | + raise SystemExit(f"invalid [tool.uv.sources].harbor in {path}") |
| 46 | + |
| 47 | + changed = harbor.get("git") != pin["git"] or harbor.get("rev") != pin["rev"] |
| 48 | + if check: |
| 49 | + return changed |
| 50 | + |
| 51 | + harbor["git"] = pin["git"] |
| 52 | + harbor["rev"] = pin["rev"] |
| 53 | + path.write_text(tomlkit.dumps(doc), encoding="utf-8") |
| 54 | + return changed |
| 55 | + |
| 56 | + |
| 57 | +def main() -> int: |
| 58 | + parser = argparse.ArgumentParser(description=__doc__) |
| 59 | + parser.add_argument( |
| 60 | + "--check", |
| 61 | + action="store_true", |
| 62 | + help="exit 1 when any pyproject pin drifts from harbor-pin.toml", |
| 63 | + ) |
| 64 | + args = parser.parse_args() |
| 65 | + |
| 66 | + pin = _load_pin() |
| 67 | + drifted: list[Path] = [] |
| 68 | + for target in _TARGETS: |
| 69 | + if _sync_file(target, pin, check=args.check): |
| 70 | + drifted.append(target) |
| 71 | + |
| 72 | + if args.check: |
| 73 | + if drifted: |
| 74 | + rel = ", ".join(str(p.relative_to(_REPO_ROOT)) for p in drifted) |
| 75 | + print( |
| 76 | + "harbor pin drift: run `cd oddish && uv run python scripts/sync_harbor_pin.py` " |
| 77 | + f"to sync {rel} with src/oddish/harbor-pin.toml", |
| 78 | + file=sys.stderr, |
| 79 | + ) |
| 80 | + return 1 |
| 81 | + print("harbor pin sync: OK") |
| 82 | + return 0 |
| 83 | + |
| 84 | + if drifted: |
| 85 | + rel = ", ".join(str(p.relative_to(_REPO_ROOT)) for p in drifted) |
| 86 | + print(f"updated harbor pin in {rel}") |
| 87 | + else: |
| 88 | + print("harbor pin already in sync") |
| 89 | + return 0 |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + raise SystemExit(main()) |
0 commit comments