File size: 1,045 Bytes
336c50f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)