#!/usr/bin/env python3 """Run a reproducible pipeline step, capturing its command, output and wall time.""" import datetime import json import pathlib import shlex import subprocess import sys import time root = pathlib.Path(__file__).resolve().parents[1] name, *command = sys.argv[1:] if command and command[0] == "--": command.pop(0) start = datetime.datetime.now(datetime.timezone.utc) tick = time.monotonic() with (root / "logs" / f"{name}.log").open("w") as log: log.write(f"$ {shlex.join(command)}\n") log.flush() result = subprocess.run(command, cwd=root, stdout=log, stderr=subprocess.STDOUT) record = dict(stage=name, command=command, cwd=str(root), started_utc=start.isoformat(), finished_utc=datetime.datetime.now(datetime.timezone.utc).isoformat(), elapsed_seconds=time.monotonic() - tick, returncode=result.returncode) (root / "logs" / f"{name}.timing.json").write_text(json.dumps(record, indent=2) + "\n") print(json.dumps(record), flush=True) sys.exit(result.returncode)