|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Clone all PathSim package repositories declared in lib.config.PACKAGES. |
| 3 | +
|
| 4 | +Idempotent: skips clone when target directory already exists. |
| 5 | +Required repos that fail to clone produce a non-zero exit; optional repos |
| 6 | +(``required: False``) only emit a warning. Tags are fetched after each clone |
| 7 | +and the latest tag is logged. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import subprocess |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +SCRIPT_DIR = Path(__file__).resolve().parent |
| 17 | +sys.path.insert(0, str(SCRIPT_DIR)) |
| 18 | + |
| 19 | +from lib.config import PACKAGES, ROOT_DIR # noqa: E402 |
| 20 | + |
| 21 | + |
| 22 | +def clone(github_repo: str, dest: Path, required: bool) -> bool: |
| 23 | + if dest.exists(): |
| 24 | + print(f"✓ {dest.name}: already present") |
| 25 | + return True |
| 26 | + url = f"https://github.com/{github_repo}.git" |
| 27 | + print(f" Cloning {github_repo} → {dest} ...") |
| 28 | + result = subprocess.run(["git", "clone", url, str(dest)]) |
| 29 | + if result.returncode == 0: |
| 30 | + return True |
| 31 | + if required: |
| 32 | + print(f"✗ Failed to clone {github_repo} (required)") |
| 33 | + return False |
| 34 | + print(f"⊘ Skipping {github_repo} (not available)") |
| 35 | + return True |
| 36 | + |
| 37 | + |
| 38 | +def fetch_tags(dest: Path) -> None: |
| 39 | + if not dest.exists(): |
| 40 | + return |
| 41 | + subprocess.run(["git", "-C", str(dest), "fetch", "--tags"], check=False) |
| 42 | + result = subprocess.run( |
| 43 | + ["git", "-C", str(dest), "describe", "--tags", "--abbrev=0"], |
| 44 | + capture_output=True, text=True, |
| 45 | + ) |
| 46 | + latest = result.stdout.strip() if result.returncode == 0 and result.stdout.strip() else "no tags" |
| 47 | + print(f" {dest.name}: latest_tag={latest}") |
| 48 | + |
| 49 | + |
| 50 | +def main() -> int: |
| 51 | + failures: list[str] = [] |
| 52 | + for pkg_id, pkg in PACKAGES.items(): |
| 53 | + github_repo = pkg["github_repo"] |
| 54 | + required = pkg.get("required", True) |
| 55 | + dest = ROOT_DIR / github_repo.split("/")[-1] |
| 56 | + if not clone(github_repo, dest, required): |
| 57 | + failures.append(github_repo) |
| 58 | + continue |
| 59 | + fetch_tags(dest) |
| 60 | + |
| 61 | + if failures: |
| 62 | + print("\nRequired repos failed to clone:") |
| 63 | + for repo in failures: |
| 64 | + print(f" - {repo}") |
| 65 | + return 1 |
| 66 | + return 0 |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + sys.exit(main()) |
0 commit comments