subprocess calls now work on linux

This commit is contained in:
Faraphel 2022-08-30 14:41:00 +02:00
parent 8e800d1cbe
commit 48f7236755

View file

@ -6,6 +6,13 @@ from typing import Callable
from source.translation import translate as _ from source.translation import translate as _
tools_dir: Path = Path("./tools/")
system = "win64" if os.name == "nt" else "lin64"
subprocess_kwargs = {"creationflags": subprocess.CREATE_NO_WINDOW} if system == "win64" else {}
# creationflags are Windows specific. Linux don't show any subprocess window per default.
class WTError(Exception): class WTError(Exception):
def __init__(self, tools_path: Path | str, return_code: int): def __init__(self, tools_path: Path | str, return_code: int):
try: try:
@ -13,7 +20,7 @@ class WTError(Exception):
[tools_path, "ERROR", str(return_code)], [tools_path, "ERROR", str(return_code)],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
check=True, check=True,
creationflags=subprocess.CREATE_NO_WINDOW, **subprocess_kwargs
).stdout.decode() ).stdout.decode()
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
error = _("- ", "CANNOT_GET_ERROR_MESSAGE", " -") error = _("- ", "CANNOT_GET_ERROR_MESSAGE", " -")
@ -26,10 +33,6 @@ class MissingWTError(Exception):
super().__init__(_("CANNOT_FIND_TOOL", ' "', tool_name, '" ', "IN_TOOLS_DIRECTORY")) super().__init__(_("CANNOT_FIND_TOOL", ' "', tool_name, '" ', "IN_TOOLS_DIRECTORY"))
tools_dir = Path("./tools/")
system = "win64" if os.name == "nt" else "lin64"
try: tools_szs_dir = next(tools_dir.glob("./szs*/")) / system try: tools_szs_dir = next(tools_dir.glob("./szs*/")) / system
except StopIteration as e: raise MissingWTError("szs") from e except StopIteration as e: raise MissingWTError("szs") from e
@ -66,7 +69,7 @@ def _run(tools_path: Path | str, *args, **kwargs) -> bytes:
[tools_path, *args], [tools_path, *args],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
check=True, check=True,
creationflags=subprocess.CREATE_NO_WINDOW **subprocess_kwargs
).stdout ).stdout
@ -124,9 +127,9 @@ def _run_popen(tools_path: Path | str, *args, universal_newlines=False) -> subpr
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
creationflags=subprocess.CREATE_NO_WINDOW,
bufsize=1 if universal_newlines else None, bufsize=1 if universal_newlines else None,
universal_newlines=universal_newlines, universal_newlines=universal_newlines,
**subprocess_kwargs
) )