Files
prometeu-runtime/scripts/run-quality-gate.py
T

102 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Run Makefile-owned gate steps serially and retain evidence on failure."""
import hashlib
import json
import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
from datetime import datetime, timezone
def capture(*command):
try:
result = subprocess.run(command, text=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, check=False)
return {"exit_code": result.returncode, "output": result.stdout.strip()}
except OSError as error:
return {"exit_code": 127, "output": str(error)}
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def main():
root = Path(__file__).resolve().parent.parent
os.chdir(root)
output = root / "target/quality-gate"
# These two directories contain generated reports only. Never archive a
# previous run's coverage as evidence for an early lint/test failure.
for directory in (output, root / "target/llvm-cov"):
if directory.exists():
shutil.rmtree(directory)
output.mkdir(parents=True)
inputs = [Path("Makefile"), Path("Cargo.lock"), Path(".github/workflows/ci.yml"),
Path("files/config/Jenkinsfile"), Path("files/config/runtime-edge-coverage-domains.json")]
inputs.extend(sorted(Path("scripts").rglob("*.py")))
inputs.extend(sorted(Path("scripts").glob("*.sh")))
fingerprints = {str(path): sha(path) for path in inputs if path.is_file()}
sources = "".join(f"{path}\0{sha(path)}\n" for path in sorted(Path("crates").rglob("*.rs")))
evidence = {
"started_utc": datetime.now(timezone.utc).isoformat(),
"host": platform.platform(), "revision": capture("git", "rev-parse", "HEAD"),
"working_tree": capture("git", "status", "--porcelain"),
"rustc": capture("rustc", "-vV"), "cargo_llvm_cov": capture("cargo", "llvm-cov", "--version"),
"config_sha256": fingerprints,
"rust_source_sha256": hashlib.sha256(sources.encode()).hexdigest(),
"source_hash_algorithm": "SHA-256 of sorted path + NUL + file SHA-256 hex + newline records",
"steps": [], "status": "running",
}
metadata = output / "metadata.json"
def persist():
metadata.write_text(json.dumps(evidence, indent=2) + "\n")
persist()
exit_code = 0
try:
if not sys.argv[1:]:
raise ValueError("Makefile must supply the required gate steps")
for target in sys.argv[1:]:
if not target.replace("-", "").isalnum():
raise ValueError(f"invalid make target: {target!r}")
command = ["make", "--no-print-directory", "-j1", target]
log = output / f"{target}.log"
print(f"Quality gate: {target}", flush=True)
with log.open("w") as stream:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, errors="replace")
for line in process.stdout:
stream.write(line)
print(line, end="", flush=True)
result = process.wait()
exit_code = result if result >= 0 else 128 - result
evidence["steps"].append({"target": target, "command": command,
"exit_code": exit_code, "log_sha256": sha(log)})
persist()
if exit_code:
break
except KeyboardInterrupt:
evidence["error"] = "Interrupted"
exit_code = 130
except Exception as error:
evidence["error"] = str(error)
exit_code = 1
finally:
evidence["status"] = "passed" if exit_code == 0 else "failed"
evidence["exit_code"] = exit_code
evidence["finished_utc"] = datetime.now(timezone.utc).isoformat()
evidence["reports_sha256"] = {
str(path.relative_to(root)): sha(path)
for path in sorted((root / "target/llvm-cov").rglob("*")) if path.is_file()
}
persist()
return exit_code
if __name__ == "__main__":
sys.exit(main())