Skip to content

Commit f17c5c7

Browse files
authored
Improve printing child process output (truckersmp-cli#181)
1 parent 32efb41 commit f17c5c7

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

Diff for: .pylintrc

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
[MESSAGES CONTROL]
2+
# In steamruntime_helper.py, we can't call print_child_output()
3+
# because the helper may be running inside Steam Runtime container
4+
# and unable to import our modules.
5+
disable=
6+
duplicate-code,
7+
8+
19
[TYPECHECK]
210
# Args.* are set dynamically in main.py:
311
#

Diff for: truckersmp_cli/main.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from .truckersmp import update_mod
1717
from .utils import (
1818
activate_native_d3dcompiler_47, check_libsdl2, find_discord_ipc_sockets,
19-
get_proton_version, perform_self_update, set_wine_desktop_registry,
20-
setup_wine_discord_ipc_bridge, wait_for_steam,
19+
get_proton_version, perform_self_update, print_child_output,
20+
set_wine_desktop_registry, setup_wine_discord_ipc_bridge, wait_for_steam,
2121
)
2222
from .variables import AppId, Args, Dir, File, URL
2323

@@ -335,9 +335,8 @@ def start_with_proton():
335335
with subproc.Popen(
336336
argv_helper,
337337
env=env, stdout=subproc.PIPE, stderr=subproc.STDOUT) as proc:
338-
if Args.verbose and Args.verbose >= 1:
339-
for line in proc.stdout:
340-
print(line.decode("utf-8"), end="")
338+
if Args.verbose:
339+
print_child_output(proc)
341340
except subproc.CalledProcessError as ex:
342341
logging.error(
343342
"Steam Runtime helper exited abnormally:\n%s", ex.output.decode("utf-8"))
@@ -402,8 +401,12 @@ def start_with_wine():
402401
cmd_str += "\n ".join(argv)
403402
logging.info("Running Wine:\n %s%s", env_str, cmd_str)
404403
try:
405-
output = subproc.check_output(argv, env=env, stderr=subproc.STDOUT)
406-
logging.info("Wine output:\n%s", output.decode("utf-8"))
404+
with subproc.Popen(
405+
argv,
406+
env=env, stdout=subproc.PIPE, stderr=subproc.STDOUT) as proc:
407+
if Args.verbose:
408+
print("Wine output:")
409+
print_child_output(proc)
407410
except subproc.CalledProcessError as ex:
408411
logging.error("Wine output:\n%s", ex.output.decode("utf-8"))
409412

Diff for: truckersmp_cli/steamruntime_helper.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ def main():
4949
env=env_3rdparty, stderr=subproc.STDOUT))
5050

5151
try:
52-
output = subproc.check_output(
53-
args.game_arguments, env=env, stderr=subproc.STDOUT)
54-
if args.verbose is not None:
55-
print("Proton output:\n" + output.decode("utf-8"))
52+
with subproc.Popen(
53+
args.game_arguments,
54+
env=env, stdout=subproc.PIPE, stderr=subproc.STDOUT) as proc:
55+
if args.verbose:
56+
print("Proton output:")
57+
for line in proc.stdout:
58+
try:
59+
print(line.decode("utf-8"), end="", flush=True)
60+
except UnicodeDecodeError:
61+
print(
62+
"!! NON UNICODE OUTPUT !!", repr(line),
63+
sep=" ", end="", flush=True)
5664
except subproc.CalledProcessError as ex:
5765
print("Proton output:\n" + ex.output.decode("utf-8"), file=sys.stderr)
5866

Diff for: truckersmp_cli/utils.py

+14
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,20 @@ def perform_self_update():
433433
logging.info("Self update complete")
434434

435435

436+
def print_child_output(proc):
437+
"""
438+
Print child process output.
439+
440+
proc: A subprocess.Popen object
441+
"""
442+
for line in proc.stdout:
443+
try:
444+
print(line.decode("utf-8"), end="", flush=True)
445+
except UnicodeDecodeError:
446+
print(
447+
"!! NON UNICODE OUTPUT !!", repr(line), sep=" ", end="", flush=True)
448+
449+
436450
def setup_wine_discord_ipc_bridge():
437451
"""
438452
Check and download wine-discord-ipc-bridge.

0 commit comments

Comments
 (0)