Skip to content

Commit

Permalink
change installer _exe_cmd to text-mode readline
Browse files Browse the repository at this point in the history
  • Loading branch information
GB609 committed Dec 2, 2024
1 parent 310243b commit f284efa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
17 changes: 9 additions & 8 deletions minigalaxy/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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:
Expand All @@ -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


Expand Down
26 changes: 13 additions & 13 deletions tests/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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 = ""
Expand All @@ -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."
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f284efa

Please sign in to comment.