| from __future__ import annotations |
|
|
| from dataclasses import asdict, is_dataclass |
| from datetime import datetime, timezone |
| import json |
| from pathlib import Path |
| import platform |
| import subprocess |
| import sys |
| from typing import Any |
|
|
|
|
| def write_run_metadata( |
| output_dir: str | Path, |
| *, |
| run_type: str, |
| args: dict[str, Any], |
| config: Any, |
| extra: dict[str, Any] | None = None, |
| ) -> Path: |
| output = Path(output_dir) |
| output.mkdir(parents=True, exist_ok=True) |
| payload = { |
| "run_type": run_type, |
| "created_at_utc": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), |
| "command": " ".join(sys.argv), |
| "python": sys.executable, |
| "platform": platform.platform(), |
| "git_commit": _git(["rev-parse", "HEAD"]), |
| "git_status_short": _git(["status", "--short"]), |
| "args": _jsonable(args), |
| "config": _jsonable(asdict(config) if is_dataclass(config) else config), |
| "extra": _jsonable(extra or {}), |
| } |
| path = output / "run_metadata.json" |
| path.write_text(json.dumps(payload, indent=2), encoding="utf-8") |
| return path |
|
|
|
|
| def _git(args: list[str]) -> str | None: |
| try: |
| result = subprocess.run( |
| ["git", *args], |
| check=True, |
| capture_output=True, |
| text=True, |
| timeout=10, |
| ) |
| except Exception: |
| return None |
| return result.stdout.strip() |
|
|
|
|
| def _jsonable(value: Any) -> Any: |
| if isinstance(value, Path): |
| return str(value) |
| if isinstance(value, dict): |
| return {str(key): _jsonable(item) for key, item in value.items()} |
| if isinstance(value, (list, tuple)): |
| return [_jsonable(item) for item in value] |
| return value |
|
|
|
|