write_toolchain_manifest.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. """Record executable path, version, and SHA-256 for tools used by a run."""
  3. from __future__ import annotations
  4. import hashlib
  5. import json
  6. import os
  7. import shutil
  8. import subprocess
  9. from pathlib import Path
  10. TOOLS = (
  11. ("go", ["go", "version"]),
  12. ("gcc", ["gcc", "--version"]),
  13. ("python3", ["python3", "--version"]),
  14. ("python3.11", ["python3.11", "--version"]),
  15. ("python3.12", ["python3.12", "--version"]),
  16. ("python3.13", ["python3.13", "--version"]),
  17. ("pip3", ["pip3", "--version"]),
  18. ("protoc", ["protoc", "--version"]),
  19. ("protoc-gen-go", ["protoc-gen-go", "--version"]),
  20. ("protoc-gen-go-grpc", ["protoc-gen-go-grpc", "--version"]),
  21. ("buf", ["buf", "--version"]),
  22. ("staticcheck", ["staticcheck", "-version"]),
  23. ("govulncheck", ["govulncheck", "-version"]),
  24. ("qemu-system-x86_64", ["qemu-system-x86_64", "--version"]),
  25. ("qemu-system-aarch64", ["qemu-system-aarch64", "--version"]),
  26. ("iverilog", ["iverilog", "-V"]),
  27. ("verilator", ["verilator", "--version"]),
  28. ("git", ["git", "--version"]),
  29. )
  30. def _sha(path: str) -> str:
  31. p = Path(path)
  32. if not p.is_file():
  33. return ""
  34. h = hashlib.sha256()
  35. with p.open("rb") as f:
  36. for chunk in iter(lambda: f.read(1 << 20), b""):
  37. h.update(chunk)
  38. return h.hexdigest()
  39. def _run(args: list[str]) -> str:
  40. try:
  41. out = subprocess.check_output(args, stderr=subprocess.STDOUT, text=True, timeout=15)
  42. return out.strip().splitlines()[0][:300]
  43. except (OSError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
  44. return f"unavailable: {exc}"
  45. def main() -> int:
  46. run = Path(os.environ.get("HTH_RUN_DIR") or "")
  47. if not run.is_dir():
  48. raise SystemExit("set HTH_RUN_DIR")
  49. rows = []
  50. for name, args in TOOLS:
  51. exe = shutil.which(args[0])
  52. rows.append(
  53. {
  54. "name": name,
  55. "path": exe or "",
  56. "version": _run(args) if exe else "NOT FOUND",
  57. "sha256": _sha(exe) if exe else "",
  58. }
  59. )
  60. extra = {
  61. "uname": _run(["uname", "-sr"]),
  62. "kernel": Path("/proc/version").read_text(encoding="utf-8", errors="replace").strip()
  63. if Path("/proc/version").is_file()
  64. else "",
  65. }
  66. try:
  67. import google.protobuf as pb
  68. extra["protobuf_runtime"] = getattr(pb, "__version__", "unknown")
  69. except Exception as exc: # noqa: BLE001
  70. extra["protobuf_runtime"] = f"unavailable: {exc}"
  71. payload = {"tools": rows, "extra": extra}
  72. out = run / "toolchain-manifest.json"
  73. out.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
  74. print(out)
  75. return 0
  76. if __name__ == "__main__":
  77. raise SystemExit(main())