Skip to content

Commit

Permalink
tests and linting fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
GB609 committed Dec 1, 2024
1 parent 82a9580 commit a484c4b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 79 deletions.
59 changes: 34 additions & 25 deletions minigalaxy/installer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import os
import shutil
import subprocess
Expand Down Expand Up @@ -123,12 +122,12 @@ def make_tmp_dir(game):
return error_message, temp_dir


def extract_installer(game: Game, installer: str, temp_dir: str, config: Config, use_innoextract: bool):
def extract_installer(game: Game, installer: str, temp_dir: str, config: Config, use_innoextract=False):
# Extract the installer
if game.platform in ["linux"]:
err_msg = extract_linux(installer, temp_dir)
else:
err_msg = extract_windows(game, installer, temp_dir, config)
err_msg = extract_windows(game, installer, temp_dir, config, use_innoextract)
return err_msg


Expand All @@ -144,9 +143,8 @@ def extract_linux(installer, temp_dir):
return err_msg


def extract_windows(game: Game, installer: str, temp_dir: str, config: Config):
use_innoextract = False
#config.windows_installer == 'innoextract' and shutil.which("innoextract")
def extract_windows(game: Game, installer: str, temp_dir: str, config: Config, use_innoextract=False):
# config.windows_installer == 'innoextract' and shutil.which("innoextract")
err_msg = extract_by_innoextract(installer, temp_dir, config.lang, use_innoextract)
if err_msg:
err_msg = extract_by_wine(game, installer, temp_dir, config)
Expand All @@ -159,7 +157,7 @@ def extract_by_innoextract(installer: str, temp_dir: str, language: str, use_inn
if use_innoextract:
lang = lang_install(installer, language)
cmd = ["innoextract", installer, "-d", temp_dir, "--gog", lang]
stdout, stderr, exitcode = _exe_cmd(cmd)
stdout, stderr, exitcode = _exe_cmd(cmd, False, True)
if exitcode not in [0]:
err_msg = _("Innoextract extraction failed.")
else:
Expand Down Expand Up @@ -192,13 +190,10 @@ 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", "-u"]
stdout, stderr, exitcode = _exe_cmd(command)
command = ["env", *wine_env, wine_bin, "wineboot", "-i"]
stdout, stderr, exitcode = _exe_cmd(command, False, True)
if exitcode not in [0]:
print(stderr, file=sys.stderr)
return _("Wineprefix creation failed.")
else:
print(stdout, file=sys.stdout)

# calculate relative link from prefix-internal folder to game.install_dir
# keeping it relative makes sure that the game can be moved around without stuff breaking
Expand All @@ -219,8 +214,7 @@ def extract_by_wine(game: Game, installer: str, temp_dir: str, config: Config):
'/SILENT' # installers can run very long, give at least a bit of visual feedback
]
command = ["env", *wine_env, wine_bin, installer, *installer_args]
stdout, stderr, exitcode = _exe_cmd(command)

stdout, stderr, exitcode = _exe_cmd(command, False, True)
if exitcode not in [0]:
return _("Wine extraction failed.")

Expand Down Expand Up @@ -361,13 +355,30 @@ def uninstall_game(game):
os.remove(path_to_shortcut)


def _exe_cmd(cmd):
logger.error(' '.join(cmd))
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout = stdout.decode("utf-8")
stderr = stderr.decode("utf-8")
return stdout, stderr, process.returncode
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)
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:
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.decode("utf-8"))

process.stdout.close()
output = ''.join(std_out)

return output, output, rc


