Fix tool log streaming — switch from subprocess.run to Popen

subprocess.run with capture_output=True blocked until process exit,
dumping all output at once. Now uses Popen with line-by-line reading,
-u flag, and PYTHONUNBUFFERED=1 so logs stream in real time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
revernomad17
2026-04-08 16:10:36 +07:00
parent 80ecd3a919
commit b7dbf54512

View File

@@ -191,23 +191,29 @@ class ArmaModManagerApp(ctk.CTk):
def run_tool(self, script_args: list[str]) -> None:
"""Run a maintenance script via subprocess, streaming output to Logs."""
import os
script = script_args[0]
extra = script_args[1:]
def worker() -> None:
self.post_log(f"\n{''*50}\n {' '.join(script_args)}\n{''*50}\n")
try:
result = subprocess.run(
[sys.executable, str(PROJECT_ROOT / script)] + extra,
capture_output=True, text=True, cwd=str(PROJECT_ROOT),
env = os.environ.copy()
env["PYTHONUNBUFFERED"] = "1"
proc = subprocess.Popen(
[sys.executable, "-u", str(PROJECT_ROOT / script)] + extra,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
cwd=str(PROJECT_ROOT),
env=env,
)
if result.stdout:
self.post_log(result.stdout)
if result.stderr:
self.post_log(result.stderr)
ok = result.returncode == 0
for line in iter(proc.stdout.readline, ""):
self.post_log(line)
proc.wait()
ok = proc.returncode == 0
self.post_log(
f"\n{'✓ Done' if ok else f'✗ Exited with code {result.returncode}'}.\n"
f"\n{'✓ Done' if ok else f'✗ Exited with code {proc.returncode}'}.\n"
)
except Exception as e:
self.post_log(f"\n✗ Failed to start {script}: {e}\n")