diff --git a/scripts/probes/campaign.py b/scripts/probes/campaign.py index e99ebfa95..16be386a5 100644 --- a/scripts/probes/campaign.py +++ b/scripts/probes/campaign.py @@ -37,6 +37,7 @@ from probes.entries import ( region_matches, regions_of, ) +from probes.findings import OPEN_STATUSES, latest_findings from probes.gate import gate from probes.host import ( Host, @@ -629,16 +630,10 @@ class Campaign: def finding_key(self, emulator: str, content: Content, entry: dict, verdict: rules.Verdict) -> str: return f"{emulator}/{content.system}/{entry['name']}/{verdict.field}" - def latest_findings(self) -> dict[str, dict]: - latest: dict[str, dict] = {} - for line in mr.read_campaign(self.paths.root, mr.FINDINGS): - latest[line["key"]] = line - return latest - def finding(self, emulator: str, content: Content, core: Core, entry: dict, verdict: rules.Verdict, status: str, says: object, extra: str = "") -> None: key = self.finding_key(emulator, content, entry, verdict) - previous = self.latest_findings().get(key) + previous = latest_findings(self.paths.root).get(key) if previous and previous["status"] == status and previous["rule"] == verdict.rule \ and previous["run_shows"] == verdict.value and previous["profile_says"] == says \ and previous["evidence"] == list(verdict.evidence): @@ -669,8 +664,8 @@ class Campaign: def resolve(self, emulator: str, content: Content, core: Core, entry: dict, verdict: rules.Verdict, says: object) -> None: - previous = self.latest_findings().get(self.finding_key(emulator, content, entry, verdict)) - if previous and previous["status"] in ("open", "flagged"): + previous = latest_findings(self.paths.root).get(self.finding_key(emulator, content, entry, verdict)) + if previous and previous["status"] in OPEN_STATUSES: self.finding(emulator, content, core, entry, verdict, "resolved", says) def library_version(self, emulator: str, ids: Sequence[str]) -> str | None: diff --git a/scripts/probes/findings.py b/scripts/probes/findings.py new file mode 100644 index 000000000..38000d2a8 --- /dev/null +++ b/scripts/probes/findings.py @@ -0,0 +1,14 @@ +"""The current state of each campaign finding: its latest line, and the statuses still open.""" + +from __future__ import annotations + +from pathlib import Path + +from model import measurements as mr + +OPEN_STATUSES = ("open", "flagged") + + +def latest_findings(root: Path) -> dict[str, dict]: + """Each finding key's latest line in the findings file: the last line written wins.""" + return {line["key"]: line for line in mr.read_campaign(root, mr.FINDINGS)} diff --git a/scripts/probes/reports.py b/scripts/probes/reports.py index d9a5b25a2..9365b5e90 100644 --- a/scripts/probes/reports.py +++ b/scripts/probes/reports.py @@ -10,6 +10,7 @@ import yaml from core.statefile import now_iso from model import measurements as mr from probes import rules +from probes.findings import OPEN_STATUSES, latest_findings from probes.profileedit import write_fields from probes.state import DONE, FAILED, PENDING @@ -69,11 +70,8 @@ def render_status(report: dict) -> str: def open_findings(root: Path, everything: bool = False) -> list[dict]: - latest: dict[str, dict] = {} - for line in mr.read_campaign(root, mr.FINDINGS): - latest[line["key"]] = line - return [line for key, line in sorted(latest.items()) - if everything or line["status"] in ("open", "flagged")] + return [line for key, line in sorted(latest_findings(root).items()) + if everything or line["status"] in OPEN_STATUSES] REVIEWED = ("applied", "rejected") @@ -92,11 +90,10 @@ def review_finding(root: Path, key: str, status: str, reason: str, raise ValueError(f"status {status!r}: one of {', '.join(REVIEWED)}") if not reason.strip(): raise ValueError("a review names its reason") - latest = {line["key"]: line for line in mr.read_campaign(root, mr.FINDINGS)} - finding = latest.get(key) + finding = latest_findings(root).get(key) if finding is None: raise ValueError(f"no finding {key}") - if finding["status"] not in ("open", "flagged"): + if finding["status"] not in OPEN_STATUSES: raise ValueError(f"{key} is {finding['status']}, not open") if updates and status != "applied": raise ValueError("only an applied review changes the profile") diff --git a/tests/sources.py b/tests/sources.py index 98f33a071..c9ce9c28c 100644 --- a/tests/sources.py +++ b/tests/sources.py @@ -162,6 +162,7 @@ COMMANDS = { "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", "probes/entries.py", "probes/campaignmodes.py", + "probes/findings.py", ), "standalone_probe.py": ( "probes/standaloneemulators.py", "probes/appimage.py", "probes/xvfb.py",