From 398782a5cd1ecd76a61f0a2cc59f8a641b9ada0a Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:21:25 +0100 Subject: [PATCH] fix: key the fbneo dat cache by commit fetch_pack refilled one fbneo-dats folder for whatever revision was asked, and the offline arcade import rebuilt its paths by hand: a parent map labelled with the profile's pin could be read from another commit's DATs. The folder is now fbneo-dats/, and cached_pack() next to fetch_pack is the offline read, raising NotCached. --- scripts/scraper/arcade_parent_importer.py | 17 ++--------- scripts/scraper/romset_dat_importer.py | 36 +++++++++++++++++++++-- tests/test_romset_recipes.py | 18 +++++++++++- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/scripts/scraper/arcade_parent_importer.py b/scripts/scraper/arcade_parent_importer.py index 9de40edaf..1a17c007f 100644 --- a/scripts/scraper/arcade_parent_importer.py +++ b/scripts/scraper/arcade_parent_importer.py @@ -61,7 +61,7 @@ from .fbneo_parser import zip_name from .clex import DriverSourceError, blank_comments, call_args, call_span from .listxml import Opener, attrs, iter_elements, open_listxml, rom_members from model.mamerelease import mame_tag -from .romset_dat_importer import MAME_LISTXML_URL, _api_json, fetch_pack +from .romset_dat_importer import MAME_LISTXML_URL, _api_json, cached_pack, fetch_pack SCHEMA_VERSION = 1 DEFAULT_OUTPUT = Path("games") / "arcade" @@ -953,12 +953,7 @@ def import_profile( openers: list[Opener] if kind == "listxml": tag = mame_tag(str(data["core_version"])) - if offline: - pack = cache_dir / f"{tag}lx.zip" - if not pack.is_file(): - raise FileNotFoundError(f"{pack} is not cached") - else: - pack, _ = fetch_pack("mame", tag, cache_dir) + pack, _ = (cached_pack if offline else fetch_pack)("mame", tag, cache_dir) source = { "release": f"mamedev/mame {tag}", "url": MAME_LISTXML_URL.format(tag=tag), @@ -966,13 +961,7 @@ def import_profile( } index = build_index(read_sets([lambda: open_listxml(pack)]), spec["marker"]) elif kind == "fbneo": - if offline: - folder = cache_dir / "fbneo-dats" - blobs = {p.name: git_blob_sha(p.read_bytes()) for p in sorted(folder.glob("*.dat"))} - if not blobs: - raise FileNotFoundError(f"{folder} is not cached") - else: - folder, blobs = fetch_pack("fbneo", commit, cache_dir) + folder, blobs = (cached_pack if offline else fetch_pack)("fbneo", commit, cache_dir) slug = _repo_slug(source_url) listing, listing_sha = ( _cached_repo_file(slug, commit, FBNEO_DRIVERLIST, cache_dir) diff --git a/scripts/scraper/romset_dat_importer.py b/scripts/scraper/romset_dat_importer.py index af4d2233c..24fc2dbb1 100644 --- a/scripts/scraper/romset_dat_importer.py +++ b/scripts/scraper/romset_dat_importer.py @@ -40,6 +40,7 @@ from datetime import datetime, timezone from pathlib import Path from typing import IO +from core.hashing import git_blob_sha from core.ziptools import is_archive_path, zip_members from model.registry import list_registered_platforms from model.profiles import load_emulator_profiles @@ -419,6 +420,20 @@ def fetch_tag(source: str, requested: str) -> str: return requested +class NotCached(FileNotFoundError): + """The cache holds no pack for the revision asked.""" + + +def _listxml_path(tag: str, cache_dir: Path) -> Path: + return cache_dir / f"{tag}lx.zip" + + +def _fbneo_folder(commit: str, cache_dir: Path) -> Path: + # One folder per commit: a folder refilled for whatever revision was + # last asked gave an offline read the DATs of another pin. + return cache_dir / "fbneo-dats" / commit + + def fetch_pack( source: str, tag: str, cache_dir: Path ) -> tuple[Path, dict[str, str]]: @@ -431,7 +446,7 @@ def fetch_pack( FBNeo, whose files change under a constant header version. """ if source == "mame": - listxml = cache_dir / f"{tag}lx.zip" + listxml = _listxml_path(tag, cache_dir) download(MAME_LISTXML_URL.format(tag=tag), listxml, skip_existing=True) return listxml, {} @@ -442,7 +457,7 @@ def fetch_pack( listing = _api_json(f"{FBNEO_API}/contents/dats?ref={sha}") if not isinstance(listing, list): raise BlobMismatchError(f"libretro/FBNeo lists no dats at {sha}") - target = cache_dir / "fbneo-dats" + target = _fbneo_folder(sha, cache_dir) target.mkdir(parents=True, exist_ok=True) blobs: dict[str, str] = {} for item in listing: @@ -460,6 +475,23 @@ def fetch_pack( return target, blobs +def cached_pack( + source: str, revision: str, cache_dir: Path +) -> tuple[Path, dict[str, str]]: + """What fetch_pack wrote for *revision* (a MAME tag, a full FBNeo commit), + read offline. The FBNeo blobs are recomputed from the bytes.""" + if source == "mame": + listxml = _listxml_path(revision, cache_dir) + if not listxml.is_file(): + 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"))} + if not blobs: + raise NotCached(f"{folder} is not cached") + return folder, blobs + + def _is_listxml(pack: Path) -> bool: """Whether *pack* holds a MAME -listxml document rather than a DAT. diff --git a/tests/test_romset_recipes.py b/tests/test_romset_recipes.py index f78a8bbfa..6845b74b3 100644 --- a/tests/test_romset_recipes.py +++ b/tests/test_romset_recipes.py @@ -931,7 +931,7 @@ class FbneoFetchReadsOneRevision(unittest.TestCase): self.served = FBNEO_DAT.replace(b"1.0.0.03", b"1.0.0.02") with self._patched(), self.assertRaises(self.importer.BlobMismatchError): self.importer.fetch_pack("fbneo", "latest", self.dir) - self.assertFalse((self.dir / "fbneo-dats" / "Demo.dat").exists()) + self.assertFalse((self.dir / "fbneo-dats" / "c0ffee" / "Demo.dat").exists()) def test_the_named_revision_is_the_one_listed(self): with self._patched(): @@ -943,6 +943,22 @@ class FbneoFetchReadsOneRevision(unittest.TestCase): ) self.assertEqual(list(blobs), ["Demo.dat"]) + def test_the_cache_is_keyed_by_the_commit(self): + with self._patched(): + target, blobs = self.importer.fetch_pack("fbneo", "v1.0.0.02", self.dir) + self.assertEqual(target, self.dir / "fbneo-dats" / "c0ffee") + self.assertEqual(self.importer.cached_pack("fbneo", "c0ffee", self.dir), (target, blobs)) + + def test_another_commit_is_not_cached(self): + with self._patched(): + self.importer.fetch_pack("fbneo", "latest", self.dir) + with self.assertRaises(self.importer.NotCached): + self.importer.cached_pack("fbneo", "da14132b", self.dir) + + def test_an_uncached_listxml_is_named(self): + with self.assertRaises(self.importer.NotCached): + self.importer.cached_pack("mame", "mame0289", self.dir) + def test_latest_reads_the_default_branch(self): with self._patched(): self.importer.fetch_pack("fbneo", "latest", self.dir)