SXXXXXXX_PyInstallerGUIWrapper/pyinstallerguiwrapper/builder.py
2025-05-05 12:35:58 +02:00

67 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
""" builder.py - Handles the execution of the PyInstaller build process in a separate thread. """
import subprocess
import threading
import queue
import os
import traceback # For logging exceptions
# NO CHANGES NEEDED in this file for the restructuring
def run_build_in_thread(command, working_dir, output_queue, logger_func, output_dir_name, environment=None):
"""
Executes the PyInstaller command using subprocess.Popen in the background.
... (docstring remains the same) ...
"""
# ... (Function body remains the same as the previously translated version) ...
build_process = None
try:
logger_func("Build thread starting execution...", level="INFO")
env_log_msg = f"Using {'custom' if environment else 'inherited'} environment."
if environment: pass
logger_func(f"PyInstaller process starting. {env_log_msg}", level="INFO")
build_process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=working_dir,
text=True, encoding='utf-8', errors='replace', bufsize=1, env=environment )
logger_func(f"PyInstaller process launched (PID: {build_process.pid}).", level="INFO")
while True:
try:
line = build_process.stdout.readline()
if not line:
if build_process.poll() is not None:
logger_func("End of PyInstaller output stream.", level="DEBUG")
break
else: pass
else: output_queue.put(("LOG", line))
except Exception as read_err:
logger_func(f"Error reading PyInstaller output stream: {read_err}\n{traceback.format_exc()}", level="ERROR")
break
return_code = build_process.wait()
logger_func(f"PyInstaller process finished with exit code: {return_code}", level="INFO")
if return_code == 0:
dist_path_abs = os.path.join(working_dir, output_dir_name)
if os.path.exists(dist_path_abs):
success_msg = f"Build completed successfully!\nOutput Directory: {dist_path_abs}"
output_queue.put(("BUILD_SUCCESS", success_msg))
else:
warn_msg = (f"Build finished with exit code 0, but output directory not found:\n"
f"{dist_path_abs}\nCheck the full log for potential issues.")
logger_func(warn_msg, level="WARNING")
output_queue.put(("BUILD_ERROR", warn_msg))
else:
error_msg = f"Build failed! (Exit Code: {return_code})\nCheck the log for details."
output_queue.put(("BUILD_ERROR", error_msg))
except FileNotFoundError:
error_msg = ("Error: 'pyinstaller' command not found.\n"
"Ensure PyInstaller is installed correctly and in the system PATH.")
output_queue.put(("BUILD_ERROR", error_msg))
logger_func(error_msg, level="CRITICAL")
except Exception as e:
error_msg = f"Unexpected error during build process execution: {e}"
output_queue.put(("BUILD_ERROR", error_msg))
logger_func(error_msg, level="CRITICAL")
logger_func(traceback.format_exc(), level="DEBUG")
finally:
output_queue.put(("BUILD_FINISHED", None))
logger_func("Build thread finished.", level="INFO")