refactor: freshness asks the importer its dat rule

This commit is contained in:
Abdessamad Derraz committed 2026-11-24 03:38:59 +01:00
1 parent c0d85cca0b
commit a5425d69cd
4 files changed
+42 -14

No files matched your search

+4 -9
View File
@@ -12,6 +12,7 @@ from pathlib import Path
from core import upstream
from core.paths import REPO_ROOT
from scripts.scraper import game_dat_importer
from scripts.scraper.datfetch import is_tracked_dat, label_version
from scripts.scraper.redump_dat_scraper import fetch_snapshot
from freshness.findings import (
FBNEO_RECIPES, MAME_RECIPES, NO_INTRO_GAMES, OK, REDUMP_GAMES, SKIPPED, STALE, UNKNOWN,
@@ -211,12 +212,8 @@ def _check_tosec(offline: bool) -> Finding:
def mame_versions(recipes: dict) -> list[str]:
"""MAME version tags a recipe snapshot has absorbed, oldest first."""
tags = set()
for label in (recipes.get("dats") or {}):
match = re.search(r"\b(mame\d{4})\b", str(label))
if match:
tags.add(match.group(1))
return sorted(tags)
tags = {label_version(str(label)) for label in (recipes.get("dats") or {})}
return sorted(tag for tag in tags if tag)
def _check_mame(cache_dir: str, offline: bool) -> Finding:
@@ -249,9 +246,7 @@ def fbneo_blob_drift(snapshot: dict, listing: object) -> tuple[list[str], bool]:
remote = {
item["name"]: item.get("sha", "")
for item in (listing if isinstance(listing, list) else [])
if isinstance(item, dict)
and item.get("type") == "file"
and str(item.get("name", "")).lower().endswith(".dat")
if is_tracked_dat(item)
}
drifted = sorted(
name for name in set(recorded) | set(remote) if recorded.get(name) != remote.get(name)
+27 -2
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
import re
from pathlib import Path
from core.hashing import git_blob_sha
@@ -25,6 +26,26 @@ def api_json(url: str) -> object:
return github_json(url, f"cannot read {url}", timeout=60, error=ConnectionError)
def is_tracked_dat(item: object) -> bool:
"""Whether an entry of the FBNeo ``dats`` listing is a DAT the import reads."""
return (
isinstance(item, dict)
and item.get("type") == "file"
and str(item.get("name", "")).lower().endswith(".dat")
)
def mame_label(pack_stem: str) -> str:
"""The snapshot label of a MAME listxml pack (``mame0289lx`` gives ``MAME mame0289``)."""
return f"MAME {pack_stem.replace('lx', '')}"
def label_version(label: str) -> str | None:
"""The MAME tag a snapshot label names, None for a label without one."""
match = re.search(r"\b(mame\d{4})\b", label)
return match.group(1) if match else None
class NotCached(FileNotFoundError):
"""The cache holds no pack for the revision asked."""
@@ -66,7 +87,7 @@ def fetch_pack(
target.mkdir(parents=True, exist_ok=True)
blobs: dict[str, str] = {}
for item in listing:
if item.get("type") != "file" or not item["name"].lower().endswith(".dat"):
if not is_tracked_dat(item):
continue
fetch_verified_blob(
item["download_url"], target / item["name"], item["sha"],
@@ -91,7 +112,11 @@ def cached_pack(
raise NotCached(f"{listxml} is not cached")
return listxml, {}
folder = _fbneo_folder(revision, cache_dir)
blobs = {p.name: git_blob_sha(p.read_bytes()) for p in sorted(folder.glob("*.dat"))}
blobs = {
p.name: git_blob_sha(p.read_bytes())
for p in (sorted(folder.iterdir()) if folder.is_dir() else ())
if is_tracked_dat({"type": "file" if p.is_file() else "dir", "name": p.name})
}
if not blobs:
raise NotCached(f"{folder} is not cached")
return folder, blobs
+2 -2
View File
@@ -50,7 +50,7 @@ from model.database import load_database
from model.dumpcatalog import build_snapshot, load_provenance_snapshots, write_snapshot
from resolve.localfile import variant_base_name
from .base_scraper import BlobMismatchError, fetch_github_latest_version
from .datfetch import fetch_pack
from .datfetch import fetch_pack, mame_label
from .listxml import (
Opener,
iter_dat_documents,
@@ -475,7 +475,7 @@ def _selection(args: argparse.Namespace) -> _Selection:
def _import_listxml(pack: Path, args: argparse.Namespace, output: str, chosen: _Selection) -> int:
label = f"MAME {pack.stem.replace('lx', '')}"
label = mame_label(pack.stem)
entries = listxml_entries(
lambda: open_listxml(pack), label, chosen.keep, chosen.targets, chosen.by_members
)
+9 -1
View File
@@ -23,6 +23,7 @@ from core import upstream
from core.cli import InputMissing
from exporter.native import ExportResult
from exporter.originals import SOURCES_INDEX
import freshness.catalogs
from freshness.catalogs import (
NO_INTRO_MIRROR,
_check_no_intro,
@@ -70,7 +71,7 @@ from model.romfolders import load_sources, sources_dir
from model.safeparse import yaml_load
from model.targets import platform_overrides
from profiling.reports import ProfileReport, SkipKind, report_to_dict
from scripts.scraper import game_dat_importer
from scripts.scraper import datfetch, game_dat_importer
from tests.sources import SCRIPTS
REPO_ROOT = Path(__file__).resolve().parents[1]
@@ -646,6 +647,13 @@ class CatalogParsingTests(unittest.TestCase):
recipes = {"dats": {"MAME mame0250": "0.250", "MAME mame0289": "0.289", "FinalBurn Neo": "x"}}
self.assertEqual(mame_versions(recipes), ["mame0250", "mame0289"])
def test_versions_and_blobs_follow_the_importer_rules(self):
recipes = {"dats": {datfetch.mame_label("mame0289lx"): "mame0289lx"}}
self.assertEqual(mame_versions(recipes), ["mame0289"])
# The check asks the importer which DATs it reads, never a copy of the rule.
self.assertIs(freshness.catalogs.is_tracked_dat, datfetch.is_tracked_dat)
self.assertIs(freshness.catalogs.label_version, datfetch.label_version)
def _rows(*reports: ProfileReport) -> list[dict]:
"""The JSON rows profile_sync --json writes for these reports."""