diff --git a/install.ps1 b/install.ps1 index 75ef4160b..a7abcf39f 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 = "d245aefa5fc0b6ee54bccbaf2b5788c8c032aac4f7f25e9967328ed3dc7bd8e3" + $defaultInstallSha256 = "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c" # 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 f6f8a4ee6..09bda9849 100644 --- a/install.py +++ b/install.py @@ -14927,6 +14927,29 @@ def _refuse_file(dest: Path, named: str) -> None: f"the file {named}.", message=where) +class MissingFolder(NamedTuple): + """What a BIOS folder that does not exist yet needs before it is made.""" + nearest: Path + missing: int + # "create": one level, a BIOS folder not made yet, made without a + # question. "ask": several, a path that may be mistyped, asked first + # with no as the answer when it can be asked. + verdict: str + + +def missing_folder(dest: Path) -> MissingFolder | None: + """None when dest exists, else its nearest existing folder, how many + levels are missing below it and whether they are made or asked for: + the one rule line mode and the full screen both follow.""" + if dest.exists(): + return None + nearest = dest.absolute() + while not nearest.exists() and nearest.parent != nearest: + nearest = nearest.parent + missing = len(dest.absolute().relative_to(nearest).parts) + return MissingFolder(nearest, missing, "ask" if missing > 1 else "create") + + def _missing_dest(dest: Path, ask: bool, named: str = "named with --dest", again: bool = False) -> bool: """Say that a --dest folder does not exist before anything goes there. @@ -14942,17 +14965,16 @@ def _missing_dest(dest: Path, ask: bool, named: str = "named with --dest", """ _refuse_closed(dest, named) _refuse_file(dest, named) - if dest.exists(): + found = missing_folder(dest) + if found is None: return True - nearest = dest.absolute() - while not nearest.exists() and nearest.parent != nearest: - nearest = nearest.parent - missing = len(dest.absolute().relative_to(nearest).parts) + nearest, missing, verdict = found + asked = verdict == "ask" and ask # Said where it comes before a question: elsewhere the folder is made. - (print if missing > 1 and ask else detail)( + (print if asked else detail)( f" {shown_path(dest)} does not exist. The nearest folder that does: " f"{shown_path(nearest)}") - if missing > 1 and ask and not _answer_yes( + if asked and not _answer_yes( f" Create {_plural(missing, 'folder')} under {shown_path(nearest)}? [y/N] "): if again: print(" Not created.") @@ -29180,14 +29202,18 @@ def _choose(state: TuiState, plat: str, dest: Path, proposed: bool = False) -> l state.card_note = (f"{dest.name} is created in " f"{_short_path(dest.parent, state.home)}{reads}.") return _start_check(state) - if not dest.exists(): - nearest = _nearest_folder(dest) - missing = len(dest.absolute().parts) - len(nearest.parts) - state.facts = {"nearest": nearest, "risky": nearest.parent == nearest or missing > 2} - # A path whose start was mistyped would be created from the root: - # the safe answer is the one under A. - return _push(state, "create", 1 if state.facts["risky"] else 0) - return _start_check(state) + found = missing_folder(dest) + if found is None: + return _start_check(state) + if found.verdict == "create": + # One level is a BIOS folder not made yet: line mode makes it and + # says so, the plan says so here. + state.card_note = f"{dest.name} is created in {_short_path(dest.parent, state.home)}." + return _start_check(state) + # Asked with no as the answer, as line mode asks. A path whose start + # was mistyped would be created from the root: said on the screen. + state.facts = {"nearest": found.nearest, "risky": found.nearest.parent == found.nearest} + return _push(state, "create", 1) def _card_folder(state: TuiState, folder: Path) -> CardChoice | None: diff --git a/install.sh b/install.sh index 9940de216..241bf5002 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="d245aefa5fc0b6ee54bccbaf2b5788c8c032aac4f7f25e9967328ed3dc7bd8e3" +DEFAULT_INSTALL_SHA256="fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c" # 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 885b58444..8a39e4bb2 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": 1470751, - "sha256": "d245aefa5fc0b6ee54bccbaf2b5788c8c032aac4f7f25e9967328ed3dc7bd8e3", + "size": 1471771, + "sha256": "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1524376, - "sha256": "71f1b4e07193fd40e74c7910597aa9f0c86ede64472fb6935e7b7dd73cb0079d" + "size": 1525396, + "sha256": "13a0aa4bbf7c17d7dc171568b060722a095b0010cfe6ee4ddd827c838b0d7bc2" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1470751, - "sha256": "d245aefa5fc0b6ee54bccbaf2b5788c8c032aac4f7f25e9967328ed3dc7bd8e3", + "size": 1471771, + "sha256": "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1470751, - "sha256": "d245aefa5fc0b6ee54bccbaf2b5788c8c032aac4f7f25e9967328ed3dc7bd8e3", + "size": 1471771, + "sha256": "fdb3c4d389126120d7f865212b08d99864983c6ac49210d04a98c1150cc46c9c", "executable": false }, { diff --git a/installer/destination.py b/installer/destination.py index 5db092c2a..aa493fab0 100644 --- a/installer/destination.py +++ b/installer/destination.py @@ -68,6 +68,29 @@ def _refuse_file(dest: Path, named: str) -> None: f"the file {named}.", message=where) +class MissingFolder(NamedTuple): + """What a BIOS folder that does not exist yet needs before it is made.""" + nearest: Path + missing: int + # "create": one level, a BIOS folder not made yet, made without a + # question. "ask": several, a path that may be mistyped, asked first + # with no as the answer when it can be asked. + verdict: str + + +def missing_folder(dest: Path) -> MissingFolder | None: + """None when dest exists, else its nearest existing folder, how many + levels are missing below it and whether they are made or asked for: + the one rule line mode and the full screen both follow.""" + if dest.exists(): + return None + nearest = dest.absolute() + while not nearest.exists() and nearest.parent != nearest: + nearest = nearest.parent + missing = len(dest.absolute().relative_to(nearest).parts) + return MissingFolder(nearest, missing, "ask" if missing > 1 else "create") + + def _missing_dest(dest: Path, ask: bool, named: str = "named with --dest", again: bool = False) -> bool: """Say that a --dest folder does not exist before anything goes there. @@ -83,17 +106,16 @@ def _missing_dest(dest: Path, ask: bool, named: str = "named with --dest", """ _refuse_closed(dest, named) _refuse_file(dest, named) - if dest.exists(): + found = missing_folder(dest) + if found is None: return True - nearest = dest.absolute() - while not nearest.exists() and nearest.parent != nearest: - nearest = nearest.parent - missing = len(dest.absolute().relative_to(nearest).parts) + nearest, missing, verdict = found + asked = verdict == "ask" and ask # Said where it comes before a question: elsewhere the folder is made. - (print if missing > 1 and ask else detail)( + (print if asked else detail)( f" {shown_path(dest)} does not exist. The nearest folder that does: " f"{shown_path(nearest)}") - if missing > 1 and ask and not _answer_yes( + if asked and not _answer_yes( f" Create {_plural(missing, 'folder')} under {shown_path(nearest)}? [y/N] "): if again: print(" Not created.") diff --git a/installer/tui_actions.py b/installer/tui_actions.py index 6baf32ca3..d67c65ce6 100644 --- a/installer/tui_actions.py +++ b/installer/tui_actions.py @@ -28,6 +28,7 @@ from installer.destination import ( card_root_above, copy_source_refusal, dest_drive, + missing_folder, mounted_cards_of, ) from installer.detect_os import _DETECTED_OS, _FROM_CARD, detected_os, detected_shown @@ -578,14 +579,18 @@ def _choose(state: TuiState, plat: str, dest: Path, proposed: bool = False) -> l state.card_note = (f"{dest.name} is created in " f"{_short_path(dest.parent, state.home)}{reads}.") return _start_check(state) - if not dest.exists(): - nearest = _nearest_folder(dest) - missing = len(dest.absolute().parts) - len(nearest.parts) - state.facts = {"nearest": nearest, "risky": nearest.parent == nearest or missing > 2} - # A path whose start was mistyped would be created from the root: - # the safe answer is the one under A. - return _push(state, "create", 1 if state.facts["risky"] else 0) - return _start_check(state) + found = missing_folder(dest) + if found is None: + return _start_check(state) + if found.verdict == "create": + # One level is a BIOS folder not made yet: line mode makes it and + # says so, the plan says so here. + state.card_note = f"{dest.name} is created in {_short_path(dest.parent, state.home)}." + return _start_check(state) + # Asked with no as the answer, as line mode asks. A path whose start + # was mistyped would be created from the root: said on the screen. + state.facts = {"nearest": found.nearest, "risky": found.nearest.parent == found.nearest} + return _push(state, "create", 1) def _card_folder(state: TuiState, folder: Path) -> CardChoice | None: diff --git a/tests/test_install_tui.py b/tests/test_install_tui.py index 47d7392fe..391ac0fb6 100644 --- a/tests/test_install_tui.py +++ b/tests/test_install_tui.py @@ -717,12 +717,13 @@ class Flow(unittest.TestCase): state.path_text = "" press(state, "paste") install.tui_update(state, Key("paste", '"~/my bios"')) - with mock.patch.object(install.Path, "exists", return_value=False), \ - mock.patch.object(install, "_nearest_folder", return_value=HOME): + # Two missing levels are asked, Choose another folder in focus. + with mock.patch.object(install, "missing_folder", + return_value=install.MissingFolder(HOME, 2, "ask")): press(state, "enter") self.assertEqual(state.screen, "create") self.assertEqual(state.dest, Path(os.path.expanduser("~/my bios"))) - press(state, "down", "enter") + press(state, "enter") self.assertEqual(state.screen, "folder") def test_typed_paths(self): @@ -2343,8 +2344,18 @@ class TicksBarAndPaths(unittest.TestCase): " ".join(install.tui_view(state, 120, 40))) near = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, near, True) - install._choose(state, "retroarch", near / "bios") - self.assertEqual(self.focused(state).action, "create") + # Line mode's rule: two missing levels are asked with no in focus, + # one is made and said on the plan. + state.stack = ["folder"] + with contextlib.redirect_stdout(io.StringIO()): + install._choose(state, "retroarch", near / "a" / "b") + self.assertEqual((state.screen, self.focused(state).action), ("create", "other")) + self.assertFalse(state.facts["risky"]) + state.stack = ["folder"] + with contextlib.redirect_stdout(io.StringIO()): + install._choose(state, "retroarch", near / "bios") + self.assertEqual(state.screen, "checking") + self.assertIn("bios is created in", state.card_note) def test_two_setups_of_one_name(self): flatpak = HOME / ".var/app/org.libretro.RetroArch/config/retroarch/system"