RohanExploit/Meta-hackathon
0
1"""Pre-submission checks mapped to Context.txt requirements.2 3Runs local validations and prints a rubric-aligned PASS/FAIL report.4"""5 6from __future__ import annotations7 8import os9import subprocess10import sys11from pathlib import Path12 13 14ROOT = Path(__file__).resolve().parent.parent15 16 17def _run_cmd(cmd: list[str], env: dict[str, str] | None = None) -> tuple[bool, str]:18 try:19 out = subprocess.run(20 cmd,21 cwd=ROOT,22 env=env,23 capture_output=True,24 text=True,25 check=True,26 )27 text = (out.stdout or "") + (out.stderr or "")28 return True, text.strip()29 except subprocess.CalledProcessError as exc:30 text = (exc.stdout or "") + (exc.stderr or "")31 return False, text.strip()32 except FileNotFoundError as exc:33 return False, str(exc)34 35 36def _check_health_script() -> tuple[bool, str]:37 return _run_cmd([sys.executable, "scripts/health_check.py"])38 39 40def _check_task_count() -> tuple[bool, str]:41 code = (42 "from environment.tasks import TASKS; "43 "n=len(TASKS); "44 "print(f'tasks={n}'); "45 "raise SystemExit(0 if n>=3 else 1)"46 )47 return _run_cmd([sys.executable, "-c", code])48 49 50def _check_inference_contract() -> tuple[bool, str]:51 # Mirror what inference.py does: load .env if present52 try:53 from dotenv import load_dotenv54 load_dotenv(ROOT / ".env")55 except ImportError:56 pass57 58 # Checklist rule: only HF_TOKEN is the required auth var (no fallbacks)59 missing = []60 if not os.getenv("HF_TOKEN"):61 missing.append("HF_TOKEN")62 # API_BASE_URL and MODEL_NAME have defaults set in inference.py, so env vars are optional63 if missing:64 return False, f"Missing required env vars for inference: {', '.join(missing)}"65 return True, "Inference env vars present (HF_TOKEN set)"66 67 68def _check_docker_available() -> tuple[bool, str]:69 return _run_cmd(["docker", "--version"])70 71 72def _check_openenv_cli_available() -> tuple[bool, str]:73 ok, out = _run_cmd(["openenv", "validate", "."])74 if not ok and "cannot find" in out.lower():75 return True, "openenv CLI not installed locally (non-blocking — judges run this server-side)"76 return ok, out77 78 79def main() -> int:80 skip_health = os.getenv("PRECHECK_SKIP_HEALTH", "0") == "1"81 skip_docker = os.getenv("PRECHECK_SKIP_DOCKER", "0") == "1"82 skip_openenv = os.getenv("PRECHECK_SKIP_OPENENV", "0") == "1"83 84 checks = [85 ("At least 3 tasks are defined", _check_task_count),86 ("Inference env vars configured", _check_inference_contract),87 ]88 89 if not skip_health:90 checks.insert(0, ("Core health checks (compile+tests+smoke)", _check_health_script))91 92 if not skip_docker:93 checks.append(("Docker CLI available", _check_docker_available))94 if not skip_openenv:95 checks.append(("OpenEnv validate CLI available", _check_openenv_cli_available))96 97 print("\n=== Pre-Submission Check ===")98 failures = 099 100 for title, fn in checks:101 ok, details = fn()102 status = "PASS" if ok else "FAIL"103 print(f"\n[{status}] {title}")104 if details:105 print(details[:1200])106 if not ok:107 failures += 1108 109 print("\n=== Result ===")110 if failures == 0:111 print("All checks passed.")112 return 0113 114 print(f"{failures} check(s) failed. Resolve these before submission.")115 return 1116 117 118if __name__ == "__main__":119 raise SystemExit(main())120 