#!/usr/bin/env python3 """Offline device preflight for the LakeIce clean-room one-fit planning floor.""" import argparse import ctypes import hashlib import json import os from pathlib import Path import platform import shutil import struct import sys def memory_bytes(): if os.name == "nt": class Status(ctypes.Structure): _fields_ = [("length", ctypes.c_ulong), ("load", ctypes.c_ulong), ("total", ctypes.c_ulonglong), ("available", ctypes.c_ulonglong), ("total_page", ctypes.c_ulonglong), ("available_page", ctypes.c_ulonglong), ("total_virtual", ctypes.c_ulonglong), ("available_virtual", ctypes.c_ulonglong), ("available_extended", ctypes.c_ulonglong)] value = Status(); value.length = ctypes.sizeof(Status) if not ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(value)): return None return int(value.total) page = os.sysconf("SC_PAGE_SIZE") if hasattr(os, "sysconf") else None count = os.sysconf("SC_PHYS_PAGES") if hasattr(os, "sysconf") else None return int(page * count) if page and count else None def main(): parser = argparse.ArgumentParser() parser.add_argument("--envelope", type=Path, default=Path(__file__).with_name("resource-envelope.json")) parser.add_argument("--working-directory", type=Path, default=Path.cwd()) parser.add_argument("--out", type=Path) args = parser.parse_args() envelope = json.loads(args.envelope.read_text(encoding="utf-8")) if envelope.get("status") != "conservative_planning_floor_not_benchmark": raise ValueError("wrong resource envelope") minimum = envelope["minimums"] actual = {"python_version": platform.python_version(), "architecture_bits": struct.calcsize("P") * 8, "logical_cpu_count": os.cpu_count(), "physical_memory_bytes": memory_bytes(), "free_working_directory_bytes": shutil.disk_usage(args.working_directory).free} checks = { "python_version": sys.version_info >= (3, 9), "architecture_bits": actual["architecture_bits"] >= minimum["architecture_bits"], "logical_cpu_count": (actual["logical_cpu_count"] or 0) >= minimum["logical_cpu_count"], "physical_memory_bytes": actual["physical_memory_bytes"] is not None and actual["physical_memory_bytes"] >= minimum["physical_memory_bytes"], "free_working_directory_bytes": actual["free_working_directory_bytes"] >= minimum["free_working_directory_bytes"] } receipt = {"schema_version": "kiwerb-lakeice-cleanroom-resource-preflight-v1", "status": "planning_floor_passed_not_task_execution" if all(checks.values()) else "planning_floor_not_met", "envelope_sha256": hashlib.sha256(args.envelope.read_bytes()).hexdigest(), "actual": actual, "checks": checks, "network_requests": 0, "files_downloaded": 0, "task_executed": False, "scientific_result_produced": False} body = json.dumps(receipt, indent=2) + "\n" if args.out: with args.out.open("x", encoding="utf-8") as stream: stream.write(body) print(body, end="") raise SystemExit(0 if all(checks.values()) else 2) if __name__ == "__main__": main()