forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadiness_gate_common.py
More file actions
79 lines (60 loc) · 2.49 KB
/
Copy pathreadiness_gate_common.py
File metadata and controls
79 lines (60 loc) · 2.49 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
#!/usr/bin/env python3
"""Shared helpers for memory readiness scripts with optional fail-closed --require-go."""
from __future__ import annotations
import argparse
from typing import Any
GATE_STATUS_GO = "GO"
GATE_STATUS_BLOCKED = "BLOCKED"
GATE_STATUS_NOT_RUN = "NOT_RUN"
def evaluate_gates(gates: dict[str, Any]) -> tuple[str, list[str]]:
"""Return overall status and blockers for gate dicts with per-gate ``status`` keys."""
if not gates:
return GATE_STATUS_NOT_RUN, ["no_gates_defined"]
blockers: list[str] = []
non_go_statuses: list[str] = []
for gate_name, gate in gates.items():
if isinstance(gate, dict):
status = str(gate.get("status", GATE_STATUS_NOT_RUN))
else:
status = GATE_STATUS_NOT_RUN
if status != GATE_STATUS_GO:
blockers.append(f"{gate_name}:{status}")
non_go_statuses.append(status)
if not blockers:
return GATE_STATUS_GO, []
if GATE_STATUS_BLOCKED in non_go_statuses:
overall = GATE_STATUS_BLOCKED
elif GATE_STATUS_NOT_RUN in non_go_statuses:
overall = GATE_STATUS_NOT_RUN
else:
overall = non_go_statuses[0]
return overall, blockers
def exit_code_for_status(status: str, require_go: bool) -> int:
"""Inventory mode always exits 0; --require-go exits 0 only when status is GO."""
if not require_go:
return 0
return 0 if status == GATE_STATUS_GO else 1
def add_require_go_arg(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--require-go",
action="store_true",
help="Fail closed: exit non-zero unless every evaluated gate status is GO.",
)
def collect_gates_from_artifact(artifact: dict[str, Any]) -> dict[str, Any]:
"""Collect gate-like status entries from a readiness artifact payload.
The overall artifact ``status`` is always collected as an ``overall`` gate so
that fail-closed ``--require-go`` behavior is not bypassed when a payload
also carries a ``gates`` dict.
"""
gates: dict[str, Any] = {}
if "status" in artifact:
gates["overall"] = {"status": artifact["status"]}
artifact_gates = artifact.get("gates")
if isinstance(artifact_gates, dict):
gates.update(artifact_gates)
proof_cases = artifact.get("proof_cases")
if isinstance(proof_cases, dict):
for name, case in proof_cases.items():
if isinstance(case, dict) and "status" in case:
gates[f"proof_case:{name}"] = case
return gates