Files
mtusi2026lab6/scripts/capture_evidence.py
T
Artyom 12db77133f Forward the lab 5 foundation while preserving lab 6 telemetry
Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files.

Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
2026-11-06 12:29:35 +03:00

74 lines
2.1 KiB
Python

import argparse
import json
import os
import shlex
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SETTINGS = (
"COMPOSE_PROJECT_NAME",
"COMPOSE_FILE",
"LB_HTTP_PORT",
"LB_STATS_PORT",
"BASE_URL",
"EVIDENCE_DIR",
"RECONCILE_EVIDENCE_DIR",
"NO_PROXY",
"S3_CPUS",
"S3_MEMORY",
"UPLOAD_GRACE_SECONDS",
"API_IMAGE",
"API_REPLICAS",
"WEB_CONCURRENCY",
"MALLOC_MMAP_THRESHOLD_",
)
def main():
parser = argparse.ArgumentParser(description="Capture a command without overwriting evidence")
parser.add_argument("output", type=Path)
parser.add_argument("command", nargs=argparse.REMAINDER)
args = parser.parse_args()
command = args.command[1:] if args.command[:1] == ["--"] else args.command
if not command:
parser.error("command is required")
args.output.parent.mkdir(parents=True, exist_ok=True)
commit = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip()
started = time.monotonic()
with args.output.open("x") as output:
def write(line):
line = line.replace(str(ROOT), "<repo>")
output.write(line)
output.flush()
sys.stdout.write(line)
sys.stdout.flush()
write(f"date: {time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())} commit: {commit}\n")
write(
"environment: "
+ json.dumps({k: os.environ[k] for k in SETTINGS if k in os.environ})
+ "\n"
)
write("$ " + shlex.join(command) + "\n")
with subprocess.Popen(
command,
cwd=ROOT,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
errors="replace",
) as process:
for line in process.stdout:
write(line)
code = process.wait()
write(f"exit_code: {code} elapsed_seconds: {time.monotonic() - started:.3f}\n")
return code
if __name__ == "__main__":
sys.exit(main())