diff --git a/install.ps1 b/install.ps1 index 772b0e635..98424c6a5 100644 --- a/install.ps1 +++ b/install.ps1 @@ -16,7 +16,7 @@ # and Invoke-WebRequest throws on a cut transfer before the hash is read: # the site's build rewrites this line there (scripts/sitebootstrap.py). $mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." - $defaultInstallSha256 = "bbc5e1c6597873856565869e1a7f8ef376bc698b9d04e7301a3ae56635cfafbd" + $defaultInstallSha256 = "24f8bbd81e850b808ac113ec91893a98296453119714c62ad19888add0d42075" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. $maximumInstallerBytes = 8MB diff --git a/install.py b/install.py index a5eacd263..91e080313 100644 --- a/install.py +++ b/install.py @@ -22840,31 +22840,7 @@ def _pass_download(run: _InstallRun, p: _PlatformPass) -> str: return "stop" if _pass_fetch(run, p, dl_size, created) else "go" -# --- installer/run_files.py -------------------------------------------------- - -def _system_label(name: str, manifest: dict | None) -> str: - """A system id with the names the platform gives it: nintendo-gba (gba).""" - names = ((manifest or {}).get("system_names") or {}).get(name) - return f"{name} ({', '.join(names)})" if names else name - - -def _print_choices( - plat: str, files: list[dict], omitted: list[dict], systems: bool, - manifest: dict | None = None, -) -> None: - """What --system or --core accepts for a platform, with what each holds.""" - choices = available_choices(files, omitted) - names = choices["systems"] if systems else choices["cores"] - if not names: - print(f" {plat} records no {'systems' if systems else 'cores'}") - return - labels = {name: _system_label(name, manifest) if systems else name for name in names} - width = min(max(len(label) for label in labels.values()), 40) - for name in names: - held = [f for f in files if _chosen(f, {name})] if systems else core_files(files, name) - print(f" {labels[name]:<{width}} " - f"{_system_sized(held) if systems else _core_sized(held)}") - +# --- installer/setup_passes.py ----------------------------------------------- def _print_emulators(plat: str, manifest: dict, files: list[dict], omitted: list[dict]) -> None: """What --emulator accepts for a platform, one name per line.""" @@ -23267,6 +23243,32 @@ def _unlisted_save(entry: dict, why: str) -> dict: return {**entry, "why": why, "remedy": ""} +# --- installer/run_files.py -------------------------------------------------- + +def _system_label(name: str, manifest: dict | None) -> str: + """A system id with the names the platform gives it: nintendo-gba (gba).""" + names = ((manifest or {}).get("system_names") or {}).get(name) + return f"{name} ({', '.join(names)})" if names else name + + +def _print_choices( + plat: str, files: list[dict], omitted: list[dict], systems: bool, + manifest: dict | None = None, +) -> None: + """What --system or --core accepts for a platform, with what each holds.""" + choices = available_choices(files, omitted) + names = choices["systems"] if systems else choices["cores"] + if not names: + print(f" {plat} records no {'systems' if systems else 'cores'}") + return + labels = {name: _system_label(name, manifest) if systems else name for name in names} + width = min(max(len(label) for label in labels.values()), 40) + for name in names: + held = [f for f in files if _chosen(f, {name})] if systems else core_files(files, name) + print(f" {labels[name]:<{width}} " + f"{_system_sized(held) if systems else _core_sized(held)}") + + def _pass_games(run: _InstallRun, p: _PlatformPass) -> None: """The games of the card read, and the files narrowed to what they need where no menu chooses.""" diff --git a/install.sh b/install.sh index 13ad5b3f1..5bfd9f15b 100755 --- a/install.sh +++ b/install.sh @@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins # curl or wget stop on a cut transfer before the hash is read: the site's # build rewrites this line there (scripts/sitebootstrap.py). MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." -DEFAULT_INSTALL_SHA256="bbc5e1c6597873856565869e1a7f8ef376bc698b9d04e7301a3ae56635cfafbd" +DEFAULT_INSTALL_SHA256="24f8bbd81e850b808ac113ec91893a98296453119714c62ad19888add0d42075" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. MAX_INSTALLER_BYTES=8388608 diff --git a/install/launchers/index.json b/install/launchers/index.json index 8b37ee563..94f5b065a 100644 --- a/install/launchers/index.json +++ b/install/launchers/index.json @@ -27,8 +27,8 @@ { "dest": "roms/ports/retrobios/install.py", "repo_path": "install.py", - "size": 1503429, - "sha256": "bbc5e1c6597873856565869e1a7f8ef376bc698b9d04e7301a3ae56635cfafbd", + "size": 1503510, + "sha256": "24f8bbd81e850b808ac113ec91893a98296453119714c62ad19888add0d42075", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1557054, - "sha256": "faa3e9a8d39ebd57654ed3daba9b3de730800fde723f5e97b595d9edba950c74" + "size": 1557135, + "sha256": "6e7ac34c49310e24291d5fd0ccec18f9656ff562ca0d7cc3d17a6fafe77f9707" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1503429, - "sha256": "bbc5e1c6597873856565869e1a7f8ef376bc698b9d04e7301a3ae56635cfafbd", + "size": 1503510, + "sha256": "24f8bbd81e850b808ac113ec91893a98296453119714c62ad19888add0d42075", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1503429, - "sha256": "bbc5e1c6597873856565869e1a7f8ef376bc698b9d04e7301a3ae56635cfafbd", + "size": 1503510, + "sha256": "24f8bbd81e850b808ac113ec91893a98296453119714c62ad19888add0d42075", "executable": false }, { diff --git a/installer/romm.py b/installer/romm.py index 4245eb530..b0701addb 100644 --- a/installer/romm.py +++ b/installer/romm.py @@ -9,7 +9,7 @@ from installer.host_fs import _is_dir from installer.refusal import Refusal if TYPE_CHECKING: - from installer.run_files import _PlatformPass + from installer.setup_passes import _PlatformPass from installer.run_setups import _InstallRun # RomM serves the firmware rows its library scan records (rommapp/romm 5.4.0, diff --git a/installer/run.py b/installer/run.py index 961667a04..65cd92061 100644 --- a/installer/run.py +++ b/installer/run.py @@ -57,7 +57,7 @@ from installer.text import agree if TYPE_CHECKING: from installer.folder_index import FolderIndex - from installer.run_files import _PlatformPass + from installer.setup_passes import _PlatformPass from installer.run_setups import _InstallRun from installer.run_check import _pass_check_existing, _pass_check_report, _pass_dry_run from installer.run_download import _pass_download, _remember_install diff --git a/installer/run_check.py b/installer/run_check.py index a6590c3dd..5a1f746db 100644 --- a/installer/run_check.py +++ b/installer/run_check.py @@ -38,7 +38,7 @@ from installer.tally import ( from installer.text import _plural if TYPE_CHECKING: - from installer.run_files import _PlatformPass + from installer.setup_passes import _PlatformPass from installer.run_setups import _InstallRun diff --git a/installer/run_download.py b/installer/run_download.py index c6a875ee0..5bac19b85 100644 --- a/installer/run_download.py +++ b/installer/run_download.py @@ -37,7 +37,7 @@ from installer.tally import _bytes from installer.text import _plural, format_size, screen_text if TYPE_CHECKING: - from installer.run_files import _PlatformPass + from installer.setup_passes import _PlatformPass from installer.run_setups import _InstallRun diff --git a/installer/run_files.py b/installer/run_files.py index 015564b63..4a764a9fc 100644 --- a/installer/run_files.py +++ b/installer/run_files.py @@ -1,71 +1,41 @@ -"""One setup's file list, narrowed pass by pass by every option and choice.""" +"""One setup's file list: the passes by games, menu, choice and placement, and the order of every pass.""" from __future__ import annotations -import argparse -import io -import json -import os -import stat import sys from pathlib import Path -from typing import TYPE_CHECKING, TextIO +from typing import TYPE_CHECKING from installer.choices import ( GAMES_REQUIRED_WORDS, GamesChoice, - _narrow_to_emulators, _older_list_advice, _requested_narrowing, - _standalone_builds, available_choices, core_files, - emulator_names, records_selection, - trim_recorded, ) from installer.console import OUTPUT, detail -from installer.cores import _unprofiled_cores_firmware from installer.datadirs import select_data_directories -from installer.destination import _unknown_target from installer.detect_os import detected_shown -from installer.device import card_board, detect_board, version_note from installer.events import EVENTS -from installer.fetch_plan import FetchPlan -from installer.filesystems import ( - FAT_REFUSED, - FAT_REFUSED_SHOWN, - fold_case_twins, - holds_fat_refused_names, - keeps_case, - split_fat_refused, -) from installer.frontend_folders import ( - place_emulator_entries, - place_relative_entries, - place_save_entries, say_saves, say_unplaced, unplaced_whys, ) from installer.games import fallback_lines, read_games -from installer.install_record import _RECORD_TAKEN, by_record, drop_recorded, record_menu_words +from installer.install_record import _RECORD_TAKEN, record_menu_words from installer.invocation import _INVOCATION from installer.line_menu import MenuContext, _prompt_custom_selection -from installer.lists import ListFailure, fetch_manifest, fetch_targets, load_targets, local_targets, targets_failure_lines from installer.manifest import GAME_DATA from installer.narrowing import ( _chosen, - board_target, - designated_cores, - find_by_hash, narrow, narrow_to_cores, one_per_slot, - truth_lists, ) from installer.offline import LOCAL from installer.pack import _pack_refused, pack_path -from installer.paths import shown_path from installer.plan_words import ( INSTALLED_ONLY, _core_sized, @@ -78,25 +48,31 @@ from installer.plan_words import ( game_data_only_words, unlisted_saves_words, ) -from installer.platforms import PLATFORM_NAMES, display_name, system_title +from installer.platforms import system_title from installer.refusal import Refusal from installer.tally import ( _bytes, _left_out_lines, - keep_declared, kinds_definition, leave_out_game_data, not_installable_words, parts_words, tally, ) -from installer.text import _plural, format_size, screen_text +from installer.text import _plural, format_size if TYPE_CHECKING: - from installer.folder_index import FolderIndex, LocalCopy - from installer.local_copies import Unreadable - from installer.game_folders import GamesPlan from installer.run_setups import _InstallRun +from installer.setup_passes import ( + _PlatformPass, + _pass_case_and_names, + _pass_emulators, + _pass_files, + _pass_installed_cores, + _pass_options, + _pass_target, + _pass_version_and_saves, +) def _system_label(name: str, manifest: dict | None) -> str: @@ -123,407 +99,6 @@ def _print_choices( f"{_system_sized(held) if systems else _core_sized(held)}") -def _print_emulators(plat: str, manifest: dict, files: list[dict], omitted: list[dict]) -> None: - """What --emulator accepts for a platform, one name per line.""" - names = emulator_names(manifest, files, omitted) - if not names: - print(f" {plat} records no emulators") - return - width = min(max(len(name) for name in names), 40) - for name in sorted(names): - profile = names[name] - print(f" {name:<{width}} {'' if profile == name else 'same as ' + profile}".rstrip()) - - -FIND_SCHEMA = 1 -# What a match of --find --json carries, every key in this order, null -# where the list records none. -FIND_FIELDS = ("dest", "root", "sha1", "sha256", "md5", "crc32", "size", "systems", "cores") - - -def _found_files(args: argparse.Namespace, plat: str, - files: list[dict]) -> tuple[list[dict], list[dict]]: - """--find: the file of the list with the hash given, the run narrowed to - it, or with --json the matches printed and nothing installed. - - One content found at several destinations installs at each. A hash two - contents share (a crc32) is ambiguous: named, never chosen. A kind the - list does not record (md5, crc32) is said as such, never as unknown. - """ - value = args.find.strip().lower() - kind, matches, recorded = find_by_hash(files, value) - contents = {match.get("sha1") or match.get("sha256") for match in matches} - result = ("found" if len(contents) == 1 else "ambiguous" if contents - else "unknown" if recorded else "unrecorded") - if args.json: - record = {"schema": FIND_SCHEMA, "platform": plat, "hash": value, "kind": kind, - "result": result, - "matches": [{field: match.get(field) for field in FIND_FIELDS} - for match in matches]} - print(json.dumps(record), file=args.json_out, flush=True) - if result != "found": - args.find_failed = True - return [], [] - lines = { - "unrecorded": [f"Error: the {plat} file list records no {kind}: --find reads " - "sha1 and sha256 in every list."], - "unknown": [f"Error: no file of the {plat} list has the {kind} {value}."], - "ambiguous": [f"Error: {len(contents)} files of the {plat} list have the " - f"{kind} {value}: give their sha1 instead.", - *(f" {match['dest']} {match.get('sha1', '')}" for match in matches)], - }.get(result) - if lines is not None: - # 4: the hash was read and the list holds no one file for it, neither - # a failure (1) nor an option refused (2). - raise Refusal("option", "unknown", plat, *lines, code=4, - message=lines[0][len("Error: "):]) - for match in matches: - print(f" {value} is {match['dest']}") - return matches, [] - - -def _output_kind(stream: TextIO) -> str: - """"pipe" or "file" for what stream writes to, "" when neither is known. - `| tee log.txt` shows every line, `> log.txt` none of them.""" - try: - mode = os.fstat(stream.fileno()).st_mode - except (OSError, ValueError, AttributeError, io.UnsupportedOperation): - return "" - if stat.S_ISFIFO(mode) or stat.S_ISSOCK(mode): - return "pipe" - return "file" if stat.S_ISREG(mode) else "" - - -class _PlatformPass: - """One setup of the run: its file list as each stage narrows it, then - what the check found in its folder.""" - - def __init__(self, plat_name: str, bios_path: Path, manifest: dict) -> None: - self.plat_name = plat_name - self.bios_path = bios_path - self.manifest = manifest - self.files: list[dict] = manifest.get("files", []) - self.omitted_files: list[dict] = manifest.get("omitted_files", []) - self.variants: list[dict] = [] - self.case_kept = False - self.game_data_cut = 0 - self.target: str | None = None - # The cores whose order decides a slot: the board's, else every core. - self.slot_reach: set[str] | None = None - self.found_cores: list[str] = [] - self.display = "" - self.unplaced: list[dict] = [] - self.menu = False - self.games_plan: GamesPlan | None = None - self.games_applied = False - self.core_choices: set[str] = set() - self.selection: tuple[list[str], list[str], list[str]] | None = None - # What declining the size question points back at, when it was asked. - self.smaller = "" - # g at the menu: whether it left the game data out. - self.cut: list[bool] | None = None - self.run_counts = tally([]) - self.narrowed = False - self.data_dirs: list[dict] = [] - self.head_data = 0 - self.record_files: list[dict] = [] - self.to_download: list[dict] = [] - self.up_to_date: list[dict] = [] - # The entries this run put in place, each checked as it landed. - self.landed: list[dict] = [] - self.mismatched: list[dict] = [] - self.data_states: list[tuple[dict, tuple[Path, dict[str, dict], int]]] = [] - self.trees_missing = 0 - self.bios_index: FolderIndex | None = None - self.copies: dict[int, LocalCopy] = {} - self.unread: list[Unreadable] = [] - self.plan = FetchPlan([], [], [], 0, 0) - self.hint: list[str] = [] - self.space_checked = False - - -def _pass_files(run: _InstallRun, plat_name: str, bios_path: Path) -> _PlatformPass: - """The setup's file list read, narrowed to what --source truth keeps.""" - if bios_path not in run.installed_paths: - run.installed_paths.append(bios_path) - if LOCAL.manifest is not None: - detail(f"\nReading the file index for {plat_name} from {LOCAL.manifest}...") - elif LOCAL.pack is not None: - detail(f"\nReading the file index for {plat_name} from {LOCAL.pack.name}...") - else: - detail(f"\nFetching file index for {plat_name}...") - manifest = fetch_manifest(plat_name) - run.categorized = run.categorized or bool(manifest.get("categorized")) - p = _PlatformPass(plat_name, bios_path, manifest) - if run.args.source == "truth": - if manifest.get("truth_files") is None: - raise Refusal("option", "not_recorded", plat_name, - f"Error: the {plat_name} file list does not say what the emulator " - "profiles prove is read, which --source truth needs.", - _older_list_advice(plat_name)) - before = len(p.files) - p.files, p.omitted_files = truth_lists(manifest, p.files, p.omitted_files) - detail(f" What the emulator profiles prove is read: {before} to " - f"{len(p.files)} files (--source truth)") - return p - - -def _pass_case_and_names(run: _InstallRun, p: _PlatformPass) -> None: - """The case variants kept or folded, and the names the drive refuses left out.""" - bios_path, manifest, files = p.bios_path, p.manifest, p.files - variants = manifest.get("case_variants", []) - case_kept = keeps_case(bios_path) - if variants and case_kept: - # The list is folded for Windows and macOS, but here bios.col and - # BIOS.col are two files that two cores open. - files = files + [{**variant, "case_variant": True} for variant in variants] - elif run.packing: - # A ZIP keeps both spellings, whatever drive it is written to: - # the device installing it folds them. - files = files + [{**variant, "case_variant": True} for variant in variants] - case_kept = True - p.variants, p.case_kept = variants, case_kept - if not case_kept: - files, folded = fold_case_twins(files) - if folded: - # Batocera's share opened over SMB, an exFAT card: the second - # spelling overwrote the first, and every --check found one - # of them wrong and fetched it again. - shown = ", ".join(folded[:3]) + (" ..." if len(folded) > 3 else "") - detail( - f" {_plural(len(folded), 'file')} left out: {bios_path} does not keep " - f"case and holds another file under the same name ({shown})" - ) - if not run.packing and any(FAT_REFUSED.search(str(entry.get("dest", ""))) - for entry in files) \ - and not holds_fat_refused_names(bios_path): - files, refused = split_fat_refused(files) - shown = ", ".join(refused[:3]) + (" ..." if len(refused) > 3 else "") - detail( - f" {_plural(len(refused), 'file')} left out: the file system of " - f"{shown_path(bios_path)} cannot hold a name with any of " - f"{FAT_REFUSED_SHOWN} ({screen_text(shown)})" - ) - p.files = files - - -def _pass_options(run: _InstallRun, p: _PlatformPass) -> bool: - """--find, --no-game-data, --declared-only and --list-targets. True when - the setup is done with.""" - args, plat_name = run.args, p.plat_name - if args.find: - # Among the case variants kept here too: bios.col is one file. - p.files, p.omitted_files = _found_files(args, plat_name, p.files) - if args.json: - return True - if args.no_game_data and not p.manifest.get("categorized") and by_record(args): - drop_recorded(args, plat_name, "--no-game-data", "the list marks no game data") - if args.no_game_data and not p.manifest.get("categorized"): - # Without the field nothing is marked, and the run would - # install the game data it was asked to leave out. - raise Refusal("option", "not_recorded", plat_name, - f"Error: the {plat_name} file list records no file category, " - "which --no-game-data needs.", _older_list_advice(plat_name)) - - if args.declared_only: - before = len(p.files) - p.files, p.omitted_files = keep_declared(p.files, p.omitted_files) - detail(f" The platform's own list: {before} to {len(p.files)} files " - "(--declared-only)") - - if args.list_targets: - targets = needed_targets(plat_name) - if not targets: - print(f" No targets available for {plat_name}") - else: - for t in sorted(targets.keys()): - cores = targets[t].get("cores") - label = "no core list" if cores is None else f"{len(cores)} cores" - print(f" {t} ({label})") - return True - return False - - -def _pass_board(run: _InstallRun, p: _PlatformPass) -> str | None: - """The board the setup narrows to: --target, else the one the device or - the card mounted here names.""" - args, plat_name = run.args, p.plat_name - target = args.target - if (target or args.every_board or run.packing or run.chosen_by_flag - or (LOCAL.offline and local_targets(plat_name) is None)): - return target - board = detect_board(plat_name) - where = "detected" - if not board and args.dest is not None: - board, where = card_board(plat_name), "found on the card" - if not board: - return target - known = fetch_targets(plat_name) or {} - image, board = board, board_target(board, known) or board - if board in known: - target = board - named = board if image == board else f"{board} ({image})" - words = f"Board {named} {where} (--every-board installs all boards)" - else: - words = (f"Board {board} {where} has no " - f"{PLATFORM_NAMES.get(plat_name, plat_name)} core list: " - "installing the files of every board") - detail(f" {words}") - EVENTS.emit("board", platform=plat_name, board=board, - applied=target == board, message=words) - return target - - -def _pass_target(run: _InstallRun, p: _PlatformPass) -> None: - """The files narrowed to the cores of the board.""" - # Target filtering. With no --target, a system that names its board - # narrows to it: a Batocera RPi4 took 8.6 GiB where its board needs - # 5.5. Read from the device itself, else from the card mounted here: - # a card prepared on a computer took every board's files, and the - # full-screen interface, which always gives --dest, never applied one. - p.target = target = _pass_board(run, p) - if target: - _pass_target_cores(run, p, target) - - -def needed_targets(plat: str) -> dict: - """The board list a run cannot go on without, its failure the run's refusal.""" - try: - return load_targets(plat) - except ListFailure as stopped: - # A list the run would carry on without stops this one all the same: - # it said why on stderr and ended with code 1, no error event sent. - problem = stopped.problem - raise Refusal("targets", problem.code, problem.plat, - *targets_failure_lines(problem), code=problem.fatal or 1) from None - - -def _pass_target_cores(run: _InstallRun, p: _PlatformPass, target: str) -> None: - """The files narrowed to the cores the board's list names.""" - targets = run.target_lists.get(p.plat_name) - if targets is None: - targets = needed_targets(p.plat_name) - target_info = targets.get(target) - if target_info is None: - # Carrying on would install every file, which is the opposite - # of what a filter was asked for: a 4 GB download instead of - # the subset the target needs. Refuse rather than ignore. - _unknown_target(target, p.plat_name, targets) - elif target_info.get("cores") is None: - detail( - f" Target '{target}' publishes no core list: " - "installing every file" - ) - else: - before = len(p.files) - p.files, p.omitted_files = narrow_to_cores( - p.manifest, target_info["cores"], p.files, p.omitted_files, - target=target, - ) - p.slot_reach = designated_cores(p.manifest, target_info["cores"], target) - detail(f" Filtered {before} to {len(p.files)} files for target {target}") - - -def _pass_installed_cores(run: _InstallRun, p: _PlatformPass) -> None: - """The files narrowed to the cores installed, with --installed-cores.""" - args, plat_name, bios_path, manifest = run.args, p.plat_name, p.bios_path, p.manifest - found_cores = run.cores_found.get((plat_name, bios_path), []) - if "system_cores" not in manifest: - found_cores = [] - elif found_cores: - detail(f" {_plural(len(found_cores), 'core')} installed") - p.found_cores = found_cores - if args.installed_cores and "system_cores" not in manifest and by_record(args): - drop_recorded(args, plat_name, "--installed-cores", - "the list does not say which cores read which systems") - if not args.installed_cores: - return - if "system_cores" not in manifest: - raise Refusal("option", "not_recorded", plat_name, - f"Error: the {plat_name} file list does not say which cores " - "read which systems, which --installed-cores needs.", - _older_list_advice(plat_name)) - before = len(p.files) - info_dests = _unprofiled_cores_firmware( - plat_name, bios_path, manifest, found_cores, - run.cores_searched.get((plat_name, bios_path), []), - ) - p.files, p.omitted_files = narrow_to_cores( - manifest, found_cores, p.files, p.omitted_files, info_dests - ) - detail(f" Narrowed {before} to {len(p.files)} files to the installed cores") - # A slot is ranked by the cores installed, as a pack built for them. - found_reach = designated_cores(manifest, found_cores) - p.slot_reach = found_reach if p.slot_reach is None else p.slot_reach & found_reach - - -def _pass_emulators(run: _InstallRun, p: _PlatformPass) -> bool: - """--list-emulators, or the files narrowed to --emulator. True when the - setup is done with.""" - args = run.args - if args.list_emulators: - _print_emulators(p.plat_name, p.manifest, *( - leave_out_game_data(p.files, p.omitted_files)[:2] if args.no_game_data - else (p.files, p.omitted_files))) - return True - - if args.emulator and by_record(args): - for part in trim_recorded({"--emulator": list(args.emulator)}, p.plat_name, - p.manifest, p.files, p.omitted_files): - drop_recorded(args, p.plat_name, part.flag, part.why, part.keep) - if args.emulator: - names = emulator_names(p.manifest, p.files, p.omitted_files) - before = len(p.files) - chosen, p.files, p.omitted_files = _narrow_to_emulators( - args.emulator, p.manifest, p.plat_name, p.files, p.omitted_files - ) - detail(f" Narrowed {before} to {len(p.files)} files to {', '.join(chosen)}") - if args.standalone: - p.files, p.omitted_files = _standalone_builds( - args.emulator, names, p.manifest, p.plat_name, p.files, p.omitted_files) - return False - - -def _pass_version_and_saves(run: _InstallRun, p: _PlatformPass) -> None: - """The list's version said, and the files read from save and emulator - folders placed.""" - plat_name, bios_path, manifest = p.plat_name, p.bios_path, p.manifest - # Said before the install and again with the result. - note = None if run.listing or run.packing else version_note(plat_name, bios_path, manifest) - if note is not None: - print(f" {note[2]}") - run.version_notes.append(note[2]) - EVENTS.emit("version", platform=plat_name, list_version=note[0], - installed_version=note[1], message=note[2]) - - # Before the menu: it offered 23 files, then 22 were checked, the - # one read from an unknown save directory left out. Said once the - # choice is made, over the files it keeps (say_saves, say_unplaced). - p.display = display = display_name(plat_name, manifest) - if not run.packing: - # A pack keeps every root's files: the device installing it - # places them. - files, unplaced = place_save_entries( - p.files, bios_path, manifest, run.args.save_dir, display) - files, beyond = place_relative_entries(files, bios_path, manifest) - p.files, elsewhere = place_emulator_entries(files, bios_path, manifest) - p.unplaced = [*unplaced, *beyond, *elsewhere] - if not manifest.get("save_directory") and not run.packing: - # Its own list leaves these files out: naming the folder with - # --save-dir installs none of them, and was advised in a loop. - why = unlisted_saves_words(display) - p.unplaced = [_unlisted_save(entry, why) for entry in p.unplaced] - p.omitted_files = [_unlisted_save(entry, why) for entry in p.omitted_files] - - -def _unlisted_save(entry: dict, why: str) -> dict: - """entry, when read from a save directory the list names no place for, - with why in words and no remedy.""" - if entry.get("reason") != "no_save_directory": - return entry - return {**entry, "why": why, "remedy": ""} - - def _pass_games(run: _InstallRun, p: _PlatformPass) -> None: """The games of the card read, and the files narrowed to what they need where no menu chooses.""" diff --git a/installer/run_setups.py b/installer/run_setups.py index 6b9028a1a..2924f0543 100644 --- a/installer/run_setups.py +++ b/installer/run_setups.py @@ -36,7 +36,7 @@ from installer.pack_source import pack_name_platforms from installer.paths import MOVED from installer.platforms import AVAILABLE_PLATFORMS, _NO_BIOS_FOLDER, display_name, normalize_platform from installer.refusal import Refusal -from installer.run_files import _output_kind, needed_targets +from installer.setup_passes import _output_kind, needed_targets from installer.safe_replace import KEPT from installer.setups import PORTABLE_ONLY, _default_dest, _line_setups, drive_example, found_setups from installer.standalone import _install_standalone_folders diff --git a/installer/setup_passes.py b/installer/setup_passes.py new file mode 100644 index 000000000..d49136471 --- /dev/null +++ b/installer/setup_passes.py @@ -0,0 +1,468 @@ +"""The first passes over a setup's file list: names, options, board, target, cores, emulators, saves.""" + +from __future__ import annotations + +import argparse +import io +import json +import os +import stat +from pathlib import Path +from typing import TYPE_CHECKING, TextIO + +from installer.choices import ( + _narrow_to_emulators, + _older_list_advice, + _standalone_builds, + emulator_names, + trim_recorded, +) +from installer.console import detail +from installer.cores import _unprofiled_cores_firmware +from installer.destination import _unknown_target +from installer.device import card_board, detect_board, version_note +from installer.events import EVENTS +from installer.fetch_plan import FetchPlan +from installer.filesystems import ( + FAT_REFUSED, + FAT_REFUSED_SHOWN, + fold_case_twins, + holds_fat_refused_names, + keeps_case, + split_fat_refused, +) +from installer.frontend_folders import ( + place_emulator_entries, + place_relative_entries, + place_save_entries, +) +from installer.install_record import by_record, drop_recorded +from installer.lists import ( + ListFailure, + fetch_manifest, + fetch_targets, + load_targets, + local_targets, + targets_failure_lines, +) +from installer.narrowing import ( + board_target, + designated_cores, + find_by_hash, + narrow_to_cores, + truth_lists, +) +from installer.offline import LOCAL +from installer.paths import shown_path +from installer.plan_words import unlisted_saves_words +from installer.platforms import PLATFORM_NAMES, display_name +from installer.refusal import Refusal +from installer.tally import keep_declared, leave_out_game_data, tally +from installer.text import _plural, screen_text + +if TYPE_CHECKING: + from installer.folder_index import FolderIndex, LocalCopy + from installer.game_folders import GamesPlan + from installer.local_copies import Unreadable + from installer.run_setups import _InstallRun + + +def _print_emulators(plat: str, manifest: dict, files: list[dict], omitted: list[dict]) -> None: + """What --emulator accepts for a platform, one name per line.""" + names = emulator_names(manifest, files, omitted) + if not names: + print(f" {plat} records no emulators") + return + width = min(max(len(name) for name in names), 40) + for name in sorted(names): + profile = names[name] + print(f" {name:<{width}} {'' if profile == name else 'same as ' + profile}".rstrip()) + + +FIND_SCHEMA = 1 +# What a match of --find --json carries, every key in this order, null +# where the list records none. +FIND_FIELDS = ("dest", "root", "sha1", "sha256", "md5", "crc32", "size", "systems", "cores") + + +def _found_files(args: argparse.Namespace, plat: str, + files: list[dict]) -> tuple[list[dict], list[dict]]: + """--find: the file of the list with the hash given, the run narrowed to + it, or with --json the matches printed and nothing installed. + + One content found at several destinations installs at each. A hash two + contents share (a crc32) is ambiguous: named, never chosen. A kind the + list does not record (md5, crc32) is said as such, never as unknown. + """ + value = args.find.strip().lower() + kind, matches, recorded = find_by_hash(files, value) + contents = {match.get("sha1") or match.get("sha256") for match in matches} + result = ("found" if len(contents) == 1 else "ambiguous" if contents + else "unknown" if recorded else "unrecorded") + if args.json: + record = {"schema": FIND_SCHEMA, "platform": plat, "hash": value, "kind": kind, + "result": result, + "matches": [{field: match.get(field) for field in FIND_FIELDS} + for match in matches]} + print(json.dumps(record), file=args.json_out, flush=True) + if result != "found": + args.find_failed = True + return [], [] + lines = { + "unrecorded": [f"Error: the {plat} file list records no {kind}: --find reads " + "sha1 and sha256 in every list."], + "unknown": [f"Error: no file of the {plat} list has the {kind} {value}."], + "ambiguous": [f"Error: {len(contents)} files of the {plat} list have the " + f"{kind} {value}: give their sha1 instead.", + *(f" {match['dest']} {match.get('sha1', '')}" for match in matches)], + }.get(result) + if lines is not None: + # 4: the hash was read and the list holds no one file for it, neither + # a failure (1) nor an option refused (2). + raise Refusal("option", "unknown", plat, *lines, code=4, + message=lines[0][len("Error: "):]) + for match in matches: + print(f" {value} is {match['dest']}") + return matches, [] + + +def _output_kind(stream: TextIO) -> str: + """"pipe" or "file" for what stream writes to, "" when neither is known. + `| tee log.txt` shows every line, `> log.txt` none of them.""" + try: + mode = os.fstat(stream.fileno()).st_mode + except (OSError, ValueError, AttributeError, io.UnsupportedOperation): + return "" + if stat.S_ISFIFO(mode) or stat.S_ISSOCK(mode): + return "pipe" + return "file" if stat.S_ISREG(mode) else "" + + +class _PlatformPass: + """One setup of the run: its file list as each stage narrows it, then + what the check found in its folder.""" + + def __init__(self, plat_name: str, bios_path: Path, manifest: dict) -> None: + self.plat_name = plat_name + self.bios_path = bios_path + self.manifest = manifest + self.files: list[dict] = manifest.get("files", []) + self.omitted_files: list[dict] = manifest.get("omitted_files", []) + self.variants: list[dict] = [] + self.case_kept = False + self.game_data_cut = 0 + self.target: str | None = None + # The cores whose order decides a slot: the board's, else every core. + self.slot_reach: set[str] | None = None + self.found_cores: list[str] = [] + self.display = "" + self.unplaced: list[dict] = [] + self.menu = False + self.games_plan: GamesPlan | None = None + self.games_applied = False + self.core_choices: set[str] = set() + self.selection: tuple[list[str], list[str], list[str]] | None = None + # What declining the size question points back at, when it was asked. + self.smaller = "" + # g at the menu: whether it left the game data out. + self.cut: list[bool] | None = None + self.run_counts = tally([]) + self.narrowed = False + self.data_dirs: list[dict] = [] + self.head_data = 0 + self.record_files: list[dict] = [] + self.to_download: list[dict] = [] + self.up_to_date: list[dict] = [] + # The entries this run put in place, each checked as it landed. + self.landed: list[dict] = [] + self.mismatched: list[dict] = [] + self.data_states: list[tuple[dict, tuple[Path, dict[str, dict], int]]] = [] + self.trees_missing = 0 + self.bios_index: FolderIndex | None = None + self.copies: dict[int, LocalCopy] = {} + self.unread: list[Unreadable] = [] + self.plan = FetchPlan([], [], [], 0, 0) + self.hint: list[str] = [] + self.space_checked = False + + +def _pass_files(run: _InstallRun, plat_name: str, bios_path: Path) -> _PlatformPass: + """The setup's file list read, narrowed to what --source truth keeps.""" + if bios_path not in run.installed_paths: + run.installed_paths.append(bios_path) + if LOCAL.manifest is not None: + detail(f"\nReading the file index for {plat_name} from {LOCAL.manifest}...") + elif LOCAL.pack is not None: + detail(f"\nReading the file index for {plat_name} from {LOCAL.pack.name}...") + else: + detail(f"\nFetching file index for {plat_name}...") + manifest = fetch_manifest(plat_name) + run.categorized = run.categorized or bool(manifest.get("categorized")) + p = _PlatformPass(plat_name, bios_path, manifest) + if run.args.source == "truth": + if manifest.get("truth_files") is None: + raise Refusal("option", "not_recorded", plat_name, + f"Error: the {plat_name} file list does not say what the emulator " + "profiles prove is read, which --source truth needs.", + _older_list_advice(plat_name)) + before = len(p.files) + p.files, p.omitted_files = truth_lists(manifest, p.files, p.omitted_files) + detail(f" What the emulator profiles prove is read: {before} to " + f"{len(p.files)} files (--source truth)") + return p + + +def _pass_case_and_names(run: _InstallRun, p: _PlatformPass) -> None: + """The case variants kept or folded, and the names the drive refuses left out.""" + bios_path, manifest, files = p.bios_path, p.manifest, p.files + variants = manifest.get("case_variants", []) + case_kept = keeps_case(bios_path) + if variants and case_kept: + # The list is folded for Windows and macOS, but here bios.col and + # BIOS.col are two files that two cores open. + files = files + [{**variant, "case_variant": True} for variant in variants] + elif run.packing: + # A ZIP keeps both spellings, whatever drive it is written to: + # the device installing it folds them. + files = files + [{**variant, "case_variant": True} for variant in variants] + case_kept = True + p.variants, p.case_kept = variants, case_kept + if not case_kept: + files, folded = fold_case_twins(files) + if folded: + # Batocera's share opened over SMB, an exFAT card: the second + # spelling overwrote the first, and every --check found one + # of them wrong and fetched it again. + shown = ", ".join(folded[:3]) + (" ..." if len(folded) > 3 else "") + detail( + f" {_plural(len(folded), 'file')} left out: {bios_path} does not keep " + f"case and holds another file under the same name ({shown})" + ) + if not run.packing and any(FAT_REFUSED.search(str(entry.get("dest", ""))) + for entry in files) \ + and not holds_fat_refused_names(bios_path): + files, refused = split_fat_refused(files) + shown = ", ".join(refused[:3]) + (" ..." if len(refused) > 3 else "") + detail( + f" {_plural(len(refused), 'file')} left out: the file system of " + f"{shown_path(bios_path)} cannot hold a name with any of " + f"{FAT_REFUSED_SHOWN} ({screen_text(shown)})" + ) + p.files = files + + +def _pass_options(run: _InstallRun, p: _PlatformPass) -> bool: + """--find, --no-game-data, --declared-only and --list-targets. True when + the setup is done with.""" + args, plat_name = run.args, p.plat_name + if args.find: + # Among the case variants kept here too: bios.col is one file. + p.files, p.omitted_files = _found_files(args, plat_name, p.files) + if args.json: + return True + if args.no_game_data and not p.manifest.get("categorized") and by_record(args): + drop_recorded(args, plat_name, "--no-game-data", "the list marks no game data") + if args.no_game_data and not p.manifest.get("categorized"): + # Without the field nothing is marked, and the run would + # install the game data it was asked to leave out. + raise Refusal("option", "not_recorded", plat_name, + f"Error: the {plat_name} file list records no file category, " + "which --no-game-data needs.", _older_list_advice(plat_name)) + + if args.declared_only: + before = len(p.files) + p.files, p.omitted_files = keep_declared(p.files, p.omitted_files) + detail(f" The platform's own list: {before} to {len(p.files)} files " + "(--declared-only)") + + if args.list_targets: + targets = needed_targets(plat_name) + if not targets: + print(f" No targets available for {plat_name}") + else: + for t in sorted(targets.keys()): + cores = targets[t].get("cores") + label = "no core list" if cores is None else f"{len(cores)} cores" + print(f" {t} ({label})") + return True + return False + + +def _pass_board(run: _InstallRun, p: _PlatformPass) -> str | None: + """The board the setup narrows to: --target, else the one the device or + the card mounted here names.""" + args, plat_name = run.args, p.plat_name + target = args.target + if (target or args.every_board or run.packing or run.chosen_by_flag + or (LOCAL.offline and local_targets(plat_name) is None)): + return target + board = detect_board(plat_name) + where = "detected" + if not board and args.dest is not None: + board, where = card_board(plat_name), "found on the card" + if not board: + return target + known = fetch_targets(plat_name) or {} + image, board = board, board_target(board, known) or board + if board in known: + target = board + named = board if image == board else f"{board} ({image})" + words = f"Board {named} {where} (--every-board installs all boards)" + else: + words = (f"Board {board} {where} has no " + f"{PLATFORM_NAMES.get(plat_name, plat_name)} core list: " + "installing the files of every board") + detail(f" {words}") + EVENTS.emit("board", platform=plat_name, board=board, + applied=target == board, message=words) + return target + + +def _pass_target(run: _InstallRun, p: _PlatformPass) -> None: + """The files narrowed to the cores of the board.""" + # Target filtering. With no --target, a system that names its board + # narrows to it: a Batocera RPi4 took 8.6 GiB where its board needs + # 5.5. Read from the device itself, else from the card mounted here: + # a card prepared on a computer took every board's files, and the + # full-screen interface, which always gives --dest, never applied one. + p.target = target = _pass_board(run, p) + if target: + _pass_target_cores(run, p, target) + + +def needed_targets(plat: str) -> dict: + """The board list a run cannot go on without, its failure the run's refusal.""" + try: + return load_targets(plat) + except ListFailure as stopped: + # A list the run would carry on without stops this one all the same: + # it said why on stderr and ended with code 1, no error event sent. + problem = stopped.problem + raise Refusal("targets", problem.code, problem.plat, + *targets_failure_lines(problem), code=problem.fatal or 1) from None + + +def _pass_target_cores(run: _InstallRun, p: _PlatformPass, target: str) -> None: + """The files narrowed to the cores the board's list names.""" + targets = run.target_lists.get(p.plat_name) + if targets is None: + targets = needed_targets(p.plat_name) + target_info = targets.get(target) + if target_info is None: + # Carrying on would install every file, which is the opposite + # of what a filter was asked for: a 4 GB download instead of + # the subset the target needs. Refuse rather than ignore. + _unknown_target(target, p.plat_name, targets) + elif target_info.get("cores") is None: + detail( + f" Target '{target}' publishes no core list: " + "installing every file" + ) + else: + before = len(p.files) + p.files, p.omitted_files = narrow_to_cores( + p.manifest, target_info["cores"], p.files, p.omitted_files, + target=target, + ) + p.slot_reach = designated_cores(p.manifest, target_info["cores"], target) + detail(f" Filtered {before} to {len(p.files)} files for target {target}") + + +def _pass_installed_cores(run: _InstallRun, p: _PlatformPass) -> None: + """The files narrowed to the cores installed, with --installed-cores.""" + args, plat_name, bios_path, manifest = run.args, p.plat_name, p.bios_path, p.manifest + found_cores = run.cores_found.get((plat_name, bios_path), []) + if "system_cores" not in manifest: + found_cores = [] + elif found_cores: + detail(f" {_plural(len(found_cores), 'core')} installed") + p.found_cores = found_cores + if args.installed_cores and "system_cores" not in manifest and by_record(args): + drop_recorded(args, plat_name, "--installed-cores", + "the list does not say which cores read which systems") + if not args.installed_cores: + return + if "system_cores" not in manifest: + raise Refusal("option", "not_recorded", plat_name, + f"Error: the {plat_name} file list does not say which cores " + "read which systems, which --installed-cores needs.", + _older_list_advice(plat_name)) + before = len(p.files) + info_dests = _unprofiled_cores_firmware( + plat_name, bios_path, manifest, found_cores, + run.cores_searched.get((plat_name, bios_path), []), + ) + p.files, p.omitted_files = narrow_to_cores( + manifest, found_cores, p.files, p.omitted_files, info_dests + ) + detail(f" Narrowed {before} to {len(p.files)} files to the installed cores") + # A slot is ranked by the cores installed, as a pack built for them. + found_reach = designated_cores(manifest, found_cores) + p.slot_reach = found_reach if p.slot_reach is None else p.slot_reach & found_reach + + +def _pass_emulators(run: _InstallRun, p: _PlatformPass) -> bool: + """--list-emulators, or the files narrowed to --emulator. True when the + setup is done with.""" + args = run.args + if args.list_emulators: + _print_emulators(p.plat_name, p.manifest, *( + leave_out_game_data(p.files, p.omitted_files)[:2] if args.no_game_data + else (p.files, p.omitted_files))) + return True + + if args.emulator and by_record(args): + for part in trim_recorded({"--emulator": list(args.emulator)}, p.plat_name, + p.manifest, p.files, p.omitted_files): + drop_recorded(args, p.plat_name, part.flag, part.why, part.keep) + if args.emulator: + names = emulator_names(p.manifest, p.files, p.omitted_files) + before = len(p.files) + chosen, p.files, p.omitted_files = _narrow_to_emulators( + args.emulator, p.manifest, p.plat_name, p.files, p.omitted_files + ) + detail(f" Narrowed {before} to {len(p.files)} files to {', '.join(chosen)}") + if args.standalone: + p.files, p.omitted_files = _standalone_builds( + args.emulator, names, p.manifest, p.plat_name, p.files, p.omitted_files) + return False + + +def _pass_version_and_saves(run: _InstallRun, p: _PlatformPass) -> None: + """The list's version said, and the files read from save and emulator + folders placed.""" + plat_name, bios_path, manifest = p.plat_name, p.bios_path, p.manifest + # Said before the install and again with the result. + note = None if run.listing or run.packing else version_note(plat_name, bios_path, manifest) + if note is not None: + print(f" {note[2]}") + run.version_notes.append(note[2]) + EVENTS.emit("version", platform=plat_name, list_version=note[0], + installed_version=note[1], message=note[2]) + + # Before the menu: it offered 23 files, then 22 were checked, the + # one read from an unknown save directory left out. Said once the + # choice is made, over the files it keeps (say_saves, say_unplaced). + p.display = display = display_name(plat_name, manifest) + if not run.packing: + # A pack keeps every root's files: the device installing it + # places them. + files, unplaced = place_save_entries( + p.files, bios_path, manifest, run.args.save_dir, display) + files, beyond = place_relative_entries(files, bios_path, manifest) + p.files, elsewhere = place_emulator_entries(files, bios_path, manifest) + p.unplaced = [*unplaced, *beyond, *elsewhere] + if not manifest.get("save_directory") and not run.packing: + # Its own list leaves these files out: naming the folder with + # --save-dir installs none of them, and was advised in a loop. + why = unlisted_saves_words(display) + p.unplaced = [_unlisted_save(entry, why) for entry in p.unplaced] + p.omitted_files = [_unlisted_save(entry, why) for entry in p.omitted_files] + + +def _unlisted_save(entry: dict, why: str) -> dict: + """entry, when read from a save directory the list names no place for, + with why in words and no remedy.""" + if entry.get("reason") != "no_save_directory": + return entry + return {**entry, "why": why, "remedy": ""} diff --git a/wiki/architecture.md b/wiki/architecture.md index 0d55a289d..694d4ecd1 100644 --- a/wiki/architecture.md +++ b/wiki/architecture.md @@ -505,7 +505,7 @@ those it imports: | Sources of files | `offline`, `network`, `lists`, `pack_source`, `folder_index`, `local_copies`, `from_folders`, `diagnosis`, `download`, `datadirs`, `standalone` | | What a run keeps | `narrowing`, `choices`, `romm`, `install_record`, `game_files`, `games_index`, `game_regions`, `game_folders`, `games`, `tally`, `space`, `fetch_plan` | | Line mode | `destination`, `line_folder`, `line_menu`, `plan_words`, `plan`, `failure_causes`, `outcome` | -| Runs and actions | `arguments`, `run_setups`, `run_files`, `run_check`, `run_download`, `run`, `pack_parts`, `pack`, `removal_plan`, `removal`, `backups`, `handheld_app`, `cli` | +| Runs and actions | `arguments`, `run_setups`, `setup_passes`, `run_files`, `run_check`, `run_download`, `run`, `pack_parts`, `pack`, `removal_plan`, `removal`, `backups`, `handheld_app`, `cli` | | Full screen | `tui_state`, `tui_routes`, `tui_commands`, `tui_keys`, `tui_paint`, `tui_choice`, `tui_folder_scan`, `tui_rows`, `tui_frame`, `tui_plan_view`, `tui_outcome`, `tui_where_views`, `tui_screens`, `tui_help`, `tui_steps`, `tui_folder_actions`, `tui_actions`, `tui_input`, `tui_receive`, `tui_list_words`, `tui_engine`, `tui_first_screen`, `tui_terminal`, `tui_session` | ## Tests