Datasets:
File size: 1,728 Bytes
4bd5225 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 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
|