refactor: read finding state in one probes module

This commit is contained in:
Abdessamad Derraz committed 2026-11-23 21:02:47 +01:00
1 parent 6b1798cb97
commit 42322a2fd9
4 files changed
+24 -17

No files matched your search

+4 -9
View File
@@ -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:
+14
View File
@@ -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)}
+5 -8
View File
@@ -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")
+1
View File
@@ -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",