fix: refuse an unknown mame or fbneo version

This commit is contained in:
Abdessamad Derraz committed 2026-10-06 09:42:33 +02:00
1 parent a848730a80
commit a70ff264d2
3 files changed
+39 -18

No files matched your search

+5 -2
View File
@@ -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"],
+21 -16
View File
@@ -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:
+13
View File
@@ -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()