refactor: give each installer module one role

The split had pushed each helper down into the lowest module using it.
Names now sit with the concern they belong to (68 modules, 45 before),
each with a one-line docstring saying it, and the import graph stays
acyclic. Statement source and comments moved unchanged: every top-level
statement of install.py keeps an identical ast.dump.
This commit is contained in:
Abdessamad Derraz committed 2026-11-18 02:55:56 +01:00
1 parent 9b0c7a390c
commit 6b597f85a0
78 files changed
+30571 -30127

No files matched your search

+73 -295
View File
@@ -1,20 +1,19 @@
"""Libretro cores installed, their info files, and the names people type for platforms and systems."""
"""Libretro cores installed here, the firmware their info files declare, and --cores-dir."""
from __future__ import annotations
import errno
import os
import re
import sys
import tempfile
from pathlib import Path, PurePosixPath
from typing import NoReturn
from pathlib import Path
from installer.constants import AVAILABLE_PLATFORMS
from installer.detect_os import _NO_FOLDER, _retroarch_cfg_dir
from installer.embedded import _EMBEDDED_RULES, _name_key, display_name, shown_path
from installer.config_values import _retroarch_cfg_dir
from installer.events import EVENTS
from installer.local_files import _retroarch_config_for
from installer.probe import _exists, _is_dir, _is_file
from installer.frontend_folders import _retroarch_config_for
from installer.host_fs import _is_file, closed_folder
from installer.invocation import _INVOCATION
from installer.local_check import closed_folder_words
from installer.platforms import display_name
from installer.text import _plural, screen_text
# A libretro core as RetroArch loads it: <name>_libretro.so|dll|dylib, and
# <name>_libretro_android.so on Android.
@@ -54,112 +53,6 @@ def core_dirs(plat: str, bios_path: Path) -> list[Path]:
return list(dict.fromkeys(dirs))
def keeps_case(directory: Path) -> bool:
"""Whether names differing only by case are distinct files there.
Probed on the nearest existing directory, since the filesystem decides,
not the operating system: an ext4 drive mounted on macOS keeps case and
a casefolded ext4 directory folds it. Unprobeable: the system's default.
"""
probe_dir = directory
while not _is_dir(probe_dir) and probe_dir != probe_dir.parent:
probe_dir = probe_dir.parent
try:
handle, name = tempfile.mkstemp(prefix=".retrobios-case-", dir=str(probe_dir))
except OSError:
return sys.platform not in ("win32", "darwin")
os.close(handle)
try:
twin = os.path.join(os.path.dirname(name), os.path.basename(name).upper())
return not os.path.exists(twin)
finally:
os.unlink(name)
def fold_case_twins(files: list[dict]) -> tuple[list[dict], list[str]]:
"""One file per name where the directory folds case, the others named.
The first in manifest order stays, as the pack's own order places it.
"""
kept: list[dict] = []
seen: set[str] = set()
folded: list[str] = []
for entry in files:
key = str(entry.get("dest", "")).casefold()
if key in seen:
folded.append(str(entry.get("dest", "")))
continue
seen.add(key)
kept.append(entry)
return kept, folded
# The characters FAT and exFAT refuse in a name, as Windows does on any drive
# (Linux 7475ea8b, fs/fat/namei_vfat.c:201-207 and fs/exfat/nls.c:396-403;
# "Naming Files, Paths, and Namespaces" in the Win32 documentation). The
# slash, the backslash and the controls never reach a destination
# (_safe_relative_path). RetroDECK names three files zxs:48.rom,
# zxs:if1-2.rom and snes:st018.data.rom (its retroarch component manifest):
# on an exFAT card each write failed, on every run.
FAT_REFUSED = re.compile(r'[<>:"|?*]')
FAT_REFUSED_SHOWN = '< > : " | ? *'
def holds_fat_refused_names(directory: Path) -> bool:
"""Whether a name holding a colon can be created there.
Probed on the nearest existing directory, as keeps_case is: an ext4
card on a Steam Deck holds one, an exFAT card refuses it with EINVAL.
Windows refuses it on every drive. A probe refused for another cause
(a folder this user cannot write) decides nothing.
"""
if os.name == "nt":
return False
probe_dir = directory
while not _is_dir(probe_dir) and probe_dir != probe_dir.parent:
probe_dir = probe_dir.parent
try:
handle, name = tempfile.mkstemp(prefix=".retrobios-name-", suffix=":",
dir=str(probe_dir))
except OSError as exc:
return exc.errno != errno.EINVAL
os.close(handle)
os.unlink(name)
return True
def split_fat_refused(files: list[dict]) -> tuple[list[dict], list[str]]:
"""The entries whose destination a FAT, exFAT or Windows folder can
hold, and the destinations it cannot."""
kept = [entry for entry in files if not FAT_REFUSED.search(str(entry.get("dest", "")))]
refused = [str(entry.get("dest", "")) for entry in files
if FAT_REFUSED.search(str(entry.get("dest", "")))]
return kept, refused
# Android's media scanner marks a folder holding a .nomedia file hidden, and
# every folder below it (MediaProvider a183e2b9, FileUtils.java:1587-1629
# isDirectoryHidden, ModernMediaScanner.java:845-846 and 1017-1020). Without
# one, the samples, overlays and videos of a BIOS folder in shared storage
# showed in the phone's gallery and music apps. RetroArch writes none
# (libretro-common/file/file_path_io.c:194 only skips one).
NOMEDIA = ".nomedia"
def hide_from_media_scanner(folder: Path) -> bool:
"""Leave an empty .nomedia in folder, True when this call wrote it."""
marker = folder / NOMEDIA
if not _is_dir(folder) or _exists(marker):
return False
try:
marker.touch()
except OSError as exc:
print(f" {NOMEDIA} not written in {shown_path(folder)}: {exc.strerror}",
file=sys.stderr)
return False
return True
def installed_cores(dirs: list[Path]) -> list[str]:
"""The cores present in these directories, by the name the buildbot uses.
@@ -247,190 +140,75 @@ def unknown_cores(manifest: dict, names: list[str]) -> list[str]:
return sorted(name for name in names if name not in known)
# A list this short is printed whole rather than pointed at.
SHORT_LIST = 12
def _check_cores_dir(folder: Path) -> None:
"""Refuse a --cores-dir that cannot be listed, saying why: a typo
(core/ for cores/) or a closed folder read as an empty one, and the
run said "none in" and asked for the --cores-dir it was given."""
try:
with os.scandir(folder):
return
except FileNotFoundError:
why = "does not exist"
except NotADirectoryError:
why = "is not a folder"
except PermissionError as exc:
closed = closed_folder(folder)
why = (f"cannot be opened by this user ({exc.strerror or exc})"
if closed is None or closed[0] == folder
else f"cannot be opened: {closed_folder_words(folder, closed)}")
except OSError as exc:
why = f"could not be read ({exc.strerror or exc})"
print(f"Error: --cores-dir {screen_text(str(folder))} {why}.", file=sys.stderr)
print(" Name the folder RetroArch loads cores from (libretro_directory in "
"retroarch.cfg, cores/ beside RetroArch by default).", file=sys.stderr)
sys.exit(1)
# Names people type for a system, each with its id. Only ever suggested:
# --system gba, psx and ps1 were refused with no hint on RetroArch, whose
# list names no system that way, and taking one in silence could install
# another console than the one meant.
SYSTEM_SHORT_NAMES = {
**dict.fromkeys(("psx", "ps1", "ps", "playstation1"), "sony-playstation"),
"ps2": "sony-playstation-2", "ps3": "sony-playstation-3", "psp": "sony-psp",
**dict.fromkeys(("vita", "psvita"), "sony-playstation-vita"),
"gb": "nintendo-gb", "gbc": "nintendo-gbc", "gba": "nintendo-gba",
**dict.fromkeys(("nes", "famicom", "fc"), "nintendo-nes"), "fds": "nintendo-fds",
**dict.fromkeys(("snes", "sfc", "superfamicom"), "nintendo-snes"),
"n64": "nintendo-64", "n64dd": "nintendo-64dd", "sgb": "nintendo-sgb",
**dict.fromkeys(("nds", "ds"), "nintendo-ds"), "dsi": "nintendo-dsi",
"3ds": "nintendo-3ds", "wiiu": "nintendo-wii-u", "switch": "nintendo-switch",
**dict.fromkeys(("gc", "ngc", "gcn", "gamecube"), "nintendo-gamecube"),
"pokemini": "nintendo-pokemon-mini",
**dict.fromkeys(("md", "gen", "genesis", "megadrive"), "sega-mega-drive"),
**dict.fromkeys(("scd", "segacd", "megacd"), "sega-mega-cd"),
"32x": "sega-32x", **dict.fromkeys(("sms", "mastersystem"), "sega-master-system"),
**dict.fromkeys(("gg", "gamegear"), "sega-game-gear"),
**dict.fromkeys(("ss", "saturn"), "sega-saturn"),
**dict.fromkeys(("dc", "dreamcast"), "sega-dreamcast"),
**dict.fromkeys(("naomi", "atomiswave"), "sega-dreamcast-arcade"),
**dict.fromkeys(("pce", "tg16", "turbografx", "turbografx16", "pcengine",
"supergrafx"), "nec-pc-engine"),
**dict.fromkeys(("pcecd", "tgcd", "turbografxcd"), "nec-pc-engine-cd"),
"pcfx": "nec-pc-fx",
**dict.fromkeys(("neogeo", "mvs", "aes"), "snk-neogeo"),
**dict.fromkeys(("neocd", "neogeocd"), "snk-neogeo-cd"),
**dict.fromkeys(("msx", "msx2"), "microsoft-msx"),
"c64": "commodore-c64", "c128": "commodore-c128",
**dict.fromkeys(("amiga", "cd32"), "commodore-amiga"),
"x68k": "sharp-x68000", **dict.fromkeys(("jag", "jaguar"), "atari-jaguar"),
**dict.fromkeys(("st", "atarist"), "atari-st"), "a800": "atari-400-800",
**dict.fromkeys(("coleco", "colecovision"), "coleco-colecovision"),
**dict.fromkeys(("intv", "intellivision"), "mattel-intellivision"),
"o2": "magnavox-odyssey2", "channelf": "fairchild-channel-f",
**dict.fromkeys(("zx", "spectrum", "zxspectrum"), "sinclair-zx-spectrum"),
"cpc": "amstrad-cpc", "bbc": "acorn-bbc-micro", "apple2": "apple-ii",
}
def _unprofiled_cores_firmware(
plat: str, bios_path: Path, manifest: dict, found: list[str], searched: list[Path],
) -> list[str]:
"""What the installed cores the list has no profile for declare, each said.
# A system some lists file under another id, by the file that stands for it.
# The Neo Geo card offers --system snk-neogeo, and RetroArch's list keeps
# neogeo.zip under arcade: the run suggests arcade, never takes it.
SYSTEM_KEY_FILES = {"snk-neogeo": "neogeo.zip"}
def key_file_holders(files: list[dict], omitted: list[dict]) -> dict[str, list[str]]:
"""The systems a list files each SYSTEM_KEY_FILES file under, for the
systems the list does not name itself."""
entries = files + omitted
known = {system for entry in entries for system in entry_systems(entry)}
holders: dict[str, list[str]] = {}
for system, name in SYSTEM_KEY_FILES.items():
if system in known:
continue
held = sorted({
holder for entry in entries
if PurePosixPath(str(entry.get("dest", ""))).name.lower() == name
for holder in entry_systems(entry)
})
if held:
holders[system] = held
return holders
def did_you_mean(
typed: list[str], known: list[str], listing: str,
aliases: dict[str, str] | None = None, inside_from: int = 4,
meant: dict[str, list[str]] | None = None,
) -> str:
"""The closest known names to what was typed, and the option listing them all.
A typo was answered with every name the platform has, up to 400 on one
line, with no hint that playstation is sony-playstation. An alias close
to what was typed suggests the name it stands for. A name holding what
was typed, inside_from letters or more, is suggested too. meant gives,
by the letters and digits of a typed name, the names it stands for
here, suggested instead of the close ones.
Such a core reads systems the list cannot name, and narrowing to the
installed cores dropped its BIOS without a word (issue 87, parallel_dc
before its profile). Its .info is what RetroArch itself checks, so the
files it declares are kept. A core without one is named.
"""
import difflib
by_key: dict[str, str] = {}
for name in known:
by_key.setdefault(_name_key(name), name)
for alias, name in (aliases or {}).items():
by_key.setdefault(_name_key(alias), name)
close: list[str] = []
for word in typed:
key = _name_key(word)
if not key:
continue
if meant and key in meant:
close += [name for name in meant[key] if name not in close]
continue
matches = difflib.get_close_matches(key, list(by_key), n=3, cutoff=0.6)
if len(key) >= inside_from:
matches += sorted(k for k in by_key if key in k and k not in matches)
for k in matches:
# Two aliases of one system (psx, ps1) name it once.
if by_key[k] not in close:
close.append(by_key[k])
if close:
return f"Close matches: {', '.join(close[:3])}. {listing} lists all {len(known)}."
if len(known) <= SHORT_LIST:
return f"Known: {', '.join(known)}."
return f"{listing} lists all {len(known)}."
unknown = unknown_cores(manifest, found)
if not unknown:
return []
dirs = info_dirs(plat, bios_path, searched)
dests: list[str] = []
for core in unknown:
declared = info_firmware(dirs, core)
if declared is None:
message = (f"{core}: a core this list does not know, with no .info to read: "
"its files may be missing. Run without --installed-cores "
"to install every file.")
else:
message = (f"{core}: a core this list does not know, "
f"{_plural(len(declared), 'file')} its .info declares kept")
print(f" {message}")
EVENTS.emit("unknown_core", platform=plat, core=core,
info=declared is not None, dests=declared or [], message=message)
dests.extend(declared or [])
return dests
def refuse_name(platform: str, *lines: str) -> NoReturn:
"""End the run on a name it cannot use (a platform, target, system, core
or emulator), refused before any download.
def _no_core_found(plat: str, bios_path: Path, searched: list[Path]) -> None:
"""Refuse --installed-cores with no core, and say how to point at them.
The reason went to stderr only: a reader of --events got start, then
end with code 1, and could not tell a typo from a failed download. Code
2, as for an option argparse refuses: 1 also means --check found files
to fetch, and a script reading it reran the install on a typo forever.
With no retroarch.cfg naming the folder as its system_directory, and
none beside it, nothing names the cores folder: the run stopped on "no
known directory" with no way forward.
"""
message = " ".join(line.strip() for line in lines)
if message.startswith("Error: "):
message = message[len("Error: "):]
EVENTS.emit("error", stage="option", reason="unknown", platform=platform,
message=message)
for line in lines:
print(line, file=sys.stderr)
sys.exit(2)
# Frontends and systems that keep no BIOS folder of their own: each
# emulator they start reads its own, which a run without --platform finds.
# --platform es-de was "Unknown platform", the answer left unsaid.
_NO_BIOS_FOLDER = ("ES-DE", "LaunchBox", "Android")
def normalize_platform(name: str, say: bool = True) -> str:
"""Return the canonical platform id for user-supplied input.
The site writes MiSTer FPGA and RetroArch: letters and digits decide,
as they do for verify.py and generate_pack.py. say is False where the
name is read again later, which says it once.
"""
key = _name_key(name)
for plat in AVAILABLE_PLATFORMS:
if key and key in (_name_key(plat), _name_key(display_name(plat))):
return plat
# --list-platforms prints `muOS: retroarch`, and --platform muos was
# refused: a system named there stands for the list it gets.
for rule in _EMBEDDED_RULES:
if key and rule.manifest and key == _name_key(rule.name):
if say:
print(f" {rule.name} gets the {display_name(rule.manifest)} file list: "
f"--platform {rule.manifest}.")
return rule.manifest
for frontend in _NO_BIOS_FOLDER:
if key and key == _name_key(frontend):
refuse_name(name.strip(), f"{frontend} has no BIOS folder of its own: run again "
"without --platform, and the installer fills the folder of each "
"emulator it finds here.")
# --list-platforms prints TrimUI with why it gets no folder, and naming
# it answered "Unknown platform".
for rule in _EMBEDDED_RULES:
if key and not rule.manifest and key == _name_key(rule.name):
refuse_name(name.strip(), f"{rule.name} is not installed to: "
f"{_NO_FOLDER.get(rule.name, 'no source names its BIOS folder')}.",
" Name that folder: --platform retroarch "
"--dest FOLDER")
# Every name --list-platforms prints, so that spruce suggests spruceOS
# and the count is the one the listing shows.
known = list(AVAILABLE_PLATFORMS) + list(dict.fromkeys(
rule.name for rule in _EMBEDDED_RULES
if _name_key(rule.name) not in {_name_key(plat) for plat in AVAILABLE_PLATFORMS}
)) + list(_NO_BIOS_FOLDER)
refuse_name(name.strip(), f"Unknown platform '{name.strip()}'. "
+ did_you_mean([name], known, "--list-platforms"))
def entry_systems(entry: dict) -> list[str]:
"""The platform systems a file or an omission belongs to.
An omission's `system` is spelled the way its profile spells it. Only
`systems` names the platform's own, the ones --system accepts.
"""
return list(entry.get("systems") or [])
where = (f"none in {', '.join(map(str, searched))}" if searched
else f"no retroarch.cfg was found for {bios_path} to name their folder")
print(f"Error: no installed core found for {display_name(plat)}: {where}.",
file=sys.stderr)
print(" Name the folder RetroArch loads cores from (libretro_directory in "
"retroarch.cfg, cores/ beside RetroArch by default):", file=sys.stderr)
command = _INVOCATION.command(plat, bios_path, without=("--installed-cores", "--cores-dir"))
print(f" {command} --cores-dir <cores folder>", file=sys.stderr)
print(" Or run it without --installed-cores to install every file.", file=sys.stderr)
sys.exit(1)