From 4923873a7f72b1360b8ae6bd25de6cd310e25489 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Tue, 6 Oct 2026 09:42:33 +0200 Subject: [PATCH] fix: refuse an unknown mame or fbneo version --- scripts/scraper/fbneo_hash_scraper.py | 7 +++-- scripts/scraper/mame_hash_scraper.py | 37 +++++++++++++++------------ tests/test_target_scrapers.py | 13 ++++++++++ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/scripts/scraper/fbneo_hash_scraper.py b/scripts/scraper/fbneo_hash_scraper.py index ee97c176..07fa9718 100644 --- a/scripts/scraper/fbneo_hash_scraper.py +++ b/scripts/scraper/fbneo_hash_scraper.py @@ -117,8 +117,11 @@ def _extract_version() -> tuple[str, str]: if t["name"] != "latest" and t["name"].startswith("v"): version = t["name"] break - except (urllib.error.URLError, OSError): - pass + except (urllib.error.URLError, OSError) as exc: + raise RuntimeError(f"cannot determine the FBNeo version: {exc}") from exc + if version == "unknown": + # Written as core_version otherwise, and cached for a day. + raise RuntimeError("cannot determine the FBNeo version: no tag, resource.h or release") sha_result = subprocess.run( ["git", "rev-parse", "HEAD"], diff --git a/scripts/scraper/mame_hash_scraper.py b/scripts/scraper/mame_hash_scraper.py index 5b3581e8..ad8a7849 100644 --- a/scripts/scraper/mame_hash_scraper.py +++ b/scripts/scraper/mame_hash_scraper.py @@ -9,6 +9,7 @@ from __future__ import annotations import argparse import json +import os import logging import shutil import subprocess @@ -109,24 +110,28 @@ def _sparse_clone() -> None: def _get_version() -> str: - # version.cpp is generated at build time, not in the repo. - # Use GitHub API to get the latest release tag. + """The latest MAME release, from the GitHub API. + + version.cpp is generated at build time, not in the repo. A failed lookup + raises: "unknown" was cached for a day and written as core_version. + """ + headers = { + "User-Agent": "retrobios-scraper/1.0", + "Accept": "application/vnd.github.v3+json", + } + token = os.environ.get("GITHUB_TOKEN") + if token: + headers["Authorization"] = f"Bearer {token}" + url = "https://api.github.com/repos/mamedev/mame/releases/latest" try: - req = urllib.request.Request( - "https://api.github.com/repos/mamedev/mame/releases/latest", - headers={ - "User-Agent": "retrobios-scraper/1.0", - "Accept": "application/vnd.github.v3+json", - }, - ) + req = urllib.request.Request(url, headers=headers) with urllib.request.urlopen(req, timeout=10) as resp: - data = json.loads(resp.read()) - tag = data.get("tag_name", "") - if tag: - return _parse_version_tag(tag) - except (urllib.error.URLError, json.JSONDecodeError, OSError): - pass - return "unknown" + tag = json.loads(resp.read()).get("tag_name", "") + except (urllib.error.URLError, json.JSONDecodeError, OSError) as exc: + raise RuntimeError(f"cannot read the MAME release from {url}: {exc}") from exc + if not tag: + raise RuntimeError(f"no tag_name in {url}") + return _parse_version_tag(tag) def _parse_version_tag(tag: str) -> str: diff --git a/tests/test_target_scrapers.py b/tests/test_target_scrapers.py index d0f185d6..d61d3ec5 100644 --- a/tests/test_target_scrapers.py +++ b/tests/test_target_scrapers.py @@ -113,5 +113,18 @@ class RetroPieModuleFlags(unittest.TestCase): self.assertEqual(targets["x86_64"]["cores"], ["beetle_psx", "dosbox", "dosbox-staging"]) + +class HashScrapersRefuseAnUnknownVersion(unittest.TestCase): + """mame_hash_scraper wrote core_version "unknown" after a failed API call + and cached it for a day; the pipeline step still read OK.""" + + def test_mame_raises(self): + from scripts.scraper import mame_hash_scraper # noqa: PLC0415 + + with mock.patch.object(mame_hash_scraper.urllib.request, "urlopen", _refuse), \ + self.assertRaises(RuntimeError): + mame_hash_scraper._get_version() + + if __name__ == "__main__": unittest.main()