feat: read saved findings in refresh_stale

--findings FILE takes a check_freshness.py --json report written earlier, kept to the --only areas, so one full check can drive a refresh of a few areas.
This commit is contained in:
Abdessamad Derraz committed 2026-10-27 10:40:05 +01:00
1 parent 2439df40f7
commit 79c27b3798
2 files changed
+45 -1

No files matched your search

+22 -1
View File
@@ -44,6 +44,7 @@ Usage:
python scripts/refresh_stale.py --dry-run
python scripts/refresh_stale.py --only platforms,targets
python scripts/refresh_stale.py --jobs 6
python scripts/refresh_stale.py --findings tmp/freshness.json --only data
"""
from __future__ import annotations
@@ -159,6 +160,17 @@ def _run_check_freshness(areas: tuple[str, ...], extra: list[str]) -> list[dict]
raise RuntimeError(f"check_freshness.py returned unreadable JSON: {exc}") from exc
def load_findings(path: Path, areas: tuple[str, ...]) -> list[dict]:
"""A saved check_freshness.py --json report, kept to the asked areas."""
try:
findings = json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError) as exc:
raise RuntimeError(f"cannot read findings from {path}: {exc}") from exc
if not isinstance(findings, list):
raise RuntimeError(f"{path} is not a check_freshness.py --json report")
return [f for f in findings if f.get("area") in areas]
def _slug(subject: str) -> str:
"""A safe filename stem for a finding's subject."""
return re.sub(r"[^A-Za-z0-9_.-]+", "_", subject).strip("_") or "job"
@@ -565,6 +577,12 @@ def main() -> int:
action="store_true",
help="machine-readable summary (jobs, surfaced, ignored)",
)
parser.add_argument(
"--findings",
metavar="FILE",
help="read the check_freshness.py --json report from FILE instead of "
"running it (rows outside --only are left out)",
)
parser.add_argument(
"--freshness-arg",
action="append",
@@ -585,7 +603,10 @@ def main() -> int:
areas = all_areas
try:
findings = _run_check_freshness(areas, args.freshness_arg)
if args.findings:
findings = load_findings(Path(args.findings), areas)
else:
findings = _run_check_freshness(areas, args.freshness_arg)
except RuntimeError as exc:
print(exc, file=sys.stderr)
return 2
+23
View File
@@ -206,5 +206,28 @@ class Running(unittest.TestCase):
self.assertFalse(marker.exists())
class SavedFindings(unittest.TestCase):
"""--findings reads the report a full check_freshness run saved, so the
nightly job checks every area once and refreshes only some."""
def test_rows_outside_the_asked_areas_are_left_out(self):
with tempfile.TemporaryDirectory(dir=REPO_ROOT / "tmp" / "tests") as directory:
path = Path(directory) / "freshness.json"
path.write_text(
'[{"area": "data", "subject": "dolphin-sys", "status": "STALE"},'
' {"area": "platforms", "subject": "batocera", "status": "STALE"}]',
encoding="utf-8",
)
findings = rs.load_findings(path, ("data", "catalogs"))
self.assertEqual([f["subject"] for f in findings], ["dolphin-sys"])
def test_a_report_that_is_not_a_list_is_refused(self):
with tempfile.TemporaryDirectory(dir=REPO_ROOT / "tmp" / "tests") as directory:
path = Path(directory) / "freshness.json"
path.write_text('{"area": "data"}', encoding="utf-8")
with self.assertRaises(RuntimeError):
rs.load_findings(path, ("data",))
if __name__ == "__main__":
unittest.main()