dev_container.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python3
  2. """Thin, deterministic Podman entrypoint for the Linux-native workspace."""
  3. from __future__ import annotations
  4. import argparse
  5. import hashlib
  6. import json
  7. import os
  8. from pathlib import Path
  9. import subprocess
  10. import sys
  11. REPO = Path(__file__).resolve().parents[1]
  12. WORKSPACE = REPO.parent
  13. IMAGE = "localhost/prod2u-thermal-dev:2026-08-22"
  14. CONTAINERFILE = REPO / "containers" / "Containerfile.dev"
  15. RECEIPT = REPO / "receipts" / "environment" / "container-image.json"
  16. TOOL_BASE = Path(os.environ.get("PRODU2_TOOLCHAIN_BASE", "/data/prod2u"))
  17. def run(argv: list[str], *, capture: bool = False) -> subprocess.CompletedProcess[str]:
  18. return subprocess.run(
  19. argv,
  20. cwd=REPO,
  21. check=True,
  22. text=True,
  23. stdout=subprocess.PIPE if capture else None,
  24. stderr=subprocess.STDOUT if capture else None,
  25. )
  26. def image_digest() -> tuple[str, str]:
  27. result = run(
  28. ["podman", "image", "inspect", IMAGE, "--format", "{{.Id}} {{.Digest}}"],
  29. capture=True,
  30. )
  31. image_id, digest = result.stdout.strip().split(maxsplit=1)
  32. return image_id, digest
  33. def verified_image_identity() -> tuple[str, str]:
  34. payload = json.loads(RECEIPT.read_text(encoding="utf-8"))
  35. image_id, digest = image_digest()
  36. expected = {
  37. "image": IMAGE,
  38. "image_id": image_id,
  39. "digest": digest,
  40. "containerfile_sha256": hashlib.sha256(CONTAINERFILE.read_bytes()).hexdigest(),
  41. "requirements_sha256": hashlib.sha256((REPO / "requirements/dev.txt").read_bytes()).hexdigest(),
  42. }
  43. mismatches = [
  44. key for key, value in expected.items()
  45. if payload.get(key) != value
  46. ]
  47. if mismatches:
  48. raise RuntimeError(
  49. "pinned container receipt does not match current inputs/image: "
  50. + ", ".join(mismatches)
  51. )
  52. return image_id, digest
  53. def container_args() -> list[str]:
  54. TOOL_BASE.mkdir(parents=True, exist_ok=True)
  55. image_id, digest = verified_image_identity()
  56. return [
  57. "podman",
  58. "run",
  59. "--rm",
  60. "--pull=never",
  61. "--security-opt",
  62. "label=disable",
  63. "-e",
  64. "XDG_CACHE_HOME=/tmp/xdg-cache",
  65. "-e",
  66. "XDG_STATE_HOME=/tmp/xdg-state",
  67. "-e",
  68. "XDG_RUNTIME_DIR=/tmp/xdg-runtime",
  69. "-e",
  70. f"PROD2U_CONTAINER_IMAGE_ID={image_id}",
  71. "-e",
  72. f"PROD2U_CONTAINER_DIGEST={digest}",
  73. "-v",
  74. f"{WORKSPACE}:/workspace",
  75. "-v",
  76. f"{TOOL_BASE}:/data/prod2u",
  77. "-w",
  78. "/workspace/cpld-temperature-control",
  79. IMAGE,
  80. ]
  81. def build() -> None:
  82. run(
  83. [
  84. "podman",
  85. "build",
  86. "--pull=never",
  87. "--tag",
  88. IMAGE,
  89. "--file",
  90. str(CONTAINERFILE),
  91. str(REPO),
  92. ]
  93. )
  94. image_id, digest = image_digest()
  95. payload = {
  96. "schema": "prod2u-container-image-v1",
  97. "image": IMAGE,
  98. "image_id": image_id,
  99. "digest": digest,
  100. "base": "docker.io/library/golang@sha256:3b4a11519ad929d1e1d261a12cff056f0c85b735253d7d861346b9c6f8b36437",
  101. "containerfile_sha256": hashlib.sha256(CONTAINERFILE.read_bytes()).hexdigest(),
  102. "requirements_sha256": hashlib.sha256((REPO / "requirements/dev.txt").read_bytes()).hexdigest(),
  103. "sby_commit": "06c36d5bb0d8eb35db60ba44798477c3867e4644",
  104. "z3_solver_version": "4.16.0.0",
  105. }
  106. RECEIPT.parent.mkdir(parents=True, exist_ok=True)
  107. RECEIPT.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  108. print(json.dumps(payload, indent=2, sort_keys=True))
  109. def in_container(command: list[str]) -> None:
  110. run(container_args() + command)
  111. def doctor() -> None:
  112. probe = [
  113. "sh",
  114. "-c",
  115. "set -eu; "
  116. "cat /etc/os-release; "
  117. "python3 --version; go version; git --version; "
  118. "iverilog -V | head -1; verilator --version; yosys -V; "
  119. "sby -h >/dev/null; echo sby-commit=06c36d5bb0d8eb35db60ba44798477c3867e4644; "
  120. "boolector --version; z3 --version; pytest --version; "
  121. "python3 -c 'import cocotb,hypothesis,jsonschema,numpy,pandas,pyarrow,yaml; print(\"python-deps=ok\")'",
  122. ]
  123. in_container(probe)
  124. def main() -> int:
  125. parser = argparse.ArgumentParser()
  126. sub = parser.add_subparsers(dest="command", required=True)
  127. sub.add_parser("build")
  128. sub.add_parser("doctor")
  129. run_parser = sub.add_parser("run")
  130. run_parser.add_argument("argv", nargs=argparse.REMAINDER)
  131. shell_parser = sub.add_parser("shell")
  132. shell_parser.add_argument("argv", nargs=argparse.REMAINDER)
  133. args = parser.parse_args()
  134. if args.command == "build":
  135. build()
  136. elif args.command == "doctor":
  137. doctor()
  138. elif args.command == "run":
  139. if not args.argv:
  140. parser.error("run requires a command")
  141. in_container(args.argv)
  142. else:
  143. command = args.argv or ["bash"]
  144. run(container_args()[:-1] + ["-it", container_args()[-1]] + command)
  145. return 0
  146. if __name__ == "__main__":
  147. sys.exit(main())