Skip to content

Commit

Permalink
Fix _exe_cmd in installer.py (#2)
Browse files Browse the repository at this point in the history
* Fix _exe_cmd in installer.py

* Only print if print_output is on

* Set stdout and stderr to non-blocking in exe_cmd function
  • Loading branch information
sharkwouter authored and GB609 committed Dec 10, 2024
1 parent b5a054e commit 22de635
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions minigalaxy/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import hashlib
import textwrap
import time

from minigalaxy.config import Config
from minigalaxy.game import Game
Expand Down Expand Up @@ -237,7 +238,7 @@ def extract_by_wine(game, installer, temp_dir, config=Config()):

def try_wine_command(command_arr):
print('trying to run wine command:', shlex.join(command_arr))
stdout, stderr, exitcode = _exe_cmd(command_arr, False, True)
stdout, stderr, exitcode = _exe_cmd(command_arr, True)
print(stdout)
if exitcode not in [0]:
print(stderr, file=sys.stderr)
Expand Down Expand Up @@ -374,34 +375,35 @@ def uninstall_game(game):
os.remove(path_to_shortcut)


def _exe_cmd(cmd, capture_output=True, print_output=False):
"""Wine commands are very verbose.
We should consume the output in regularly to prevent buffers (and minigalaxy memory) from filling up"""
print(f'executing wine command: {shlex.join(cmd)}')
std_out = []
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
bufsize=1, universal_newlines=True, encoding="utf-8")
rc = process.poll()
while rc is None:
out_line = process.stdout.readline()
if capture_output and out_line != '':
std_out.append(out_line)

if print_output:
print(out_line, end='')

rc = process.poll()

print('command finished, read remaining output (if any)')
for line in process.stdout.readlines():
std_out.append(line)
print('done')
def _exe_cmd(cmd, print_output=False):
std_out = ""
std_err = ""
done = False
return_code = None
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, encoding="utf-8"
)
os.set_blocking(process.stdout.fileno(), False)
os.set_blocking(process.stderr.fileno(), False)
while not done:
if (return_code := process.poll()) is not None:
done = True
if data := process.stdout.readline():
std_out += data
if print_output:
print(data, end='')
if data := process.stderr.readline():
std_err += data
if print_output:
print(data, end='')
time.sleep(0.01)

process.stdout.close()
process.stderr.close()

output = ''.join(std_out)
return output, output, rc
return std_out, std_err, return_code


def _mv(source_dir, target_dir):
Expand Down

0 comments on commit 22de635

Please sign in to comment.