refactor: read verify output by its command

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:57 +01:00
1 parent 35ad46e8c5
commit 75ccaf460c
4 files changed
+48 -32

No files matched your search

+7
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path
@@ -35,3 +36,9 @@ class JobResult:
returncode: int
duration: float
tail: str # last non-empty line of stderr or stdout
# (command, what it printed) for each command that ran, in order.
outputs: tuple[tuple[tuple[str, ...], str], ...] = ()
def output_of(self, command: Sequence[str]) -> str:
"""What *command* printed in this job, empty when it did not run."""
return next((text for ran, text in self.outputs if ran == tuple(command)), "")
+7 -21
View File
@@ -2,29 +2,15 @@
from __future__ import annotations
from pathlib import Path
from core.paths import REPO_ROOT
from freshness.stalecommands import verify_command
from freshness.stalejob import Job, JobResult
VERIFY_MARK = "scripts/verify.py --platform"
def verify_summary(path: Path) -> str:
"""First line verify printed in a job log, empty when it did not run."""
try:
text = path.read_text(encoding="utf-8", errors="replace")
except OSError:
return ""
lines = text.splitlines()
for index, line in enumerate(lines):
if line.startswith("$ ") and VERIFY_MARK in line:
return next(
(rest.strip() for rest in lines[index + 1:]
if rest.strip() and not rest.startswith("$ ")),
"",
)
return ""
def verify_summary(result: JobResult) -> str:
"""First line verify printed in a job, empty when it did not run."""
output = result.output_of(verify_command(result.job.subject))
return next((line.strip() for line in output.splitlines() if line.strip()), "")
def _by_area(jobs: list[Job]) -> list[Job]:
@@ -121,10 +107,10 @@ def render(
"before keeping platforms/<name>.yml"
)
for r in rescraped:
summary = verify_summary(r.job.log_path) or "(no verify output)"
summary = verify_summary(r) or "(no verify output)"
lines.append(f" {r.job.subject:12} {summary}")
lines.append(
f" {'':12} python scripts/verify.py --platform {r.job.subject}"
f" {'':12} python {' '.join(verify_command(r.job.subject)[1:])}"
)
if surfaced:
+13 -1
View File
@@ -64,10 +64,12 @@ def run_job(job: Job, env: dict[str, str]) -> JobResult:
start = time.monotonic()
job.log_path.parent.mkdir(parents=True, exist_ok=True)
returncode = 0
outputs: list[tuple[tuple[str, ...], str]] = []
with job.log_path.open("w", encoding="utf-8") as log:
for command in (job.command, *job.then):
log.write(f"$ {' '.join(command)}\n")
log.flush()
start_offset = job.log_path.stat().st_size
try:
proc = subprocess.run(
list(command),
@@ -82,11 +84,21 @@ def run_job(job: Job, env: dict[str, str]) -> JobResult:
except subprocess.TimeoutExpired:
log.write(f"\nTIMEOUT after {JOB_TIMEOUT}s\n")
returncode = 124
log.flush()
outputs.append((tuple(command), _log_since(job.log_path, start_offset)))
if returncode != 0:
break
duration = time.monotonic() - start
tail = _log_tail(job.log_path)
return JobResult(job=job, returncode=returncode, duration=duration, tail=tail)
return JobResult(job=job, returncode=returncode, duration=duration, tail=tail,
outputs=tuple(outputs))
def _log_since(path: Path, offset: int) -> str:
"""What a log holds past *offset*: the output of the command that just ran."""
with path.open("rb") as log:
log.seek(offset)
return log.read().decode("utf-8", errors="replace")
def _log_tail(path: Path) -> str: