mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-11-22 17:47:48 -06:00
refactor: read verify output by its command
This commit is contained in:
1 parent
35ad46e8c5
commit
75ccaf460c
4 files changed
+48
-32
No files matched your search
@@ -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)), "")
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
+21
-10
@@ -20,8 +20,9 @@ from unittest import mock
|
||||
import yaml
|
||||
|
||||
from freshness import stalefindings
|
||||
from freshness.stalecommands import verify_command
|
||||
from freshness.stalefindings import load_findings
|
||||
from freshness.stalejob import AUTO_AREAS, MANUAL_AREAS, Job, JobResult
|
||||
from freshness.stalejob import AUTO_AREAS, LOG_DIR, MANUAL_AREAS, Job, JobResult
|
||||
from freshness.staleplan import plan_jobs
|
||||
from freshness.stalereport import render
|
||||
from freshness.stalerun import run_job, run_jobs
|
||||
@@ -202,18 +203,28 @@ class Running(unittest.TestCase):
|
||||
self.assertEqual(order.read_text().split(), ["parent", "child"])
|
||||
|
||||
def test_the_report_prints_verify_after_a_rescrape(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
log = Path(directory) / "x.log"
|
||||
log.write_text(
|
||||
"$ python -m scripts.scraper.x_scraper\nscraped\n"
|
||||
"$ python scripts/verify.py --platform x\n"
|
||||
"X: 10/12 OK, 2 missing [md5]\n"
|
||||
)
|
||||
job = Job("platforms", "x", ["true"], log)
|
||||
text = render([JobResult(job, 0, 1.0, "")], [], [])
|
||||
job = Job("platforms", "x", ["true"], LOG_DIR / "x.log")
|
||||
outputs = (
|
||||
(("true",), "scraped\n"),
|
||||
(tuple(verify_command("x")), "\nX: 10/12 OK, 2 missing [md5]\n"),
|
||||
)
|
||||
text = render([JobResult(job, 0, 1.0, "", outputs)], [], [])
|
||||
self.assertIn("X: 10/12 OK, 2 missing [md5]", text)
|
||||
self.assertIn("python scripts/verify.py --platform x", text)
|
||||
|
||||
def test_the_verify_output_is_read_whatever_its_command_line(self):
|
||||
"""The report found verify by a copy of its command line in the log:
|
||||
a verify run as `-m scripts.verify` read as `(no verify output)`."""
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
verify = (sys.executable, "-c", "print('X: 10/12 OK')")
|
||||
job = Job("platforms", "x", [sys.executable, "-c", "print('scraped')"],
|
||||
Path(directory) / "x.log", (verify,))
|
||||
with mock.patch("freshness.stalereport.verify_command", return_value=list(verify)):
|
||||
result = run_job(job, {})
|
||||
text = render([result], [], [])
|
||||
self.assertEqual(result.output_of(job.command), "scraped\n")
|
||||
self.assertIn("X: 10/12 OK", text)
|
||||
|
||||
def _job(self, directory: str, command: list[str], then=()) -> Job:
|
||||
return Job("platforms", "x", command, Path(directory) / "x.log", then)
|
||||
|
||||
|
||||
Reference in new issue
Block a user