Files
mtusi2026lab4-fork/experiments/src/common.py
T

113 lines
3.1 KiB
Python

import hashlib
import json
import os
import subprocess
import tempfile
from pathlib import Path
EXPERIMENT = Path(__file__).resolve().parents[1]
ROOT = EXPERIMENT.parent
EVIDENCE = ROOT / "evidence" / "upload-comparison"
class ExperimentError(RuntimeError):
pass
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def save(path, value):
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_suffix(path.suffix + ".tmp")
temporary.write_text(
json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
os.replace(temporary, path)
def load(path):
return json.loads(path.read_text(encoding="utf-8"))
def command(args, log=None, timeout=300, input_text=None):
# Never format the exception with subprocess output: Docker can include
# environment details. Explicit build/start logs use commands without secrets.
result = subprocess.run(
args,
cwd=ROOT,
text=True,
input=input_text,
stdout=log or subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=timeout,
)
if result.returncode:
raise ExperimentError(
"command failed: " + args[0] + " (exit " + str(result.returncode) + ")"
)
return result.stdout.strip() if log is None else ""
def source_files():
fixed = [
ROOT / "src/Dockerfile",
ROOT / "src/.dockerignore",
ROOT / "src/go.mod",
ROOT / "src/go.sum",
ROOT / "infra/minio.Dockerfile",
ROOT / "infra/haproxy.cfg",
]
app = sorted(
p
for p in (ROOT / "src/backend").rglob("*")
if p.is_file() and p.suffix in (".go", ".sql")
)
harness = sorted(
p
for p in EXPERIMENT.rglob("*")
if p.is_file()
and "evidence" not in p.relative_to(EXPERIMENT).parts
and "__pycache__" not in p.parts
and p.suffix != ".pyc"
)
return sorted(set(fixed + app + harness))
def source_manifest():
return {str(p.relative_to(ROOT)): digest(p) for p in source_files()}
def check_sources(campaign):
expected = load(campaign / "source-manifest.json")
if source_manifest() != expected:
raise ExperimentError(
"source/config/docs changed since prepare; create a new campaign"
)
def check_seal(campaign):
seal = load(campaign / "seal.json")
for name, expected in seal.items():
if digest(campaign / name) != expected:
raise ExperimentError("frozen artifact changed: " + name)
for entry in load(campaign / "fixtures.json"):
if digest(campaign / "fixtures" / entry["filename"]) != entry["sha256"]:
raise ExperimentError("fixture checksum changed")
def campaign_path(value):
path = Path(value).expanduser().resolve()
if path.parent != EVIDENCE.resolve() or not (path / "protocol.json").is_file():
raise ExperimentError(
"campaign must be a prepared directory directly inside evidence/upload-comparison"
)
return path
def private_directory():
path = Path(tempfile.mkdtemp(prefix="highload-upload-exp-", dir="/tmp"))
path.chmod(0o700)
return path