refactor: one retropie package script selector

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 19:50:10 +01:00
1 parent 5a931ad4b1
commit ff3b2699bd
3 files changed
+49 -19

No files matched your search

+17 -10
View File
@@ -30,7 +30,7 @@ from collections.abc import Iterator
from .base_scraper import BaseScraper, BiosRequirement, fetch_branch_head, fetch_bytes
from .retropie_launchers import SCRIPTMODULES
from .retropie_launchers import SECTIONS as _SCRIPT_SECTIONS
from .retropie_launchers import SECTIONS as PACKAGE_SECTIONS
from .retropie_launchers import extract_launchers
from .retropie_modules import module_fields
@@ -44,9 +44,20 @@ MAX_ARCHIVE = 64 * 1024 * 1024
# SD image. The version a user runs is the one this script declares.
_VERSION_FILE = "retropie_packages.sh"
_VERSION = re.compile(r'^__version="([^"]+)"', re.MULTILINE)
# Sections RetroPie does not build as emulators: setup helpers, themes,
# drivers and the like carry no core.
_PACKAGE_DIRS = _SCRIPT_SECTIONS
def package_script(relative: str) -> str | None:
"""section/filename of a package script, None for any other member.
Only the PACKAGE_SECTIONS hold packages: setup helpers, themes, drivers
and the like carry no core. The platform and target scrapers both select
scripts here, so a targeted pack never filters a package the platform
list names.
"""
parts = relative.split("/")
if len(parts) == 3 and parts[0] == SCRIPTMODULES and parts[1] in PACKAGE_SECTIONS:
return f"{parts[1]}/{parts[2]}"
return None
def core_name(module_id: str) -> str:
@@ -107,13 +118,9 @@ class Scraper(BaseScraper):
for relative, text in script_members(raw):
parts = relative.split("/")
is_version_file = relative == _VERSION_FILE
is_package = (
len(parts) == 3
and parts[0] == "scriptmodules"
and parts[1] in _PACKAGE_DIRS
)
is_package = package_script(relative) is not None
is_launch_script = (
len(parts) == 4 and parts[0] == SCRIPTMODULES and parts[1] in _SCRIPT_SECTIONS
len(parts) == 4 and parts[0] == SCRIPTMODULES and parts[1] in PACKAGE_SECTIONS
)
if is_package or is_launch_script:
self._scripts[relative] = text
@@ -14,7 +14,14 @@ import tarfile
from datetime import datetime, timezone
from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields
from ..retropie_scraper import SOURCE_URL, core_name, fetch_archive, script_members
from ..retropie_scraper import (
PACKAGE_SECTIONS,
SOURCE_URL,
core_name,
fetch_archive,
package_script,
script_members,
)
from . import BaseTargetScraper, target_cli
PLATFORM_NAME = "retropie"
@@ -23,9 +30,6 @@ PLATFORM_NAME = "retropie"
GITHUB_API_URL = (
"https://api.github.com/repos/RetroPie/RetroPie-Setup/contents/scriptmodules"
)
# The sections the platform scraper reads: a standalone package missing here
# is filtered out of every targeted pack.
PACKAGE_DIRS = ("emulators", "libretrocores", "ports")
ARCH_MAP: dict[str, str] = {
"rpi1": "armv6",
@@ -66,12 +70,11 @@ class Scraper(BaseTargetScraper):
modules: dict[str, str] = {}
try:
for relative, text in script_members(_fetch_archive(url)):
parts = relative.split("/")
if len(parts) == 3 and parts[0] == "scriptmodules" and parts[1] in PACKAGE_DIRS:
modules[f"{parts[1]}/{parts[2]}"] = text
if name := package_script(relative):
modules[name] = text
except tarfile.TarError as exc:
raise RuntimeError(f"{url}: {exc}") from exc
for section in PACKAGE_DIRS:
for section in PACKAGE_SECTIONS:
if not any(name.startswith(f"{section}/") for name in modules):
raise RuntimeError(f"no scriptmodules under {section}/ in {url}")
return dict(sorted(modules.items()))
+21 -1
View File
@@ -19,6 +19,7 @@ import urllib.error
from pathlib import Path
from unittest import mock
from scripts.scraper import retropie_scraper
from scripts.scraper.retropie_modules import PLATFORM_FLAGS, module_enabled
from scripts.scraper.targets import (
batocera_targets_scraper,
@@ -709,6 +710,25 @@ class RetroPieModuleFlags(unittest.TestCase):
)
self.assertEqual(targets["x86_64"]["cores"], ["beetle_psx", "dosbox", "dosbox-staging"])
def test_platform_and_targets_select_the_same_packages(self):
# A section the platform list gains reaches every target list too,
# or --target filters its systems out of the pack without a word.
modules = {
"emulators/dosbox.sh": 'rp_module_id="dosbox"',
"libretrocores/lr-mgba.sh": 'rp_module_id="lr-mgba"',
"ports/openbor.sh": 'rp_module_id="openbor"',
"supplementary/runcommand.sh": 'rp_module_id="runcommand"',
}
archive = _retropie_tarball(modules)
sections = (*retropie_scraper.PACKAGE_SECTIONS, "supplementary")
with mock.patch.object(retropie_scraper, "PACKAGE_SECTIONS", sections), \
mock.patch.object(retropie_targets_scraper, "_fetch_archive", lambda _url: archive):
platform = retropie_scraper.Scraper(url="unused").module_ids(archive)
targets = retropie_targets_scraper.Scraper(revision="abc").fetch_targets()
listed = {retropie_scraper.core_name(module_id) for module_id in platform}
self.assertIn("runcommand", listed)
self.assertEqual(set(targets["targets"]["x86_64"]["cores"]), listed)
def test_a_section_without_scripts_stops_the_scrape(self):
archive = _retropie_tarball({"emulators/dosbox.sh": 'rp_module_id="dosbox"'})
with mock.patch.object(retropie_targets_scraper, "_fetch_archive", lambda _url: archive), \
@@ -751,7 +771,7 @@ class TargetsReadThePlatformRevision(unittest.TestCase):
seen: list[str] = []
archive = _retropie_tarball({
f"{section}/x.sh": 'rp_module_id="lr-mgba"'
for section in retropie_targets_scraper.PACKAGE_DIRS
for section in retropie_targets_scraper.PACKAGE_SECTIONS
})
def fetch(url):