diff --git a/scripts/campaign.py b/scripts/campaign.py index a7e0932f7..297a976db 100644 --- a/scripts/campaign.py +++ b/scripts/campaign.py @@ -62,7 +62,6 @@ from __future__ import annotations import argparse import json -import re import signal import sys from collections.abc import Sequence @@ -86,20 +85,13 @@ from core.paths import REPO_ROOT from probes.campaign import Campaign from probes.content import ROMFETCH_ENV from probes.gate import changed_profiles, gate -from probes.host import CONTENT_FILE, Paths, Settings, host_lock -from probes.reports import REVIEWED, open_findings, render_status, review_finding, status_report +from probes.host import CONTENT_FILE, Paths, Settings, host_lock, parse_budget +from probes.reports import REVIEWED, open_findings, pass_report, render_status, review_finding, status_report EXIT_REFUSED = 3 -def parse_budget(text: str) -> float: - match = re.fullmatch(r"\s*(\d+(?:\.\d+)?)\s*([smh]?)\s*", text or "") - if not match: - raise argparse.ArgumentTypeError(f"--budget {text!r}: a number with s, m or h") - return float(match[1]) * {"": 1, "s": 1, "m": 60, "h": 3600}[match[2]] - - # -------------------------------------------------------------------------- # Command line # -------------------------------------------------------------------------- @@ -248,13 +240,7 @@ def _run_pass(campaign: Campaign, args: argparse.Namespace, only: set[str], path except hostguard.LockBusy as exc: print(f"campaign: deferred, {exc}", file=sys.stderr) return 75 - counts, _, _ = campaign.counts() - report = {"result": current.result, "detail": current.detail, "counts": counts, - "records": len(current.records), "findings": current.findings} - if current.result == "refused": - # autorun raises an alert from this key of a job's report. - report["alert"] = current.detail - print(json.dumps(report)) + print(json.dumps(pass_report(campaign, current))) if current.result == "refused": return EXIT_REFUSED return 75 if current.result == "paused" else 0 diff --git a/scripts/probes/host.py b/scripts/probes/host.py index bc74f9220..fa6274788 100644 --- a/scripts/probes/host.py +++ b/scripts/probes/host.py @@ -2,6 +2,7 @@ from __future__ import annotations +import argparse import contextlib import http.client import os @@ -127,6 +128,14 @@ class Host: return self.cpu_count / 4 +def parse_budget(text: str) -> float: + """Seconds of a --budget value: a number with s, m or h.""" + match = re.fullmatch(r"\s*(\d+(?:\.\d+)?)\s*([smh]?)\s*", text or "") + if not match: + raise argparse.ArgumentTypeError(f"--budget {text!r}: a number with s, m or h") + return float(match[1]) * {"": 1, "s": 1, "m": 60, "h": 3600}[match[2]] + + @dataclass class Settings: budget: float = 6 * 3600 diff --git a/scripts/probes/reports.py b/scripts/probes/reports.py index 7e261ec21..12e964e44 100644 --- a/scripts/probes/reports.py +++ b/scripts/probes/reports.py @@ -14,7 +14,7 @@ from probes.profileedit import write_fields from probes.state import DONE, FAILED, PENDING if TYPE_CHECKING: - from probes.campaign import Campaign + from probes.campaign import Campaign, Pass # -------------------------------------------------------------------------- @@ -32,6 +32,17 @@ def status_report(campaign: Campaign) -> dict: "failed": failed, "emulators": per} +def pass_report(campaign: Campaign, current: Pass) -> dict: + """The line a pass prints: its result, the check counts, its records and findings.""" + counts, _, _ = campaign.counts() + report = {"result": current.result, "detail": current.detail, "counts": counts, + "records": len(current.records), "findings": current.findings} + if current.result == "refused": + # autorun raises an alert from this key of a job's report. + report["alert"] = current.detail + return report + + def render_status(report: dict) -> str: lines = [] last = report["last_run"] diff --git a/tests/sources.py b/tests/sources.py index 7fedea7d7..0af381544 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -154,6 +154,9 @@ COMMANDS = { "games/inputs.py", "games/profileindex.py", "games/fallback.py", "games/folders.py", "games/setups.py", "games/systems.py", "games/arcadesets.py", "games/render.py", "games/coverage.py", "games/changes.py", "games/publish.py", + "campaign.py": ( + "probes/campaign.py", "probes/host.py", "probes/content.py", "probes/cores.py", "probes/rules.py", + "probes/state.py", "probes/gate.py", "probes/reports.py", "probes/profileedit.py", "standalone_probe.py": ( "probes/standaloneemulators.py", "probes/appimage.py", "probes/xvfb.py", "probes/standalonerun.py", "probes/standalonerecord.py", diff --git a/tests/test_campaign.py b/tests/test_campaign.py index f36613bdd..ef881310d 100644 --- a/tests/test_campaign.py +++ b/tests/test_campaign.py @@ -491,9 +491,9 @@ class StateAndResume(Workspace): self.assertEqual(again.state["checks"]["fake"]["fake/fake-system/x/files"]["status"], "pending") def test_budget(self) -> None: - self.assertEqual(campaign.parse_budget("6h"), 21600) - self.assertEqual(campaign.parse_budget("90m"), 5400) - self.assertEqual(campaign.parse_budget("30"), 30) + self.assertEqual(probes_host.parse_budget("6h"), 21600) + self.assertEqual(probes_host.parse_budget("90m"), 5400) + self.assertEqual(probes_host.parse_budget("30"), 30) class Guards(Workspace): @@ -577,6 +577,8 @@ class FailClosed(Workspace): self.assertIn("bwrap not found", current.detail) self.assertEqual(host.calls, []) self.assertEqual(mr.read_campaign(self.root, mr.CAMPAIGN_RUNS)[-1]["result"], "refused") + # autorun raises an alert from this key of the printed report. + self.assertEqual(reports.pass_report(c, current)["alert"], current.detail) def test_the_override_lets_it_run(self) -> None: c = self.make(allow_unsandboxed=True)