| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #!/usr/bin/env python3
- """Windows-friendly command recorder. Writes <name>.log and <name>.meta."""
- from __future__ import annotations
- import hashlib
- import os
- import subprocess
- import sys
- import time
- from datetime import datetime, timezone
- from pathlib import Path
- def _utc() -> str:
- return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
- def main() -> int:
- if len(sys.argv) < 3:
- raise SystemExit("usage: record_cmd.py NAME command...")
- name = sys.argv[1]
- cmd = sys.argv[2:]
- run = Path(os.environ["HTH_RUN_DIR"])
- run.mkdir(parents=True, exist_ok=True)
- start = _utc()
- t0 = time.time()
- log = run / f"{name}.log"
- meta = run / f"{name}.meta"
- proc = subprocess.run(cmd, cwd=os.environ.get("HTH_REC_CWD") or os.getcwd(), capture_output=True)
- out = (proc.stdout or b"") + (proc.stderr or b"")
- log.write_bytes(out)
- end = _utc()
- status = "PASS" if proc.returncode == 0 else ("NOT RUN" if proc.returncode == 2 else "FAIL")
- meta.write_text(
- "\n".join(
- [
- f"run_id={os.environ.get('HTH_RUN_ID', run.name)}",
- f"commit={os.environ.get('HTH_GIT_COMMIT', '')}",
- f"tree={os.environ.get('HTH_GIT_TREE', '')}",
- f"dirty={os.environ.get('HTH_GIT_DIRTY', 'dirty')}",
- f"cwd={os.getcwd()}",
- f"start_utc={start}",
- f"end_utc={end}",
- f"duration_s={int(time.time() - t0)}",
- f"exit={proc.returncode}",
- f"status={status}",
- f"log_sha256={hashlib.sha256(out).hexdigest()}",
- f"command={' '.join(cmd)}",
- "",
- ]
- ),
- encoding="utf-8",
- )
- print(f"{name} status={status} exit={proc.returncode}")
- return proc.returncode
- if __name__ == "__main__":
- raise SystemExit(main())
|