swarm repositories / source
aboutsummaryrefslogtreecommitdiff
path: root/publish.py
blob: e9df3eb3629bf965ef0f8a95e8ab2c225317e1f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
from __future__ import annotations

import subprocess
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parent


def run(*argv: str) -> None:
    print(f"[publish] {' '.join(argv)}", flush=True)
    subprocess.run(argv, cwd=ROOT, check=True)


def output(*argv: str) -> str:
    return subprocess.check_output(argv, cwd=ROOT, text=True).strip()


def require_clean_worktree() -> None:
    status = output("git", "status", "--porcelain")
    if status:
        print("[publish] dirty worktree; commit or stash before publishing", file=sys.stderr)
        print(status, file=sys.stderr)
        raise SystemExit(1)


def verify_remote(remote: str) -> None:
    head = output("git", "ls-remote", remote, "refs/heads/main").split()[0]
    local = output("git", "rev-parse", "HEAD")
    if head != local:
        raise SystemExit(f"[publish] {remote}/main is {head}, expected {local}")
    print(f"[publish] verified {remote}/main {head}", flush=True)


def main() -> None:
    require_clean_worktree()
    run("./check.py", "install")
    require_clean_worktree()
    run("git", "push", "--follow-tags", "swarm", "main")
    verify_remote("swarm")
    run("git", "push", "--follow-tags", "github", "main")
    verify_remote("github")


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        raise SystemExit(130)
    except subprocess.CalledProcessError as error:
        raise SystemExit(error.returncode)