refactor: move campaign budget, report to probes

parse_budget goes to probes.host beside Settings, and the line a pass prints, with the alert key autorun reads, to probes.reports.pass_report. campaign.py keeps its parser, main and the exit codes.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 14:59:02 +01:00
1 parent ccb78ef5d9
commit 22a81c384d
5 files changed
+32 -21

No files matched your search

+3 -17
View File
@@ -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
+9
View File
@@ -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
+12 -1
View File
@@ -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"]
+3
View File
@@ -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",
+5 -3
View File
@@ -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)