From eec854cc0bb13b3ef8a8c0d4d10f681509b16550 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:59:57 +0100 Subject: [PATCH] fix: resolve coreinfo --compare-db like the packs --- scripts/scraper/coreinfo_coverage.py | 30 ++++++++++++++ scripts/scraper/coreinfo_scraper.py | 11 ++---- tests/test_scraper_contract.py | 59 ++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 scripts/scraper/coreinfo_coverage.py diff --git a/scripts/scraper/coreinfo_coverage.py b/scripts/scraper/coreinfo_coverage.py new file mode 100644 index 000000000..a99566cd8 --- /dev/null +++ b/scripts/scraper/coreinfo_coverage.py @@ -0,0 +1,30 @@ +"""Which core-info firmware the collection holds, by the resolver's rule.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from resolve.localfile import resolve_local_file + +if TYPE_CHECKING: + from .base_scraper import BiosRequirement + + +def is_held(req: BiosRequirement, db: dict) -> bool: + """Whether the collection holds *req*: its md5 first, its name only + when the .info notes declare none.""" + basename = req.destination.rsplit("/", 1)[-1] or req.name + entry = {"name": basename, "md5": req.md5, "destination": req.destination} + local, status = resolve_local_file(entry, db, dest_hint=req.destination) + return local is not None and status not in ("not_found", "hash_mismatch") + + +def split_by_holding( + reqs: list[BiosRequirement], db: dict, +) -> tuple[list[BiosRequirement], list[BiosRequirement]]: + """The requirements the collection holds, then those it lacks.""" + found: list[BiosRequirement] = [] + missing: list[BiosRequirement] = [] + for req in reqs: + (found if is_held(req, db) else missing).append(req) + return found, missing diff --git a/scripts/scraper/coreinfo_scraper.py b/scripts/scraper/coreinfo_scraper.py index 5d7bcabf9..d87460579 100644 --- a/scripts/scraper/coreinfo_scraper.py +++ b/scripts/scraper/coreinfo_scraper.py @@ -34,6 +34,7 @@ from .base_scraper import ( _print_dry_run, github_tree, ) +from .coreinfo_coverage import split_by_holding PLATFORM_NAME = "libretro_coreinfo" @@ -314,16 +315,10 @@ def main() -> None: if args.compare_db: db = load_database(args.compare_db) - found = 0 - missing = [] - for r in reqs: - if r.name in db["indexes"]["by_name"] or r.md5 and r.md5 in db["indexes"]["by_md5"]: - found += 1 - else: - missing.append(r) + found, missing = split_by_holding(reqs, db) print(f"Core-info: {len(reqs)} unique firmware paths") - print(f"Found in DB: {found}") + print(f"Found in DB: {len(found)}") print(f"Missing: {len(missing)}") if missing: print("\nMissing files:") diff --git a/tests/test_scraper_contract.py b/tests/test_scraper_contract.py index ed0eb4e15..49bed2867 100644 --- a/tests/test_scraper_contract.py +++ b/tests/test_scraper_contract.py @@ -908,6 +908,65 @@ class UnreadableReleaseStopsTheScrape(unittest.TestCase): scraper.fetch_requirements() +class CoreInfoCompareDbResolvesLikeTheCollection(unittest.TestCase): + """--compare-db counted a firmware held as soon as any homonym existed, + whatever md5 the .info notes gave, and a generic name written with its + directory (SGB1.sfc/program.rom) missing although the collection held it + at that path.""" + + def setUp(self) -> None: + self._tmp = tempfile.TemporaryDirectory() + self.root = Path(self._tmp.name) + self.db: dict = {"files": {}, "indexes": { + "by_md5": {}, "by_name": {}, "by_crc32": {}, "by_path_suffix": {}, + }} + + def tearDown(self) -> None: + self._tmp.cleanup() + + def _hold(self, rel: str, data: bytes) -> str: + import hashlib + + path = self.root / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + sha1 = hashlib.sha1(data).hexdigest() + md5 = hashlib.md5(data).hexdigest() + self.db["files"][sha1] = { + "path": str(path), "name": path.name, "md5": md5, "sha1": sha1, + "size": len(data), + } + indexes = self.db["indexes"] + indexes["by_md5"][md5] = sha1 + indexes["by_name"].setdefault(path.name, []).append(sha1) + if "/" in rel: + indexes["by_path_suffix"].setdefault(rel, []).append(sha1) + return md5 + + def test_a_homonym_does_not_hold_a_declared_md5(self): + from scraper.coreinfo_coverage import split_by_holding + + self._hold("boot.rom", b"some other boot rom") + req = BiosRequirement(name="boot.rom", system="x", md5="0" * 32, + destination="boot.rom") + self.assertEqual(split_by_holding([req], self.db), ([], [req])) + + def test_a_generic_name_held_at_its_path_is_found(self): + from scraper.coreinfo_coverage import split_by_holding + + self._hold("SGB1.sfc/program.rom", b"sgb1 program") + req = BiosRequirement(name="SGB1.sfc/program.rom", system="x", + destination="SGB1.sfc/program.rom") + self.assertEqual(split_by_holding([req], self.db), ([req], [])) + + def test_the_declared_md5_is_found(self): + from scraper.coreinfo_coverage import split_by_holding + + md5 = self._hold("bios.bin", b"the right dump") + req = BiosRequirement(name="bios.bin", system="x", md5=md5, + destination="bios.bin") + self.assertEqual(split_by_holding([req], self.db), ([req], [])) + class EveryGitHubApiCallerSendsTheToken(unittest.TestCase): """romset_dat_importer and coreinfo_scraper called api.github.com with no