def _mv(source_dir, target_dir):
Expand All @@ -389,11 +400,9 @@ def _mv(source_dir, target_dir):
def lang_install(installer: str, language: str):
languages = []
arg = ""
process = subprocess.Popen(["innoextract", installer, "--list-languages"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
output = stdout.decode("utf-8")
stdout, stderr, ret_code = _exe_cmd(["innoextract", installer, "--list-languages"])

for line in output.split('\n'):
for line in stdout.split('\n'):
if not line.startswith(' -'):
continue
languages.append(line[3:])
Expand Down
15 changes: 11 additions & 4 deletions minigalaxy/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get_exe_cmd_with_var_command(game, exe_cmd, is_wine_cmd):


def get_windows_exe_cmd(game, files):
exe_cmd = [""]
exe_cmd = []
prefix = os.path.join(game.install_dir, "prefix")
os.environ["WINEPREFIX"] = prefix

Expand All @@ -122,18 +122,25 @@ def get_windows_exe_cmd(game, files):
if "category" in task and task["category"] == "game" and "path" in task:
working_dir = task["workingDir"] if "workingDir" in task else "."
path = task["path"]
exe_cmd = [get_wine_path(game), "start", "/b", "/wait", "/d", working_dir,
path]
exe_cmd = [get_wine_path(game), "start", "/b", "/wait", "/d", f'c:\\game\\{working_dir}',
f'c:\\game\\{path}']
if "arguments" in task:
exe_cmd += task["arguments"].split(" ")
break
if exe_cmd == [""]:
if len(exe_cmd) == 0:
# in case no goggame info file was found
executables = glob.glob(game.install_dir + '/*.exe')
executables.remove(os.path.join(game.install_dir, "unins000.exe"))
filename = os.path.splitext(os.path.basename(executables[0]))[0] + '.exe'
exe_cmd = [get_wine_path(game), filename]

# Backwards compatibility with windows games installed before installer fixes.
# Will not fix games requiring registry keys, since the paths will already
# e borked through the old installer.
gamelink = os.path.join(prefix, 'dosdevices', 'c:', 'game')
if not os.path.exists(gamelink):
os.symlink('../..', gamelink)

return ['env', *get_wine_env(game)] + exe_cmd


Expand Down
14 changes: 8 additions & 6 deletions minigalaxy/wine_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil

from urllib import request, parse
from urllib.error import URLError
from minigalaxy.config import Config
from minigalaxy.constants import WINE_VARIANTS
from minigalaxy.game import Game
Expand Down Expand Up @@ -38,7 +39,7 @@ def get_wine_env(game: Game, config: Config = Config(), quoted=False):
else:
envPattern = '{key}={value}'

environment = [ envPattern.format(key='WINEDLLOVERRIDES', value="winemenubuilder.exe=d") ]
environment = [envPattern.format(key='WINEDLLOVERRIDES', value="winemenubuilder.exe=d")]
environment.append(envPattern.format(key='WINEPREFIX', value=f"{game.install_dir}/prefix"))

if 'umu-run' in get_wine_path(game, config):
Expand All @@ -49,7 +50,7 @@ def get_wine_env(game: Game, config: Config = Config(), quoted=False):
variables = game.get_info('variable')
if not variables:
return environment

for var in shlex.split(variables):
kvp = var.split('=', 1)
if len(kvp) == 2:
Expand All @@ -59,9 +60,9 @@ def get_wine_env(game: Game, config: Config = Config(), quoted=False):


def get_default_wine(config: Config = Config()) -> str:
runner = shutil.which(config.default_wine_runner)
if runner:
return runner
runner = config.default_wine_runner
if isinstance(runner, str) and runner:
return shutil.which(runner)

# fallback: iterate through all known variants in declaration order
for option in WINE_VARIANTS:
Expand All @@ -72,6 +73,7 @@ def get_default_wine(config: Config = Config()) -> str:
# should never happen when get_default_wine is used after is_wine_installed returns true
return ""


def get_umu_id(game: Game) -> str:
id = game.get_info(GAMEINFO_UMUID)
if id:
Expand All @@ -90,7 +92,7 @@ def get_umu_id(game: Game) -> str:
queryUrl = f'{UMUDB_URL}/umu_api.php?{parse.quote_plus(strategy)}'
with request.urlopen(queryUrl) as request_result:
lookup_result = json.loads(request_result.read())
except:
except URLError:
api_reachable = api_reachable or False
continue
else:
Expand Down
Loading

0 comments on commit a484c4b

Please sign in to comment.