From 768ac05b4dfb9a8c6c0832bf64ac6bcacb6171b6 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:35:24 +0100 Subject: [PATCH] fix: compare redump datfiles by their file stamp --- scripts/freshness/catalogs.py | 13 +++++++----- scripts/scraper/game_dat_importer.py | 22 +++++++++++++++++--- tests/test_check_freshness.py | 31 ++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/scripts/freshness/catalogs.py b/scripts/freshness/catalogs.py index 26ff57e19..ac22691ef 100644 --- a/scripts/freshness/catalogs.py +++ b/scripts/freshness/catalogs.py @@ -111,18 +111,21 @@ def redump_datfile_version(code: str) -> str: disposition = upstream.open_retrying( request, lambda response: response.headers.get("Content-Disposition", ""), timeout=60 ) - named = game_dat_importer.DISPOSITION.search(disposition) - match = game_dat_importer.REDUMP_FILE.match(named["file"]) if named else None - if not match: + version = game_dat_importer.served_version(game_dat_importer.datfile_name(disposition) or "") + if not version: raise ValueError(f"{url} names no datfile") - return f"{match['date']} {match['time']}" + return version def _check_redump_games(offline: bool) -> Finding: """games/catalogs/redump/ against the datfiles redump.info serves now. A disc dumped since is not recognised by the games-on-card mode.""" subject = REDUMP_GAMES - local = {doc.get("system"): str(doc.get("version", "")) for doc in _game_catalogs("redump")} + # The stamp of the file the import read, the one the server names now. + local = { + doc.get("system"): game_dat_importer.served_version(str((doc.get("archive") or {}).get("file", ""))) + for doc in _game_catalogs("redump") + } newest = max(local.values(), default="") if offline: return Finding("catalogs", subject, SKIPPED, local=newest, detail="offline") diff --git a/scripts/scraper/game_dat_importer.py b/scripts/scraper/game_dat_importer.py index 3138c5bd3..2f9fe2f2e 100644 --- a/scripts/scraper/game_dat_importer.py +++ b/scripts/scraper/game_dat_importer.py @@ -167,6 +167,22 @@ REDUMP_FILE = re.compile( ) +def datfile_name(disposition: str) -> str | None: + """The file name a redump.info Content-Disposition header gives a datfile.""" + match = DISPOSITION.search(disposition) + return match["file"] if match else None + + +def served_version(file: str) -> str: + """The version redump.info stamps in a datfile name, empty for another name. + + The import records the name, and the freshness check reads the name the + server gives now: both sides compare this stamp, never the DAT header. + """ + match = REDUMP_FILE.match(file) + return f"{match['date']} {match['time']}" if match else "" + + def name_regions(name: str) -> list[str] | None: """Region slugs of the first parenthesised group made only of territory words, or None when the name carries no such group or a word the @@ -299,10 +315,10 @@ def fetch_redump(code: str, cache_dir: Path, offline: bool, refresh: bool) -> tu if offline: raise NotCachedError(f"{pack} is not cached") url = REDUMP_DATFILE_URL.format(code=code) - match = DISPOSITION.search(download(url, pack)) - if not match: + file = datfile_name(download(url, pack)) + if not file: raise RuntimeError(f"{url} answered without a datfile name") - meta = {"file": match["file"], "url": url, "fetched": _today()} + meta = {"file": file, "url": url, "fetched": _today()} write_text_atomic(str(meta_path), json.dumps(meta, indent=1) + "\n") return pack, meta diff --git a/tests/test_check_freshness.py b/tests/test_check_freshness.py index c2d660c2b..e81f9bcb8 100644 --- a/tests/test_check_freshness.py +++ b/tests/test_check_freshness.py @@ -534,8 +534,9 @@ class CatalogParsingTests(unittest.TestCase): had no row: only the BIOS snapshots of provenance/ were compared.""" versions = {code: "2026-01-01 00-00-00" for code in game_dat_importer.REDUMP_DATS} versions["PSX"] = "2099-01-01 00-00-00" - local = [{"system": system, "version": "2026-01-01 00-00-00"} - for _n, system, _f in game_dat_importer.REDUMP_DATS.values()] + local = [{"system": system, + "archive": {"file": f"{name} - Datfile (1) (2026-01-01 00-00-00).zip"}} + for name, system, _f in game_dat_importer.REDUMP_DATS.values()] with mock.patch("freshness.catalogs._game_catalogs", return_value=local), \ mock.patch("freshness.catalogs.redump_datfile_version", side_effect=versions.get): row = _check_redump_games(False) @@ -547,6 +548,32 @@ class CatalogParsingTests(unittest.TestCase): with mock.patch("freshness.catalogs._no_intro_asset_date", return_value=rebuilt): self.assertEqual(_check_nointro_games("", False).status, status) + def test_redump_compares_the_file_stamp_not_the_header(self): + """The header version and the file name stamp agree only because + redump writes the same stamp twice. Compared with the header, a + header that differed would keep every system STALE through any + refresh.""" + stamp = "2026-11-01 16-08-06" + versions = {code: stamp for code in game_dat_importer.REDUMP_DATS} + local = [{"system": system, "version": "a header written another way", + "archive": {"file": f"{name} - Datfile (5) ({stamp}).zip"}} + for name, system, _f in game_dat_importer.REDUMP_DATS.values()] + with mock.patch("freshness.catalogs._game_catalogs", return_value=local), \ + mock.patch("freshness.catalogs.redump_datfile_version", side_effect=versions.get): + self.assertEqual(_check_redump_games(False).status, OK) + self.assertEqual(game_dat_importer.datfile_name( + 'attachment; filename="GC - Datfile (5) (2026-11-01 16-08-06).zip"'), + "GC - Datfile (5) (2026-11-01 16-08-06).zip") + self.assertIsNone(game_dat_importer.datfile_name("attachment")) + self.assertEqual(game_dat_importer.served_version("other.zip"), "") + + def test_every_redump_catalog_records_a_stamped_file(self): + committed = sorted((REPO_ROOT / "games" / "catalogs" / "redump").glob("*.json")) + unstamped = [path.name for path in committed + if not game_dat_importer.served_version( + json.loads(path.read_text(encoding="utf-8"))["archive"]["file"])] + self.assertEqual(unstamped, []) + def test_a_mirror_that_stopped_rebuilding_is_stale(self): """The mirror failed every daily rebuild from 2026-07-08, and both No-Intro rows read OK: an import newer than a dead pack is no