diff --git a/install.ps1 b/install.ps1 index a9e80a2f6..5ca45789c 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 = "d192a33af92cb83bdbf1d436fd4458dec4fac18f4a5cff1ea25ee8de0413630d" + $defaultInstallSha256 = "a618acce8197a5069faeba667ed15e571f07de0b5d2d1b7463da2d8511cab195" # 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 1ed487c83..c1277878a 100644 --- a/install.py +++ b/install.py @@ -605,6 +605,9 @@ RESUME_FROM_SIZE = 16 * 1024 * 1024 # --- installer/invocation.py ------------------------------------------------- +# Downloads at once when --jobs is not given. +DEFAULT_JOBS = 8 + # Options that take a value, as a hint gives them back. _VALUED_OPTIONS = ( "--system", "--core", "--emulator", "--region", "--target", "--cores-dir", "--save-dir", @@ -794,6 +797,76 @@ def offline_requested() -> bool: return os.environ.get(OFFLINE_VARIABLE, "") not in ("", "0") +# --- installer/combinations.py ----------------------------------------------- + +# --source truth, named as one option among the others. +SOURCE_TRUTH = "--source=truth" + + +def given_options(args: argparse.Namespace) -> set[str]: + """The options of a run that the combination rules read, by name.""" + given = {flag for flag, on in ( + ("--json", args.json), ("--find", bool(args.find)), ("--standalone", args.standalone), + ("--emulator", args.emulator), ("--region", args.region), + ("--one-per-slot", args.one_per_slot), ("--core", args.core), ("--system", args.system), + ("--for-games", args.for_games is not None), ("--declared-only", args.declared_only), + (SOURCE_TRUTH, args.source == "truth"), ("--tui", args.tui), ("--target", args.target), + ("--installed-cores", args.installed_cores), ("--no-game-data", args.no_game_data), + ("--all-files", args.all_files), ("--check", args.check), ("--events", args.events), + ) if on} + return given | set(listings_given(args)) + + +def combination_refusals(given: Collection[str]) -> list[str]: + """Every refusal the options given meet, in the order a run says them. + --one-per-slot, --standalone, --source truth and --find are each held + equal to the pack builder's own option, so a choice the builder refuses + beside it is refused here.""" + def has(*flags: str) -> bool: + return any(flag in given for flag in flags) + + listing = has(*(flag for flag, _attr in LISTING_FLAGS)) + truth = SOURCE_TRUTH in given + find = "--find" in given + rules = ( + (has("--json") and not find, "--json applies to --find"), + (has("--standalone") and not has("--emulator"), + "--standalone applies to --emulator: it picks that emulator's standalone build"), + (has("--standalone") and has("--region", "--one-per-slot", "--core", "--system"), + "--standalone installs each emulator's standalone build whole, which " + "--region, --one-per-slot, --core and --system do not narrow"), + (has("--one-per-slot") and has("--system", "--emulator", "--for-games"), + "--one-per-slot decides each slot over the whole list, which --system, " + "--emulator and --for-games narrow first"), + (has("--one-per-slot") and has("--declared-only"), + "--one-per-slot ranks the files cores load too, which --declared-only leaves out"), + (truth and has("--system", "--core", "--emulator", "--region", "--one-per-slot", + "--for-games", "--declared-only", "--target", "--installed-cores", + "--no-game-data"), + "--source truth installs what the emulator profiles prove is read, over " + "the whole list: run it without other choices"), + (truth and has("--tui"), "the full-screen interface has no choice for --source truth"), + (find and (truth or has("--system", "--core", "--emulator", "--region", "--one-per-slot", + "--for-games", "--declared-only", "--no-game-data", "--target", + "--installed-cores", "--all-files", "--standalone")), + "--find picks one file by its hash: run it without other choices"), + (find and (listing or has("--check", "--tui")), + "--find installs or names one file, not a listing, a check or the full screen"), + (has("--json") and has("--events"), "--json and --events each write JSON on stdout"), + (listing and (truth or has("--one-per-slot", "--standalone")), + "a listing prints the whole list, which --one-per-slot, --standalone and " + "--source do not narrow"), + ) + return [reason for refused, reason in rules if refused] + + +def adds_refusal(option: str, given: Collection[str]) -> bool: + """Whether the option, added to those given, meets a refusal they did not.""" + without = set(given) - {option} + return bool(set(combination_refusals(without | {option})) + - set(combination_refusals(without))) + + # --- installer/terminal.py --------------------------------------------------- def _kernel32() -> Any: @@ -7173,7 +7246,7 @@ class LocalSources: # The folders named with --from, read once for every platform. self.folders: list[FolderIndex] = [] # The reads at once of the files in place (--jobs). - self.jobs: int = 8 + self.jobs: int = DEFAULT_JOBS # Every folder index of the run still in use, closed at its end. self.indexes: weakref.WeakSet[FolderIndex] = weakref.WeakSet() self._serves: tuple = (None, None, frozenset()) @@ -7203,7 +7276,7 @@ class LocalSources: self.manifest = None self.pack = None self.folders = [] - self.jobs = 8 + self.jobs = DEFAULT_JOBS def close_indexes(self) -> None: for index in list(self.indexes): @@ -7379,7 +7452,7 @@ def unreachable_words(exc: OSError) -> str: def check_local( - files: list[dict], bios_path: Path, jobs: int = 8, + files: list[dict], bios_path: Path, jobs: int = DEFAULT_JOBS, term: "Terminal | None" = None, ) -> tuple[list[dict], list[dict], list[dict]]: """Check which files exist locally and have correct hashes. @@ -9988,8 +10061,8 @@ def build_parser() -> argparse.ArgumentParser: "--jobs", "-j", metavar="N", type=int, - default=8, - help="parallel download threads (default: 8)", + default=DEFAULT_JOBS, + help=f"parallel download threads (default: {DEFAULT_JOBS})", ) parser.add_argument( "--verbose", "-v", @@ -12978,7 +13051,7 @@ def _open_local_sources(args: argparse.Namespace) -> None: def reset_local_sources(args: argparse.Namespace) -> None: """Forget the sources and failures a previous run in this process kept.""" LOCAL.reset() - LOCAL.jobs = getattr(args, "jobs", 8) or 8 + LOCAL.jobs = getattr(args, "jobs", DEFAULT_JOBS) or DEFAULT_JOBS SAID_FAILED.clear() @@ -13894,53 +13967,75 @@ def _tui_keys_blocker(stdin: TextIO) -> str: return "" -def tui_blocker( +class TuiObstacle(NamedTuple): + """What keeps the full screen from running here: its kind ("flag", + "stream", "term", "drawing", "size", "keys"), the reason in words, and + the TERM or the size it was decided on.""" + kind: str + text: str + term: str = "" + size: tuple[int, int] = (0, 0) + + +def tui_obstacle( args: argparse.Namespace, stdin: TextIO | None = None, stdout: TextIO | None = None, env: Mapping[str, str] | None = None, -) -> str: - """Why the full-screen interface cannot run here, "" when it can.""" +) -> TuiObstacle | None: + """What keeps the full-screen interface from running here, None when nothing does.""" env = os.environ if env is None else env stdin = stdin or sys.stdin stdout = stdout or sys.stdout flag_blocker = _tui_flag_blocker(args) if flag_blocker: - return flag_blocker + return TuiObstacle("flag", flag_blocker) for name, stream in (("input", stdin), ("output", stdout)): isatty = getattr(stream, "isatty", None) if not (isatty and isatty()): - return f"the {name} is not a terminal" - if os.name != "nt" and env.get("TERM", "") in ("", "dumb"): - return "TERM is dumb or not set" + return TuiObstacle("stream", f"the {name} is not a terminal") + term = env.get("TERM", "") + if os.name != "nt" and term in ("", "dumb"): + return TuiObstacle("term", "TERM is dumb or not set", term=term) drawing = terminal_without_ansi(env) if drawing: - return drawing + return TuiObstacle("drawing", drawing, term=term) # A pseudo-terminal nobody sized reports 0x0: script(1) fed from a pipe # is one, and the line questions run there as they always have. cols, rows = _terminal_size(stdout) if cols < TUI_MIN_COLS or rows < TUI_MIN_ROWS: - return (f"the terminal is {cols}x{rows}, smaller than " - f"{TUI_MIN_COLS}x{TUI_MIN_ROWS}") - return _tui_keys_blocker(stdin) + return TuiObstacle("size", f"the terminal is {cols}x{rows}, smaller than " + f"{TUI_MIN_COLS}x{TUI_MIN_ROWS}", size=(cols, rows)) + keys = _tui_keys_blocker(stdin) + return TuiObstacle("keys", keys) if keys else None -def line_mode_reason(blocker: str, env: Mapping[str, str] | None = None) -> str: +def tui_blocker( + args: argparse.Namespace, stdin: TextIO | None = None, stdout: TextIO | None = None, + env: Mapping[str, str] | None = None, +) -> str: + """Why the full-screen interface cannot run here, "" when it can.""" + obstacle = tui_obstacle(args, stdin, stdout, env) + return obstacle.text if obstacle else "" + + +def line_mode_reason(obstacle: TuiObstacle | None) -> str: """Why the questions come line by line, in one line, for a terminal the full screen cannot draw on: its size or its TERM. "" for a run that asks nothing (--check, a pipe), and for a pseudo-terminal nobody sized (script fed from a pipe is 0x0), which is no window.""" - env = os.environ if env is None else env - small = re.match(r"the terminal is (\d+x\d+), smaller than", blocker) - if small: - if "0" in small[1].split("x"): + if obstacle is None: + return "" + if obstacle.kind == "size": + cols, rows = obstacle.size + if not (cols and rows): return "" - return (f"The window is {small[1]}: questions come line by line " + return (f"The window is {cols}x{rows}: questions come line by line " f"({TUI_MIN_COLS}x{TUI_MIN_ROWS} for the full screen).") - if blocker == "TERM is dumb or not set": - term = screen_text(env.get("TERM", "")) + if obstacle.kind == "term": + term = screen_text(obstacle.term) return (f"TERM={term}: questions come line by line." if term else "TERM is not set: questions come line by line.") - if blocker.startswith("TERM="): - return f"{blocker}: questions come line by line." + if obstacle.kind == "drawing": + return f"{obstacle.text}: questions come line by line." return "" @@ -14458,7 +14553,7 @@ class TuiState: # A quit the user chose, said as one (tui_quit). self.quit_asked: bool = False self.installed_only: bool = False - self.jobs: int = 8 + self.jobs: int = DEFAULT_JOBS self.passthrough: list = [] # Whether Details were opened on the result: the terminal keeps # them after Exit too. @@ -17382,7 +17477,7 @@ class _Corrupt(Exception): def download_files( - files: list[dict], bios_path: Path, jobs: int = 8, verbose: bool = False, + files: list[dict], bios_path: Path, jobs: int = DEFAULT_JOBS, verbose: bool = False, term: Terminal | None = None, progress: Progress | None = None, copies: dict[int, LocalCopy] | None = None, ) -> tuple[list[tuple[str, str]], bool]: @@ -24518,7 +24613,22 @@ def _tui_system_flags(state: TuiState) -> list[tuple[str, str | None]]: def tui_flags(state: TuiState) -> list[tuple[str, str | None]]: - """The options that carry the choices made on screen, CLI ones included.""" + """The options that carry the choices made on screen, CLI ones included. + --standalone and --one-per-slot are decided against all the others, the + install record's among them, as the run itself decides.""" + head = _tui_choice_flags(state) + tail = _tui_run_flags(state, head) + expert: list[tuple[str, str | None]] = [] + if state.standalone and _tui_standalone_offered(state, head + tail): + expert.append(("--standalone", None)) + if state.one_per_slot and _tui_slot_offered(state, head + expert + tail): + expert.append(("--one-per-slot", None)) + return head + expert + tail + + +def _tui_choice_flags(state: TuiState) -> list[tuple[str, str | None]]: + """The options of the files chosen: device, emulators, systems, cores, + regions, games and game data.""" flags = list(state.passthrough) if state.target: flags.append(("--target", state.target)) @@ -24536,22 +24646,26 @@ def tui_flags(state: TuiState) -> list[tuple[str, str | None]]: flags.append(("--required-only", None)) if state.no_game_data: flags.append(("--no-game-data", None)) - if state.standalone and _tui_standalone_offered(state): - flags.append(("--standalone", None)) - if state.one_per_slot and _tui_slot_offered(state, flags): - flags.append(("--one-per-slot", None)) - if state.installed_only and not any(flag == "--cores-dir" for flag, _v in flags): + return flags + + +def _tui_run_flags(state: TuiState, head: list[tuple[str, str | None]] + ) -> list[tuple[str, str | None]]: + """The options after the choice: installed cores, downloads at once, + the record's own, and where the files come from.""" + flags: list[tuple[str, str | None]] = [] + if state.installed_only and not any(flag == "--cores-dir" for flag, _v in head): flags.append(("--installed-cores", None)) - if state.jobs != 8: + if state.jobs != DEFAULT_JOBS: flags.append(("--jobs", str(state.jobs))) flags += state.record_flags if (state.record is not None and not state.for_games - and not any(flag in RECORD_NARROWING for flag, _value in flags)): + and not any(flag in RECORD_NARROWING for flag, _value in head + flags)): # The whole list, chosen over a recorded narrower one: said so to # the runs, whose own reading of the record would narrow it again. flags.append(("--all-files", None)) flags += [("--from", str(folder)) for folder in state.from_folders] - if state.offline and not any(flag == "--offline" for flag, _v in flags): + if state.offline and not any(flag == "--offline" for flag, _v in head + flags): flags.append(("--offline", None)) return flags @@ -24679,30 +24793,41 @@ def _tui_systems_chosen(state: TuiState) -> list[str]: return [value for _flag, value in _tui_system_flags(state) if value] -def _tui_standalone_offered(state: TuiState) -> bool: +def _tui_standalone_offered(state: TuiState, + given: list[tuple[str, str | None]] | None = None) -> bool: """Whether Change offers --standalone: emulators are chosen, the list - records a standalone build for each of them, and no region, core or - system choice narrows the run, which the option refuses beside it.""" + records a standalone build for each of them, and no other option the + run is given refuses it beside them.""" manifest = state.manifest or {} layouts = manifest.get("standalone_builds") or [] - if not state.emulators or not layouts or not _tui_standalone_fits(state): + if not state.emulators or not layouts or not _tui_standalone_fits(state, given): return False names = emulator_names(manifest, *tui_run_files(state)) return all(names.get(name) in layouts for name in state.emulators) -def _tui_standalone_fits(state: TuiState) -> bool: +def _tui_standalone_fits(state: TuiState, + given: list[tuple[str, str | None]] | None = None) -> bool: """No region, core or system choice: --standalone takes none.""" - return not (state.regions or tui_core_flags(state) or _tui_system_flags(state)) + flags = _tui_given(state) if given is None else given + return not adds_refusal("--standalone", [flag for flag, _value in flags]) -def _tui_slot_offered(state: TuiState, flags: list[tuple[str, str | None]]) -> bool: +def _tui_slot_offered(state: TuiState, + given: list[tuple[str, str | None]] | None = None) -> bool: """Whether Change offers --one-per-slot: the list records its slots and - the choice is the whole list, as the pack builder decides slots over it - (no system, emulator or game choice narrows it first).""" - return bool((state.manifest or {}).get("slot_fields")) and not any( - flag in ("--system", "--emulator", "--for-games", "--declared-only") - for flag, _value in flags) + no other option the run is given refuses it, as the pack builder decides + slots over the whole list (no system, emulator, game or declared-only + choice narrows it first).""" + flags = tui_flags(state) if given is None else given + return bool((state.manifest or {}).get("slot_fields")) and not adds_refusal( + "--one-per-slot", [flag for flag, _value in flags]) + + +def _tui_given(state: TuiState) -> list[tuple[str, str | None]]: + """The options the run is given, --standalone and --one-per-slot aside.""" + head = _tui_choice_flags(state) + return head + _tui_run_flags(state, head) def _record_key(options: Iterable[tuple[str, str | None]]) -> list: @@ -25807,7 +25932,7 @@ def _option_rows(state: TuiState) -> list[TuiRow]: what.append(TuiRow("regions", "Preferred regions: " + (", ".join(map(region_title, state.regions)) or "any"), "--region")) - if _tui_slot_offered(state, tui_flags(state)): + if _tui_slot_offered(state): what.append(TuiRow("slot", "One BIOS per console and region: " + _yes(state.one_per_slot), "--one-per-slot")) if len(choices["cores"]) > 1: @@ -32323,44 +32448,10 @@ def _tui_cleanup( def _check_expert(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None: """--one-per-slot, --standalone, --source truth and --find, applied or - refused, never ignored. Each is held equal to the pack builder's own - option, so a choice the builder refuses beside it is refused here.""" - listing = bool(listings_given(args)) - truth = args.source == "truth" - refusals = ( - (args.json and not args.find, "--json applies to --find"), - (args.standalone and not args.emulator, - "--standalone applies to --emulator: it picks that emulator's standalone build"), - (args.standalone and (args.region or args.one_per_slot or args.core or args.system), - "--standalone installs each emulator's standalone build whole, which " - "--region, --one-per-slot, --core and --system do not narrow"), - (args.one_per_slot and (args.system or args.emulator or args.for_games is not None), - "--one-per-slot decides each slot over the whole list, which --system, " - "--emulator and --for-games narrow first"), - (args.one_per_slot and args.declared_only, - "--one-per-slot ranks the files cores load too, which --declared-only leaves out"), - (truth and (args.system or args.core or args.emulator or args.region - or args.one_per_slot or args.for_games is not None - or args.declared_only or args.target or args.installed_cores - or args.no_game_data), - "--source truth installs what the emulator profiles prove is read, over " - "the whole list: run it without other choices"), - (truth and args.tui, "the full-screen interface has no choice for --source truth"), - (bool(args.find) and (args.system or args.core or args.emulator or args.region - or args.one_per_slot or truth or args.for_games is not None - or args.declared_only or args.no_game_data or args.target - or args.installed_cores or args.all_files or args.standalone), - "--find picks one file by its hash: run it without other choices"), - (bool(args.find) and (listing or args.check or args.tui), - "--find installs or names one file, not a listing, a check or the full screen"), - (args.json and args.events, "--json and --events each write JSON on stdout"), - (listing and (args.one_per_slot or args.standalone or truth), - "a listing prints the whole list, which --one-per-slot, --standalone and " - "--source do not narrow"), - ) - for given, reason in refusals: - if given: - parser.error(reason) + refused, never ignored (combination_refusals).""" + refusals = combination_refusals(given_options(args)) + if refusals: + parser.error(refusals[0]) if args.find: # Each would be skipped by a run that installs one file or prints it. for flag, given in (("--pack", getattr(args, "pack", None) is not None), @@ -32561,14 +32652,14 @@ def _main_dispatch(args: argparse.Namespace) -> None: # rest, and the run keeps the platform's own list. # Nor for --source truth and --find, which choose the files themselves. lined = args.declared_only or args.source == "truth" or bool(args.find) - blocker = "" if args.tui else tui_blocker(args) - if args.tui or not (plain_requested(args) or lined or blocker): + obstacle = None if args.tui else tui_obstacle(args) + if args.tui or not (plain_requested(args) or lined or obstacle): sys.exit(run_tui(args)) if not (plain_requested(args) or lined): # The questions came line by line with no word of why: the page # says a smaller window says so, and a TERM # unset or dumb said nothing. - said = line_mode_reason(blocker) + said = line_mode_reason(obstacle) if said: print(fold_line(said)) if args.events: diff --git a/install.sh b/install.sh index d843e2315..11d6fcc03 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="d192a33af92cb83bdbf1d436fd4458dec4fac18f4a5cff1ea25ee8de0413630d" +DEFAULT_INSTALL_SHA256="a618acce8197a5069faeba667ed15e571f07de0b5d2d1b7463da2d8511cab195" # 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 f16cc8fea..df04429d0 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": 1489469, - "sha256": "d192a33af92cb83bdbf1d436fd4458dec4fac18f4a5cff1ea25ee8de0413630d", + "size": 1493467, + "sha256": "a618acce8197a5069faeba667ed15e571f07de0b5d2d1b7463da2d8511cab195", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1543094, - "sha256": "aa846698dc8dcfcc8dcf175ef0664c72de09ab34cece612cbb5c5de3e56c1310" + "size": 1547092, + "sha256": "4c4aa576277413d849cf173fe4fe9bbd7921e4994332adabdb0386b38cdea7ab" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1489469, - "sha256": "d192a33af92cb83bdbf1d436fd4458dec4fac18f4a5cff1ea25ee8de0413630d", + "size": 1493467, + "sha256": "a618acce8197a5069faeba667ed15e571f07de0b5d2d1b7463da2d8511cab195", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1489469, - "sha256": "d192a33af92cb83bdbf1d436fd4458dec4fac18f4a5cff1ea25ee8de0413630d", + "size": 1493467, + "sha256": "a618acce8197a5069faeba667ed15e571f07de0b5d2d1b7463da2d8511cab195", "executable": false }, { diff --git a/installer/arguments.py b/installer/arguments.py index a2b97228f..55353c82a 100644 --- a/installer/arguments.py +++ b/installer/arguments.py @@ -5,7 +5,7 @@ import argparse import re from pathlib import Path -from installer.invocation import OFFLINE_VARIABLE, _INVOCATION, installer_id +from installer.invocation import DEFAULT_JOBS, OFFLINE_VARIABLE, _INVOCATION, installer_id from installer.remote import INSTALLER_PAGE, active_remote from installer.safe_replace import BACKUP_DIR from installer.text import format_size @@ -390,8 +390,8 @@ def build_parser() -> argparse.ArgumentParser: "--jobs", "-j", metavar="N", type=int, - default=8, - help="parallel download threads (default: 8)", + default=DEFAULT_JOBS, + help=f"parallel download threads (default: {DEFAULT_JOBS})", ) parser.add_argument( "--verbose", "-v", diff --git a/installer/cli.py b/installer/cli.py index 211f57847..df174c26d 100644 --- a/installer/cli.py +++ b/installer/cli.py @@ -11,6 +11,7 @@ from pathlib import Path from typing import NoReturn from installer.arguments import PACK_NAME_END, build_parser +from installer.combinations import combination_refusals, given_options from installer.console import OUTPUT, fold_line from installer.detect_os import detect_os from installer.events import EVENTS, EVENT_SCHEMA, _event_exit_code, _event_mode @@ -35,50 +36,23 @@ from installer.regions import resolve_regions from installer.remote import remote_from_env, remote_in_use from installer.removal import run_backups, run_delete_backup, run_removal from installer.run import _install -from installer.tui_routes import TUI_LIST_ENV, TUI_RECORD_ENV, line_mode_reason, plain_requested, tui_blocker +from installer.tui_routes import ( + TUI_LIST_ENV, + TUI_RECORD_ENV, + line_mode_reason, + plain_requested, + tui_blocker, + tui_obstacle, +) from installer.tui_session import run_tui def _check_expert(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None: """--one-per-slot, --standalone, --source truth and --find, applied or - refused, never ignored. Each is held equal to the pack builder's own - option, so a choice the builder refuses beside it is refused here.""" - listing = bool(listings_given(args)) - truth = args.source == "truth" - refusals = ( - (args.json and not args.find, "--json applies to --find"), - (args.standalone and not args.emulator, - "--standalone applies to --emulator: it picks that emulator's standalone build"), - (args.standalone and (args.region or args.one_per_slot or args.core or args.system), - "--standalone installs each emulator's standalone build whole, which " - "--region, --one-per-slot, --core and --system do not narrow"), - (args.one_per_slot and (args.system or args.emulator or args.for_games is not None), - "--one-per-slot decides each slot over the whole list, which --system, " - "--emulator and --for-games narrow first"), - (args.one_per_slot and args.declared_only, - "--one-per-slot ranks the files cores load too, which --declared-only leaves out"), - (truth and (args.system or args.core or args.emulator or args.region - or args.one_per_slot or args.for_games is not None - or args.declared_only or args.target or args.installed_cores - or args.no_game_data), - "--source truth installs what the emulator profiles prove is read, over " - "the whole list: run it without other choices"), - (truth and args.tui, "the full-screen interface has no choice for --source truth"), - (bool(args.find) and (args.system or args.core or args.emulator or args.region - or args.one_per_slot or truth or args.for_games is not None - or args.declared_only or args.no_game_data or args.target - or args.installed_cores or args.all_files or args.standalone), - "--find picks one file by its hash: run it without other choices"), - (bool(args.find) and (listing or args.check or args.tui), - "--find installs or names one file, not a listing, a check or the full screen"), - (args.json and args.events, "--json and --events each write JSON on stdout"), - (listing and (args.one_per_slot or args.standalone or truth), - "a listing prints the whole list, which --one-per-slot, --standalone and " - "--source do not narrow"), - ) - for given, reason in refusals: - if given: - parser.error(reason) + refused, never ignored (combination_refusals).""" + refusals = combination_refusals(given_options(args)) + if refusals: + parser.error(refusals[0]) if args.find: # Each would be skipped by a run that installs one file or prints it. for flag, given in (("--pack", getattr(args, "pack", None) is not None), @@ -279,14 +253,14 @@ def _main_dispatch(args: argparse.Namespace) -> None: # rest, and the run keeps the platform's own list. # Nor for --source truth and --find, which choose the files themselves. lined = args.declared_only or args.source == "truth" or bool(args.find) - blocker = "" if args.tui else tui_blocker(args) - if args.tui or not (plain_requested(args) or lined or blocker): + obstacle = None if args.tui else tui_obstacle(args) + if args.tui or not (plain_requested(args) or lined or obstacle): sys.exit(run_tui(args)) if not (plain_requested(args) or lined): # The questions came line by line with no word of why: the page # says a smaller window says so, and a TERM # unset or dumb said nothing. - said = line_mode_reason(blocker) + said = line_mode_reason(obstacle) if said: print(fold_line(said)) if args.events: diff --git a/installer/combinations.py b/installer/combinations.py new file mode 100644 index 000000000..202d32a88 --- /dev/null +++ b/installer/combinations.py @@ -0,0 +1,74 @@ +"""Which options the installer refuses beside each other, and why.""" +from __future__ import annotations + +import argparse +from collections.abc import Collection + +from installer.invocation import LISTING_FLAGS, listings_given + +# --source truth, named as one option among the others. +SOURCE_TRUTH = "--source=truth" + + +def given_options(args: argparse.Namespace) -> set[str]: + """The options of a run that the combination rules read, by name.""" + given = {flag for flag, on in ( + ("--json", args.json), ("--find", bool(args.find)), ("--standalone", args.standalone), + ("--emulator", args.emulator), ("--region", args.region), + ("--one-per-slot", args.one_per_slot), ("--core", args.core), ("--system", args.system), + ("--for-games", args.for_games is not None), ("--declared-only", args.declared_only), + (SOURCE_TRUTH, args.source == "truth"), ("--tui", args.tui), ("--target", args.target), + ("--installed-cores", args.installed_cores), ("--no-game-data", args.no_game_data), + ("--all-files", args.all_files), ("--check", args.check), ("--events", args.events), + ) if on} + return given | set(listings_given(args)) + + +def combination_refusals(given: Collection[str]) -> list[str]: + """Every refusal the options given meet, in the order a run says them. + --one-per-slot, --standalone, --source truth and --find are each held + equal to the pack builder's own option, so a choice the builder refuses + beside it is refused here.""" + def has(*flags: str) -> bool: + return any(flag in given for flag in flags) + + listing = has(*(flag for flag, _attr in LISTING_FLAGS)) + truth = SOURCE_TRUTH in given + find = "--find" in given + rules = ( + (has("--json") and not find, "--json applies to --find"), + (has("--standalone") and not has("--emulator"), + "--standalone applies to --emulator: it picks that emulator's standalone build"), + (has("--standalone") and has("--region", "--one-per-slot", "--core", "--system"), + "--standalone installs each emulator's standalone build whole, which " + "--region, --one-per-slot, --core and --system do not narrow"), + (has("--one-per-slot") and has("--system", "--emulator", "--for-games"), + "--one-per-slot decides each slot over the whole list, which --system, " + "--emulator and --for-games narrow first"), + (has("--one-per-slot") and has("--declared-only"), + "--one-per-slot ranks the files cores load too, which --declared-only leaves out"), + (truth and has("--system", "--core", "--emulator", "--region", "--one-per-slot", + "--for-games", "--declared-only", "--target", "--installed-cores", + "--no-game-data"), + "--source truth installs what the emulator profiles prove is read, over " + "the whole list: run it without other choices"), + (truth and has("--tui"), "the full-screen interface has no choice for --source truth"), + (find and (truth or has("--system", "--core", "--emulator", "--region", "--one-per-slot", + "--for-games", "--declared-only", "--no-game-data", "--target", + "--installed-cores", "--all-files", "--standalone")), + "--find picks one file by its hash: run it without other choices"), + (find and (listing or has("--check", "--tui")), + "--find installs or names one file, not a listing, a check or the full screen"), + (has("--json") and has("--events"), "--json and --events each write JSON on stdout"), + (listing and (truth or has("--one-per-slot", "--standalone")), + "a listing prints the whole list, which --one-per-slot, --standalone and " + "--source do not narrow"), + ) + return [reason for refused, reason in rules if refused] + + +def adds_refusal(option: str, given: Collection[str]) -> bool: + """Whether the option, added to those given, meets a refusal they did not.""" + without = set(given) - {option} + return bool(set(combination_refusals(without | {option})) + - set(combination_refusals(without))) diff --git a/installer/download.py b/installer/download.py index 538af5438..8edb549a0 100644 --- a/installer/download.py +++ b/installer/download.py @@ -34,7 +34,7 @@ from installer.host_fs import ( _write_refused, _writing, ) -from installer.invocation import offline_requested +from installer.invocation import DEFAULT_JOBS, offline_requested from installer.local_check import _digest_file, _digest_handle, expected_hashes, unreachable_words from installer.network import _retry_after_failure, _server_delay, network_words from installer.offline import LOCAL, offline_words @@ -561,7 +561,7 @@ class _Corrupt(Exception): def download_files( - files: list[dict], bios_path: Path, jobs: int = 8, verbose: bool = False, + files: list[dict], bios_path: Path, jobs: int = DEFAULT_JOBS, verbose: bool = False, term: Terminal | None = None, progress: Progress | None = None, copies: dict[int, LocalCopy] | None = None, ) -> tuple[list[tuple[str, str]], bool]: diff --git a/installer/from_folders.py b/installer/from_folders.py index 2d19f36e5..ef077d750 100644 --- a/installer/from_folders.py +++ b/installer/from_folders.py @@ -13,6 +13,7 @@ from typing import IO, TYPE_CHECKING, Any from installer.console import SAID_FAILED from installer.events import EVENTS from installer.host_fs import _REPARSE_POINT +from installer.invocation import DEFAULT_JOBS from installer.local_check import _digest_file, _digest_handle, _sha1_file from installer.offline import LOCAL, local_manifest_platform from installer.pack_source import _PART_FILE, PackSource @@ -813,7 +814,7 @@ def _open_local_sources(args: argparse.Namespace) -> None: def reset_local_sources(args: argparse.Namespace) -> None: """Forget the sources and failures a previous run in this process kept.""" LOCAL.reset() - LOCAL.jobs = getattr(args, "jobs", 8) or 8 + LOCAL.jobs = getattr(args, "jobs", DEFAULT_JOBS) or DEFAULT_JOBS SAID_FAILED.clear() diff --git a/installer/invocation.py b/installer/invocation.py index 2974cea52..c3493581c 100644 --- a/installer/invocation.py +++ b/installer/invocation.py @@ -11,6 +11,9 @@ from pathlib import Path from installer.remote import BOOTSTRAP_URL +# Downloads at once when --jobs is not given. +DEFAULT_JOBS = 8 + # Options that take a value, as a hint gives them back. _VALUED_OPTIONS = ( "--system", "--core", "--emulator", "--region", "--target", "--cores-dir", "--save-dir", diff --git a/installer/local_check.py b/installer/local_check.py index 81bed922c..4bab7642c 100644 --- a/installer/local_check.py +++ b/installer/local_check.py @@ -9,6 +9,7 @@ from typing import IO, TYPE_CHECKING from installer.console import ReadCounter, _live_stdout from installer.host_fs import _held, _is_file +from installer.invocation import DEFAULT_JOBS from installer.offline import LOCAL from installer.paths import _entry_path @@ -127,7 +128,7 @@ def unreachable_words(exc: OSError) -> str: def check_local( - files: list[dict], bios_path: Path, jobs: int = 8, + files: list[dict], bios_path: Path, jobs: int = DEFAULT_JOBS, term: "Terminal | None" = None, ) -> tuple[list[dict], list[dict], list[dict]]: """Check which files exist locally and have correct hashes. diff --git a/installer/offline.py b/installer/offline.py index d5bfd5875..ac1125f95 100644 --- a/installer/offline.py +++ b/installer/offline.py @@ -7,7 +7,7 @@ from pathlib import Path from typing import TYPE_CHECKING from installer.host_fs import read_local_json -from installer.invocation import OFFLINE_VARIABLE, offline_requested +from installer.invocation import DEFAULT_JOBS, OFFLINE_VARIABLE, offline_requested from installer.platforms import AVAILABLE_PLATFORMS from installer.remote import MAX_MANIFEST_BYTES @@ -26,7 +26,7 @@ class LocalSources: # The folders named with --from, read once for every platform. self.folders: list[FolderIndex] = [] # The reads at once of the files in place (--jobs). - self.jobs: int = 8 + self.jobs: int = DEFAULT_JOBS # Every folder index of the run still in use, closed at its end. self.indexes: weakref.WeakSet[FolderIndex] = weakref.WeakSet() self._serves: tuple = (None, None, frozenset()) @@ -56,7 +56,7 @@ class LocalSources: self.manifest = None self.pack = None self.folders = [] - self.jobs = 8 + self.jobs = DEFAULT_JOBS def close_indexes(self) -> None: for index in list(self.indexes): diff --git a/installer/tui_commands.py b/installer/tui_commands.py index 67b9a8409..feedc36f8 100644 --- a/installer/tui_commands.py +++ b/installer/tui_commands.py @@ -8,10 +8,11 @@ from pathlib import Path from typing import TYPE_CHECKING from installer.choices import choosable_cores, emulator_names +from installer.combinations import adds_refusal from installer.handheld_app import card_app from installer.host_fs import _is_dir from installer.install_record import RECORD_NARROWING, record_argv, record_options -from installer.invocation import Invocation +from installer.invocation import DEFAULT_JOBS, Invocation from installer.offline import LOCAL from installer.tally import leave_out_game_data from installer.tui_routes import TUI_LIST_ENV, TUI_RECORD_ENV @@ -45,7 +46,22 @@ def _tui_system_flags(state: TuiState) -> list[tuple[str, str | None]]: def tui_flags(state: TuiState) -> list[tuple[str, str | None]]: - """The options that carry the choices made on screen, CLI ones included.""" + """The options that carry the choices made on screen, CLI ones included. + --standalone and --one-per-slot are decided against all the others, the + install record's among them, as the run itself decides.""" + head = _tui_choice_flags(state) + tail = _tui_run_flags(state, head) + expert: list[tuple[str, str | None]] = [] + if state.standalone and _tui_standalone_offered(state, head + tail): + expert.append(("--standalone", None)) + if state.one_per_slot and _tui_slot_offered(state, head + expert + tail): + expert.append(("--one-per-slot", None)) + return head + expert + tail + + +def _tui_choice_flags(state: TuiState) -> list[tuple[str, str | None]]: + """The options of the files chosen: device, emulators, systems, cores, + regions, games and game data.""" flags = list(state.passthrough) if state.target: flags.append(("--target", state.target)) @@ -63,22 +79,26 @@ def tui_flags(state: TuiState) -> list[tuple[str, str | None]]: flags.append(("--required-only", None)) if state.no_game_data: flags.append(("--no-game-data", None)) - if state.standalone and _tui_standalone_offered(state): - flags.append(("--standalone", None)) - if state.one_per_slot and _tui_slot_offered(state, flags): - flags.append(("--one-per-slot", None)) - if state.installed_only and not any(flag == "--cores-dir" for flag, _v in flags): + return flags + + +def _tui_run_flags(state: TuiState, head: list[tuple[str, str | None]] + ) -> list[tuple[str, str | None]]: + """The options after the choice: installed cores, downloads at once, + the record's own, and where the files come from.""" + flags: list[tuple[str, str | None]] = [] + if state.installed_only and not any(flag == "--cores-dir" for flag, _v in head): flags.append(("--installed-cores", None)) - if state.jobs != 8: + if state.jobs != DEFAULT_JOBS: flags.append(("--jobs", str(state.jobs))) flags += state.record_flags if (state.record is not None and not state.for_games - and not any(flag in RECORD_NARROWING for flag, _value in flags)): + and not any(flag in RECORD_NARROWING for flag, _value in head + flags)): # The whole list, chosen over a recorded narrower one: said so to # the runs, whose own reading of the record would narrow it again. flags.append(("--all-files", None)) flags += [("--from", str(folder)) for folder in state.from_folders] - if state.offline and not any(flag == "--offline" for flag, _v in flags): + if state.offline and not any(flag == "--offline" for flag, _v in head + flags): flags.append(("--offline", None)) return flags @@ -206,30 +226,41 @@ def _tui_systems_chosen(state: TuiState) -> list[str]: return [value for _flag, value in _tui_system_flags(state) if value] -def _tui_standalone_offered(state: TuiState) -> bool: +def _tui_standalone_offered(state: TuiState, + given: list[tuple[str, str | None]] | None = None) -> bool: """Whether Change offers --standalone: emulators are chosen, the list - records a standalone build for each of them, and no region, core or - system choice narrows the run, which the option refuses beside it.""" + records a standalone build for each of them, and no other option the + run is given refuses it beside them.""" manifest = state.manifest or {} layouts = manifest.get("standalone_builds") or [] - if not state.emulators or not layouts or not _tui_standalone_fits(state): + if not state.emulators or not layouts or not _tui_standalone_fits(state, given): return False names = emulator_names(manifest, *tui_run_files(state)) return all(names.get(name) in layouts for name in state.emulators) -def _tui_standalone_fits(state: TuiState) -> bool: +def _tui_standalone_fits(state: TuiState, + given: list[tuple[str, str | None]] | None = None) -> bool: """No region, core or system choice: --standalone takes none.""" - return not (state.regions or tui_core_flags(state) or _tui_system_flags(state)) + flags = _tui_given(state) if given is None else given + return not adds_refusal("--standalone", [flag for flag, _value in flags]) -def _tui_slot_offered(state: TuiState, flags: list[tuple[str, str | None]]) -> bool: +def _tui_slot_offered(state: TuiState, + given: list[tuple[str, str | None]] | None = None) -> bool: """Whether Change offers --one-per-slot: the list records its slots and - the choice is the whole list, as the pack builder decides slots over it - (no system, emulator or game choice narrows it first).""" - return bool((state.manifest or {}).get("slot_fields")) and not any( - flag in ("--system", "--emulator", "--for-games", "--declared-only") - for flag, _value in flags) + no other option the run is given refuses it, as the pack builder decides + slots over the whole list (no system, emulator, game or declared-only + choice narrows it first).""" + flags = tui_flags(state) if given is None else given + return bool((state.manifest or {}).get("slot_fields")) and not adds_refusal( + "--one-per-slot", [flag for flag, _value in flags]) + + +def _tui_given(state: TuiState) -> list[tuple[str, str | None]]: + """The options the run is given, --standalone and --one-per-slot aside.""" + head = _tui_choice_flags(state) + return head + _tui_run_flags(state, head) def _record_key(options: Iterable[tuple[str, str | None]]) -> list: diff --git a/installer/tui_routes.py b/installer/tui_routes.py index 837ec9dd7..e9cff816d 100644 --- a/installer/tui_routes.py +++ b/installer/tui_routes.py @@ -4,7 +4,6 @@ from __future__ import annotations import argparse import io import os -import re import sys from collections.abc import Mapping from typing import TYPE_CHECKING, NamedTuple, TextIO @@ -230,53 +229,75 @@ def _tui_keys_blocker(stdin: TextIO) -> str: return "" -def tui_blocker( +class TuiObstacle(NamedTuple): + """What keeps the full screen from running here: its kind ("flag", + "stream", "term", "drawing", "size", "keys"), the reason in words, and + the TERM or the size it was decided on.""" + kind: str + text: str + term: str = "" + size: tuple[int, int] = (0, 0) + + +def tui_obstacle( args: argparse.Namespace, stdin: TextIO | None = None, stdout: TextIO | None = None, env: Mapping[str, str] | None = None, -) -> str: - """Why the full-screen interface cannot run here, "" when it can.""" +) -> TuiObstacle | None: + """What keeps the full-screen interface from running here, None when nothing does.""" env = os.environ if env is None else env stdin = stdin or sys.stdin stdout = stdout or sys.stdout flag_blocker = _tui_flag_blocker(args) if flag_blocker: - return flag_blocker + return TuiObstacle("flag", flag_blocker) for name, stream in (("input", stdin), ("output", stdout)): isatty = getattr(stream, "isatty", None) if not (isatty and isatty()): - return f"the {name} is not a terminal" - if os.name != "nt" and env.get("TERM", "") in ("", "dumb"): - return "TERM is dumb or not set" + return TuiObstacle("stream", f"the {name} is not a terminal") + term = env.get("TERM", "") + if os.name != "nt" and term in ("", "dumb"): + return TuiObstacle("term", "TERM is dumb or not set", term=term) drawing = terminal_without_ansi(env) if drawing: - return drawing + return TuiObstacle("drawing", drawing, term=term) # A pseudo-terminal nobody sized reports 0x0: script(1) fed from a pipe # is one, and the line questions run there as they always have. cols, rows = _terminal_size(stdout) if cols < TUI_MIN_COLS or rows < TUI_MIN_ROWS: - return (f"the terminal is {cols}x{rows}, smaller than " - f"{TUI_MIN_COLS}x{TUI_MIN_ROWS}") - return _tui_keys_blocker(stdin) + return TuiObstacle("size", f"the terminal is {cols}x{rows}, smaller than " + f"{TUI_MIN_COLS}x{TUI_MIN_ROWS}", size=(cols, rows)) + keys = _tui_keys_blocker(stdin) + return TuiObstacle("keys", keys) if keys else None -def line_mode_reason(blocker: str, env: Mapping[str, str] | None = None) -> str: +def tui_blocker( + args: argparse.Namespace, stdin: TextIO | None = None, stdout: TextIO | None = None, + env: Mapping[str, str] | None = None, +) -> str: + """Why the full-screen interface cannot run here, "" when it can.""" + obstacle = tui_obstacle(args, stdin, stdout, env) + return obstacle.text if obstacle else "" + + +def line_mode_reason(obstacle: TuiObstacle | None) -> str: """Why the questions come line by line, in one line, for a terminal the full screen cannot draw on: its size or its TERM. "" for a run that asks nothing (--check, a pipe), and for a pseudo-terminal nobody sized (script fed from a pipe is 0x0), which is no window.""" - env = os.environ if env is None else env - small = re.match(r"the terminal is (\d+x\d+), smaller than", blocker) - if small: - if "0" in small[1].split("x"): + if obstacle is None: + return "" + if obstacle.kind == "size": + cols, rows = obstacle.size + if not (cols and rows): return "" - return (f"The window is {small[1]}: questions come line by line " + return (f"The window is {cols}x{rows}: questions come line by line " f"({TUI_MIN_COLS}x{TUI_MIN_ROWS} for the full screen).") - if blocker == "TERM is dumb or not set": - term = screen_text(env.get("TERM", "")) + if obstacle.kind == "term": + term = screen_text(obstacle.term) return (f"TERM={term}: questions come line by line." if term else "TERM is not set: questions come line by line.") - if blocker.startswith("TERM="): - return f"{blocker}: questions come line by line." + if obstacle.kind == "drawing": + return f"{obstacle.text}: questions come line by line." return "" diff --git a/installer/tui_rows.py b/installer/tui_rows.py index 3b71be581..19daedb7d 100644 --- a/installer/tui_rows.py +++ b/installer/tui_rows.py @@ -66,7 +66,6 @@ from installer.tui_commands import ( _tui_app_ready, _tui_slot_offered, _tui_standalone_offered, - tui_flags, tui_record_kept, ) from installer.tui_folder_scan import FolderScan @@ -518,7 +517,7 @@ def _option_rows(state: TuiState) -> list[TuiRow]: what.append(TuiRow("regions", "Preferred regions: " + (", ".join(map(region_title, state.regions)) or "any"), "--region")) - if _tui_slot_offered(state, tui_flags(state)): + if _tui_slot_offered(state): what.append(TuiRow("slot", "One BIOS per console and region: " + _yes(state.one_per_slot), "--one-per-slot")) if len(choices["cores"]) > 1: diff --git a/installer/tui_state.py b/installer/tui_state.py index 577c68770..f8a83da19 100644 --- a/installer/tui_state.py +++ b/installer/tui_state.py @@ -6,7 +6,7 @@ import unicodedata from pathlib import Path from typing import TYPE_CHECKING, NamedTuple -from installer.invocation import offline_requested +from installer.invocation import DEFAULT_JOBS, offline_requested from installer.offline import LOCAL if TYPE_CHECKING: @@ -266,7 +266,7 @@ class TuiState: # A quit the user chose, said as one (tui_quit). self.quit_asked: bool = False self.installed_only: bool = False - self.jobs: int = 8 + self.jobs: int = DEFAULT_JOBS self.passthrough: list = [] # Whether Details were opened on the result: the terminal keeps # them after Exit too. diff --git a/tests/test_install_expert.py b/tests/test_install_expert.py index 5caf33284..8417a212d 100644 --- a/tests/test_install_expert.py +++ b/tests/test_install_expert.py @@ -663,6 +663,29 @@ class ChangeRows(unittest.TestCase): self.assertNotIn("slot", self.actions(state)) self.assertNotIn(("--one-per-slot", None), install.tui_flags(state)) + def test_one_per_slot_beside_a_recorded_declared_only(self): + """The record's --declared-only came after the check that offers + --one-per-slot: the argv carried both and every run was refused.""" + state = self.state(LIST) + state.one_per_slot = True + state.record_flags = [("--declared-only", None)] + flags = install.tui_flags(state) + self.assertNotIn(("--one-per-slot", None), flags) + self.assertNotIn("slot", self.actions(state)) + argv = install.tui_argv(state, "check", "python", "install.py") + args = install.build_parser().parse_args(argv[2:]) + self.assertEqual(install.combination_refusals(install.given_options(args)), []) + + def test_the_cli_and_the_screen_read_one_rule(self): + """Each refusal the screen avoids is the one the run says.""" + refusals = install.combination_refusals + self.assertEqual(refusals({"--one-per-slot", "--declared-only"}), + ["--one-per-slot ranks the files cores load too, which " + "--declared-only leaves out"]) + self.assertTrue(install.adds_refusal("--one-per-slot", {"--system"})) + self.assertFalse(install.adds_refusal("--one-per-slot", {"--region"})) + self.assertFalse(install.adds_refusal("--standalone", {"--emulator"})) + def test_standalone_when_the_emulator_has_a_build(self): state = self.state(LIST) self.assertNotIn("standalone", self.actions(state)) diff --git a/tests/test_install_tui.py b/tests/test_install_tui.py index d5007eff2..5ca5c377d 100644 --- a/tests/test_install_tui.py +++ b/tests/test_install_tui.py @@ -9436,18 +9436,27 @@ class LineModeAndScreenDecideAlike(_LineModeFindings, _OutsideTextFindings, """TERM unset or dumb gave line questions without a word, where a small window said why.""" reason = install.line_mode_reason - self.assertEqual(reason("TERM is dumb or not set", {}), - "TERM is not set: questions come line by line.") - self.assertEqual(reason("TERM is dumb or not set", {"TERM": "dumb"}), - "TERM=dumb: questions come line by line.") - self.assertEqual(reason("TERM=vt52 cannot move the cursor", {}), + tty = mock.Mock(isatty=lambda: True) + + def said(env: dict, size: tuple[int, int] = (80, 24), drawing: str = "", + **given) -> str: + with mock.patch.object(install, "_terminal_size", return_value=size), \ + mock.patch.object(install, "terminal_without_ansi", return_value=drawing), \ + mock.patch.object(install, "_tui_keys_blocker", return_value=""), \ + mock.patch.object(install.os, "name", "posix"): + return reason(install.tui_obstacle(_args(**given), tty, tty, env)) + + self.assertEqual(said({}), "TERM is not set: questions come line by line.") + self.assertEqual(said({"TERM": "dumb"}), "TERM=dumb: questions come line by line.") + self.assertEqual(said({"TERM": "vt52"}, drawing="TERM=vt52 cannot move the cursor"), "TERM=vt52 cannot move the cursor: questions come line by line.") - self.assertEqual(reason("the terminal is 39x12, smaller than 40x12", {}), + self.assertEqual(said({"TERM": "xterm"}, (39, 12)), "The window is 39x12: questions come line by line (40x12 for the " "full screen).") - self.assertEqual(reason("the terminal is 0x0, smaller than 40x12", {}), "") - self.assertEqual(reason("--check runs without questions", {}), "") - self.assertEqual(reason("the input is not a terminal", {}), "") + self.assertEqual(said({"TERM": "xterm"}, (0, 0)), "") + self.assertEqual(said({"TERM": "xterm"}, check=True), "") + self.assertEqual(reason(install.tui_obstacle(_args(), io.StringIO(), tty, {})), "") + self.assertEqual(said({"TERM": "xterm"}), "") def test_game_data_has_one_wording_and_one_decision(self): """Five wordings, "No game data" read as a @@ -10879,8 +10888,9 @@ class CardNamesAndHelp(_CardFixtures, unittest.TestCase): self.assertIn("--offline.", end) def test_a_small_window_says_why_the_questions_come_line_by_line(self): - with mock.patch.object(install, "tui_blocker", - return_value="the terminal is 39x12, smaller than 40x12"), \ + small = install.TuiObstacle("size", "the terminal is 39x12, smaller than 40x12", + size=(39, 12)) + with mock.patch.object(install, "tui_obstacle", return_value=small), \ mock.patch.object(install, "_run", side_effect=SystemExit(0)), \ mock.patch.object(install, "detect_os", return_value="linux"), \ mock.patch.dict(install._WSL), \ diff --git a/wiki/architecture.md b/wiki/architecture.md index 6c71cb292..a0dda0dce 100644 --- a/wiki/architecture.md +++ b/wiki/architecture.md @@ -498,7 +498,7 @@ those it imports: | Concern | Modules | |---------|---------| -| Shared words and limits | `remote`, `regions`, `platforms`, `manifest`, `embedded`, `text`, `events`, `refusal`, `invocation` | +| Shared words and limits | `remote`, `regions`, `platforms`, `manifest`, `embedded`, `text`, `events`, `refusal`, `invocation`, `combinations` | | Terminal and line output | `terminal`, `console` | | Files and folders | `host_fs`, `paths`, `filesystems`, `config_values`, `local_check`, `safe_replace` | | This machine | `detection_notes`, `detect_os`, `detect_frontends`, `cards`, `device`, `cores`, `frontend_folders`, `setups` |