#!/usr/bin/env python3 """Record executable path, version, and SHA-256 for tools used by a run.""" from __future__ import annotations import hashlib import json import os import shutil import subprocess from pathlib import Path TOOLS = ( ("go", ["go", "version"]), ("gcc", ["gcc", "--version"]), ("python3", ["python3", "--version"]), ("python3.11", ["python3.11", "--version"]), ("python3.12", ["python3.12", "--version"]), ("python3.13", ["python3.13", "--version"]), ("pip3", ["pip3", "--version"]), ("protoc", ["protoc", "--version"]), ("protoc-gen-go", ["protoc-gen-go", "--version"]), ("protoc-gen-go-grpc", ["protoc-gen-go-grpc", "--version"]), ("buf", ["buf", "--version"]), ("staticcheck", ["staticcheck", "-version"]), ("govulncheck", ["govulncheck", "-version"]), ("qemu-system-x86_64", ["qemu-system-x86_64", "--version"]), ("qemu-system-aarch64", ["qemu-system-aarch64", "--version"]), ("iverilog", ["iverilog", "-V"]), ("verilator", ["verilator", "--version"]), ("git", ["git", "--version"]), ) def _sha(path: str) -> str: p = Path(path) if not p.is_file(): return "" h = hashlib.sha256() with p.open("rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def _run(args: list[str]) -> str: try: out = subprocess.check_output(args, stderr=subprocess.STDOUT, text=True, timeout=15) return out.strip().splitlines()[0][:300] except (OSError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc: return f"unavailable: {exc}" def main() -> int: run = Path(os.environ.get("HTH_RUN_DIR") or "") if not run.is_dir(): raise SystemExit("set HTH_RUN_DIR") rows = [] for name, args in TOOLS: exe = shutil.which(args[0]) rows.append( { "name": name, "path": exe or "", "version": _run(args) if exe else "NOT FOUND", "sha256": _sha(exe) if exe else "", } ) extra = { "uname": _run(["uname", "-sr"]), "kernel": Path("/proc/version").read_text(encoding="utf-8", errors="replace").strip() if Path("/proc/version").is_file() else "", } try: import google.protobuf as pb extra["protobuf_runtime"] = getattr(pb, "__version__", "unknown") except Exception as exc: # noqa: BLE001 extra["protobuf_runtime"] = f"unavailable: {exc}" payload = {"tools": rows, "extra": extra} out = run / "toolchain-manifest.json" out.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8") print(out) return 0 if __name__ == "__main__": raise SystemExit(main())