From f284efaccaa05315ee3d54217793c89916cac0d2 Mon Sep 17 00:00:00 2001 From: GB609 <39741460+GB609@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:29:36 +0000 Subject: [PATCH] change installer _exe_cmd to text-mode readline --- minigalaxy/installer.py | 17 +++++++++-------- tests/test_installer.py | 26 +++++++++++++------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/minigalaxy/installer.py b/minigalaxy/installer.py index a3e38754..ef1533cb 100644 --- a/minigalaxy/installer.py +++ b/minigalaxy/installer.py @@ -191,7 +191,7 @@ def extract_by_wine(game: Game, installer: str, temp_dir: str, config: Config): if not os.path.exists(prefix_dir): os.makedirs(prefix_dir, mode=0o755) # Creating the prefix before modifying dosdevices - command = ["env", *wine_env, wine_bin, "wineboot", "-i"] + command = ["env", *wine_env, wine_bin, "wineboot", "-u"] stdout, stderr, exitcode = _exe_cmd(command, False, True) if exitcode not in [0]: return _("Wineprefix creation failed.") @@ -351,12 +351,13 @@ def uninstall_game(game): def _exe_cmd(cmd, capture_output=True, print_output=False): print(f'executing command: {" ".join(cmd)}') std_out = [] - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + process = subprocess.Popen(cmd, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + bufsize=1, universal_newlines=True, encoding="utf-8") rc = process.poll() - out_line = '' - while rc is None or out_line != '': - out_line = process.stdout.readline().decode("utf-8") - if capture_output and out_line is not None: + while rc is None: + out_line = process.stdout.readline() + if capture_output and out_line != '': std_out.append(out_line) if print_output: @@ -366,11 +367,11 @@ def _exe_cmd(cmd, capture_output=True, print_output=False): print('command finished, read remaining output (if any)') for line in process.stdout.readlines(): - std_out.append(line.decode("utf-8")) + std_out.append(line) process.stdout.close() - output = ''.join(std_out) + output = ''.join(std_out) return output, output, rc diff --git a/tests/test_installer.py b/tests/test_installer.py index 192aaa47..712d508b 100644 --- a/tests/test_installer.py +++ b/tests/test_installer.py @@ -80,7 +80,7 @@ def test1_extract_installer(self, mock_subprocess, mock_listdir, mock_is_file): """[scenario: linux installer, unpack success]""" mock_is_file.return_value = True mock_subprocess().poll.return_value = 0 - mock_subprocess().stdout.readlines.return_value = [b"\n"] + mock_subprocess().stdout.readlines.return_value = ["\n"] mock_listdir.return_value = ["object1", "object2"] game = Game("Beneath A Steel Sky", install_dir="/home/makson/GOG Games/Beneath a Steel Sky") installer_path = "/home/makson/.cache/minigalaxy/download/Beneath a Steel Sky/beneath_a_steel_sky_en_gog_2_20150.sh" @@ -96,7 +96,7 @@ def test2_extract_installer(self, mock_subprocess, mock_listdir, mock_is_file): """[scenario: linux installer, unpack failed]""" mock_is_file.return_value = True mock_subprocess().poll.return_value = 2 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] mock_listdir.return_value = ["object1", "object2"] game = Game("Beneath A Steel Sky", install_dir="/home/makson/GOG Games/Beneath a Steel Sky") installer_path = "/home/makson/.cache/minigalaxy/download/Beneath a Steel Sky/beneath_a_steel_sky_en_gog_2_20150.sh" @@ -113,7 +113,7 @@ def test3_extract_installer(self, mock_which, mock_subprocess, mock_config): """[scenario: innoextract, unpack success]""" mock_which.return_value = "path" mock_subprocess().poll.return_value = 0 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] mock_config.lang = 'en' game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift", platform="windows") installer_path = "/home/makson/.cache/minigalaxy/download/Absolute Drift/setup_absolute_drift_1.0f_(64bit)_(47863).exe" @@ -128,7 +128,7 @@ def test3_extract_installer(self, mock_which, mock_subprocess, mock_config): def test_extract_linux(self, mock_subprocess, mock_listdir, mock_is_file): mock_is_file.return_value = True mock_subprocess().poll.return_value = 1 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"(attempting to process anyway)"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "(attempting to process anyway)"] mock_listdir.return_value = ["object1", "object2"] installer_path = "/home/makson/.cache/minigalaxy/download/Beneath a Steel Sky/beneath_a_steel_sky_en_gog_2_20150.sh" temp_dir = "/home/makson/.cache/minigalaxy/extract/1207658695" @@ -141,7 +141,7 @@ def test_extract_linux(self, mock_subprocess, mock_listdir, mock_is_file): def test_extract_windows(self, mock_subprocess, mock_config): """[scenario: innoextract, unpack success]""" mock_subprocess().poll.return_value = 0 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] mock_config.lang = 'en' game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift", platform="windows") installer_path = "/home/makson/.cache/minigalaxy/download/Absolute Drift/setup_absolute_drift_1.0f_(64bit)_(47863).exe" @@ -154,7 +154,7 @@ def test_extract_windows(self, mock_subprocess, mock_config): def test1_extract_by_innoextract(self, mock_subprocess): """[scenario: success]""" mock_subprocess().poll.return_value = 0 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] installer_path = "/home/makson/.cache/minigalaxy/download/Absolute Drift/setup_absolute_drift_1.0f_(64bit)_(47863).exe" temp_dir = "/home/makson/.cache/minigalaxy/extract/1136126792" exp = "" @@ -173,7 +173,7 @@ def test2_extract_by_innoextract(self): def test3_extract_by_innoextract(self, mock_subprocess): """[scenario: unpack failed]""" mock_subprocess().poll.return_value = 1 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] installer_path = "/home/makson/.cache/minigalaxy/download/Absolute Drift/setup_absolute_drift_1.0f_(64bit)_(47863).exe" temp_dir = "/home/makson/.cache/minigalaxy/extract/1136126792" exp = "Innoextract extraction failed." @@ -190,7 +190,7 @@ def test1_extract_by_wine(self, mock_symlink, wine_path, mock_path_exists, mock_ wine_path.get_wine_path().return_value = '/bin/wine' mock_path_exists.return_value = True mock_subprocess().poll.return_value = 0 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift", platform="windows") installer_path = "/home/makson/.cache/minigalaxy/download/Absolute Drift/setup_absolute_drift_1.0f_(64bit)_(47863).exe" temp_dir = "/home/makson/.cache/minigalaxy/extract/1136126792" @@ -207,7 +207,7 @@ def test2_extract_by_wine(self, mock_symlink, mock_unlink, mock_path_exists, moc """[scenario: install failed]""" mock_path_exists.return_value = True mock_subprocess().poll.return_value = 1 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift", platform="windows") installer_path = "/home/makson/.cache/minigalaxy/download/Absolute Drift/setup_absolute_drift_1.0f_(64bit)_(47863).exe" temp_dir = "/home/makson/.cache/minigalaxy/extract/1136126792" @@ -220,7 +220,7 @@ def test2_extract_by_wine(self, mock_symlink, mock_unlink, mock_path_exists, moc def test1_postinstaller(self, mock_path_isfile, mock_subprocess): mock_path_isfile.return_value = False mock_subprocess().poll.return_value = 1 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift") exp = "" obs = installer.postinstaller(game) @@ -232,7 +232,7 @@ def test1_postinstaller(self, mock_path_isfile, mock_subprocess): def test2_postinstaller(self, mock_chmod, mock_path_isfile, mock_subprocess): mock_path_isfile.return_value = True mock_subprocess().poll.return_value = 0 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift") exp = "" obs = installer.postinstaller(game) @@ -244,7 +244,7 @@ def test2_postinstaller(self, mock_chmod, mock_path_isfile, mock_subprocess): def test3_postinstaller(self, mock_chmod, mock_path_isfile, mock_subprocess): mock_path_isfile.return_value = True mock_subprocess().poll.return_value = 1 - mock_subprocess().stdout.readlines.return_value = [b"stdout", b"stderr"] + mock_subprocess().stdout.readlines.return_value = ["stdout", "stderr"] game = Game("Absolute Drift", install_dir="/home/makson/GOG Games/Absolute Drift") exp = "Postinstallation script failed: /home/makson/GOG Games/Absolute Drift/support/postinst.sh" obs = installer.postinstaller(game) @@ -308,7 +308,7 @@ def test_get_game_size_from_unzip(self, mock_subprocess): -------- ------- --- ------- 159236636 104883200 34% 189 files """ - mock_subprocess().communicate.return_value = [stdout, b"stderr"] + mock_subprocess().communicate.return_value = [stdout, "stderr"] installer_path = "/home/i/.cache/minigalaxy/download/Beneath a Steel Sky/beneath_a_steel_sky_en_gog_2_20150.sh" exp = 159236636 obs = installer.get_game_size_from_unzip(installer_path)