diff --git a/scripts/scraper/retrodeck_scraper.py b/scripts/scraper/retrodeck_scraper.py index 53c0531b0..0c6cadd54 100644 --- a/scripts/scraper/retrodeck_scraper.py +++ b/scripts/scraper/retrodeck_scraper.py @@ -57,7 +57,7 @@ from .base_scraper import ( systems_from_requirements, ) from .retrodeck_launchers import extract_launchers -from .romfolders.es import command_core, esde_systems +from .romfolders.esde import command_core, esde_systems PLATFORM_NAME = "retrodeck" COMPONENTS_REPO = "RetroDECK/components" diff --git a/scripts/scraper/romfolders/__init__.py b/scripts/scraper/romfolders/__init__.py index 617173106..df5304f81 100644 --- a/scripts/scraper/romfolders/__init__.py +++ b/scripts/scraper/romfolders/__init__.py @@ -150,7 +150,7 @@ def load_formats() -> dict[str, Parser]: """Import every parser module of this package once. Testing FORMATS for emptiness stopped short once a scraper had imported - one parser module on its own (retrodeck_scraper reads romfolders.es): + one parser module on its own (retrodeck_scraper reads romfolders.esde): the other formats were never registered. import_module returns a module already loaded at once, so every call goes through all of them. """ diff --git a/scripts/scraper/romfolders/es.py b/scripts/scraper/romfolders/es.py index da50655db..96f74700d 100644 --- a/scripts/scraper/romfolders/es.py +++ b/scripts/scraper/romfolders/es.py @@ -27,15 +27,10 @@ from __future__ import annotations import html import json import re -import shlex -from collections import Counter -from collections.abc import Iterator from model.romfolders import DEFAULT_ROW_SOURCE, format_ref from model.safeparse import parse_untrusted_xml, yaml_load_upstream -from ..retropie_modules import PLATFORM_FLAGS, module_enabled, module_fields -from ..recalbox_romfs import Defconfig, defconfig_boards, read_system_ini from . import Fetcher, SourceError, register @@ -368,595 +363,3 @@ def es_json(spec: dict, fetcher: Fetcher) -> list[dict]: "note": row.get("note"), }) return rows - - -# --------------------------------------------------------------------------- -# ROCKNIX family: config/emulators/.conf and the emulators package - - -def _conf_values(text: str) -> tuple[dict[str, str], dict[str, int]]: - values: dict[str, str] = {} - lines: dict[str, int] = {} - for number, line in enumerate(text.splitlines(), 1): - match = re.match(r"^\s*(SYSTEM_[A-Z_]+)=(.*)$", line) - if not match: - continue - raw = match.group(2).strip() - try: - value = " ".join(shlex.split(raw)) - except ValueError: - value = raw.strip('"') - values[match.group(1)] = value - lines[match.group(1)] = number - return values, lines - - -def _nesting(line: str, stack: list[list[str]]) -> bool: - """Follow case and if nesting. True when the line is consumed.""" - match = re.match(r'case\s+"?\$\{?(\w+)\}?"?\s+in', line) - if match: - stack.append(["case", match.group(1), ""]) - return True - if line == "esac": - stack.pop() - return True - if line.startswith("if ") or line.startswith("[") and "&&" not in line and line.endswith("then"): - stack.append(["if", line, ""]) - return True - if line in ("fi",): - if stack and stack[-1][0] == "if": - stack.pop() - return True - return line in ("then", "else") or line.startswith("elif ") - - -def _record_call( - words: list[str], number: int, stack: list[list[str]], - cores: list[dict], systems: dict[str, list[int]], -) -> None: - branches = [(s[1], s[2]) for s in stack] - if words[:1] == ["add_emu_core"] and len(words) >= 4: - cores.append({ - "system": words[1], "emulator": words[2], "core": words[3], - "default": len(words) > 4 and words[4] == "true", - "branches": branches, "line": number, - }) - elif words[:1] == ["add_es_system"] and len(words) >= 2: - systems.setdefault(words[1], []).append(number) - - -def _package_calls(text: str) -> tuple[list[dict], dict[str, list[int]]]: - """add_emu_core and add_es_system calls of makeinstall_target, each - with the `case` branches it sits under as (variable, pattern) pairs. - An `if` block nests a condition whose truth the build decides: its - calls carry it as a branch the default choice does not count as - unconditional.""" - cores: list[dict] = [] - systems: dict[str, list[int]] = {} - stack: list[list[str]] = [] - inside = False - for number, raw in enumerate(text.splitlines(), 1): - line = raw.split("#", 1)[0].strip() - if raw.startswith("makeinstall_target()"): - inside = True - continue - if not inside or not line: - continue - if raw.startswith("}"): - break - if _nesting(line, stack): - continue - ends = line.endswith(";;") - if ends: - line = line[:-2].strip() - if stack and stack[-1][0] == "case": - match = re.match(r"^([^()\s][^()]*)\)\s*(.*)$", line) - if match and not line.startswith(("add_", "[", "sed", "cp", "mkdir")): - stack[-1][2] = match.group(1).strip() - line = match.group(2).strip() - _record_call(line.split(), number, stack, cores, systems) - if ends and stack and stack[-1][0] == "case": - stack[-1][2] = "" - return cores, systems - - -def _board_key(branches: list[tuple[str, str]]) -> str | None: - """A default chosen under one `case` on the device names that device.""" - if len(branches) == 1 and branches[0][1] and branches[0][1] != "*": - variable, pattern = branches[0] - if not variable.startswith("["): - return f"{variable}={pattern}" - return None - - -@register("elec-conf") -def elec_conf(spec: dict, fetcher: Fetcher) -> list[dict]: - """A system ES lists is one add_es_system writes from its conf file - (SYSTEM_PATH, SYSTEM_EXTENSION). The default is the core whose - add_emu_core says true: unconditional, else the `*)` branch of a - device case, the branches for named devices going to board_defaults.""" - params = spec["params"] - key = params.get("source", DEFAULT_ROW_SOURCE) - package = params["package"] - cores, systems = _package_calls(fetcher.text(key, package)) - if not systems: - raise SourceError(f"{package}: no add_es_system call") - conf_dir = params["conf_dir"] - rows: list[dict] = [] - for name, lines in systems.items(): - conf_path = f"{conf_dir}/{name}.conf" - conf_text = fetcher.optional_text(key, conf_path) - if conf_text is None: - continue - values, value_lines = _conf_values(conf_text) - folder = _strip_prefix(values.get("SYSTEM_PATH", ""), params["path_prefixes"]) - if not folder: - continue - mine = [c for c in cores if c["system"] == name] - flagged = [c for c in mine if c["default"]] - plain = [c for c in flagged if not c["branches"]] - star = [c for c in flagged if c["branches"] and c["branches"][-1][1] == "*"] - candidates = plain or star or flagged - pick: dict | None = candidates[0] if candidates else None - if pick is None: - pick = next((c for c in mine if not c["branches"]), mine[0] if mine else None) - board_defaults = {} - for c in flagged: - board = _board_key(c["branches"]) - if c is not pick and board and (c["emulator"], c["core"]) != ( - pick and pick["emulator"], pick and pick["core"]): - board_defaults[board] = _board(c["core"], c["emulator"], format_ref(key, package, c["line"])) - refs = [ - format_ref(key, conf_path, value_lines.get("SYSTEM_PATH"), value_lines.get("SYSTEM_EXTENSION")), - format_ref(key, package, *lines), - ] - if pick: - refs.append(format_ref(key, package, pick["line"])) - note = None - if pick and pick["branches"] and not _board_key(pick["branches"]) and pick["branches"][-1][1] != "*": - note = "default core chosen under a build condition: " + ", ".join( - f"{v} {p}".strip() for v, p in pick["branches"]) - rows.append({ - "folder": folder, - "name": name, - "extensions": _extensions(values.get("SYSTEM_EXTENSION")), - "core": pick["core"] if pick else None, - "emulator": pick["emulator"] if pick else None, - "source_ref": "; ".join(refs), - "board_defaults": board_defaults, - "offers": _offers([ - {"emulator": c["emulator"], "core": c["core"], - "when": ", ".join(f"{v} {p}".strip() for v, p in c["branches"]), - "source_ref": format_ref(key, package, c["line"])} - for c in mine - ], pick["core"] if pick else None, pick["emulator"] if pick else None), - "arcade": values.get("SYSTEM_HARDWARE") == "arcade" or None, - "note": note, - }) - return rows - - -# --------------------------------------------------------------------------- -# ES-DE (EmuDeck, RetroDECK) - - -_RA_CORE = re.compile(r"%CORE_RETROARCH%[/\\]([\w.+-]+?)_libretro") -_EMULATOR = re.compile(r"%EMULATOR_([A-Za-z0-9_-]+)%") - - -def command_core(command: str) -> tuple[str | None, str | None]: - match = _RA_CORE.search(command) - if match: - return "retroarch", match.group(1) - match = _EMULATOR.search(command) - if match: - name = match.group(1).lower() - return name, name - return None, None - - -def esde_systems(text: str, path: str) -> Iterator[tuple[str, str, list[str], list[tuple[str, str]]]]: - root = parse_untrusted_xml(_lenient_xml(text), path) - for system in root.iter("system"): - name = (system.findtext("name") or "").strip() - rom_path = (system.findtext("path") or "").strip() - commands = [((c.get("label") or "").strip(), (c.text or "").strip()) - for c in system.findall("command")] - yield name, rom_path, _extensions(system.findtext("extension")), commands - - -def _alternative_emulators(text: str, function: str) -> dict[str, tuple[str, int]]: - """`ESDE_setEmu '