-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.py
More file actions
executable file
·130 lines (108 loc) · 3.31 KB
/
Copy pathci.py
File metadata and controls
executable file
·130 lines (108 loc) · 3.31 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env -S uv run --no-project --script
# /// script
# requires-python = ">=3.11"
# dependencies = ["pyyaml>=6"]
# ///
"""Canonical CI gate dispatcher.
Usage:
./ci.sh <gate> # run one gate
./ci.sh all # run every gate, continue past failures
./ci.sh --list # show registered gates in order
Every gate is `ci/gates/<name>.py` exposing `def run() -> int`.
GATE_ORDER below is the canonical sequence; `all` runs them in that
order with continue-past-failure semantics, except that a failing
`build` gate halts the run (downstream gates against an uncompiled
tree only produce noise).
See zackees/zccache#835 rule 6 for the rationale: keep ci.yml thin,
push logic to Python, lock the contract so `./ci.sh fmt` on a laptop
runs the exact same bytes as the GHA step.
"""
from __future__ import annotations
import argparse
import importlib
import sys
import traceback
from pathlib import Path
ROOT = Path(__file__).resolve().parent
# Order matters: cheap workspace-state checks first, then language-level
# linters, then the build, then tests, then artifact-shape gates.
# `build` is the only fatal gate — see the loop below.
GATE_ORDER: list[str] = [
"loc",
"fmt",
"clippy",
"ruff",
"build",
"test",
"action_yaml",
"action_surface",
]
FATAL_GATES = {"build"}
def _load_gate(name: str):
sys.path.insert(0, str(ROOT))
return importlib.import_module(f"ci.gates.{name}")
def run_one(name: str) -> int:
print(f"==> [{name}]", flush=True)
try:
mod = _load_gate(name)
except ModuleNotFoundError as exc:
print(f"[{name}] unknown gate: {exc}", file=sys.stderr)
return 1
try:
rc = mod.run()
except Exception:
print(f"[{name}] crashed:", file=sys.stderr)
traceback.print_exc()
return 1
if rc != 0:
print(f"[{name}] FAILED (rc={rc})", file=sys.stderr)
else:
print(f"[{name}] ok", flush=True)
return rc
def run_all() -> int:
failures: list[str] = []
for name in GATE_ORDER:
rc = run_one(name)
if rc != 0:
failures.append(name)
if name in FATAL_GATES:
print(
f"\n[{name}] is fatal; halting (downstream gates would produce noise against an uncompiled tree).",
file=sys.stderr,
)
break
print("")
if failures:
print(f"FAILED: {', '.join(failures)}", file=sys.stderr)
return 1
print("ALL GATES PASSED")
return 0
def main() -> int:
parser = argparse.ArgumentParser(prog="ci.sh", description=__doc__)
parser.add_argument(
"gate",
nargs="?",
default="all",
help="gate name (see --list) or 'all' (default)",
)
parser.add_argument(
"--list",
action="store_true",
help="print registered gates in run order and exit",
)
args = parser.parse_args()
if args.list:
for name in GATE_ORDER:
print(name)
return 0
if args.gate == "all":
return run_all()
if args.gate not in GATE_ORDER:
print(
f"Unknown gate: {args.gate}. Run `./ci.sh --list` for the registered gates.",
file=sys.stderr,
)
return 2
return run_one(args.gate)
if __name__ == "__main__":
raise SystemExit(main())