88 lines
2.9 KiB
Python
Executable File
88 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Clone the independently versioned Echoion component repositories."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
def run(*args: str, cwd: Path | None = None) -> None:
|
|
subprocess.run(args, cwd=cwd, check=True)
|
|
|
|
|
|
def output(*args: str, cwd: Path) -> str:
|
|
return subprocess.check_output(args, cwd=cwd, text=True).strip()
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
"--update",
|
|
action="store_true",
|
|
help="fetch and check out locked revisions in existing clean repositories",
|
|
)
|
|
parser.add_argument(
|
|
"--record",
|
|
action="store_true",
|
|
help="record the current clean component HEADs in components.lock.json",
|
|
)
|
|
args = parser.parse_args()
|
|
if args.update and args.record:
|
|
parser.error("--update and --record are mutually exclusive")
|
|
|
|
root = Path(__file__).resolve().parents[1]
|
|
lock_path = root / "components.lock.json"
|
|
lock = json.loads(lock_path.read_text())
|
|
if lock.get("schemaVersion") != 1:
|
|
raise SystemExit("unsupported components.lock.json schema")
|
|
|
|
if args.record:
|
|
for name, component in lock["components"].items():
|
|
destination = root / name
|
|
if not (destination / ".git").exists():
|
|
raise SystemExit(f"component repository is missing: {destination}")
|
|
if output("git", "status", "--porcelain", cwd=destination):
|
|
raise SystemExit(
|
|
f"refusing to record dirty repository: {destination}"
|
|
)
|
|
component["revision"] = output(
|
|
"git", "rev-parse", "HEAD", cwd=destination
|
|
)
|
|
temporary = lock_path.with_suffix(".json.tmp")
|
|
temporary.write_text(json.dumps(lock, indent=2) + "\n")
|
|
temporary.replace(lock_path)
|
|
return 0
|
|
|
|
for name, component in lock["components"].items():
|
|
destination = root / name
|
|
revision = component["revision"]
|
|
if not destination.exists():
|
|
run(
|
|
"git",
|
|
"clone",
|
|
"--no-checkout",
|
|
component["url"],
|
|
str(destination),
|
|
)
|
|
run("git", "fetch", "--depth=1", "origin", revision, cwd=destination)
|
|
run("git", "checkout", "--detach", "FETCH_HEAD", cwd=destination)
|
|
continue
|
|
|
|
if not (destination / ".git").exists():
|
|
raise SystemExit(f"{destination} exists but is not a Git repository")
|
|
if not args.update:
|
|
continue
|
|
if output("git", "status", "--porcelain", cwd=destination):
|
|
raise SystemExit(f"refusing to update dirty repository: {destination}")
|
|
run("git", "fetch", "origin", revision, cwd=destination)
|
|
run("git", "checkout", "--detach", revision, cwd=destination)
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|