From b80b6df62977749e8ec8d420e2fa85246d6b15d7 Mon Sep 17 00:00:00 2001 From: muzing Date: Sat, 9 Dec 2023 09:35:48 +0800 Subject: [PATCH] Update subprocess MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增强 `SubProcessTool` 调试能力,添加当子进程发生错误时向终端输出的警告信息; 调整启动子进程与显示对话框窗口的顺序,确保子进程输出信息能够被捕获; 增加将 `IconFileDlg` 默认打开目录自动设置为脚本所在目录的功能; --- src/py2exe_gui/Core/subprocess_tool.py | 20 +++++++++++--------- src/py2exe_gui/Widgets/center_widget.py | 4 +++- src/py2exe_gui/Widgets/subprocess_widget.py | 8 ++++---- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/py2exe_gui/Core/subprocess_tool.py b/src/py2exe_gui/Core/subprocess_tool.py index 6beb7d7..78a6210 100644 --- a/src/py2exe_gui/Core/subprocess_tool.py +++ b/src/py2exe_gui/Core/subprocess_tool.py @@ -4,6 +4,7 @@ from pathlib import Path from sys import getdefaultencoding from typing import Optional, Sequence, Union +from warnings import warn from PySide6.QtCore import QIODeviceBase, QObject, QProcess, Signal @@ -160,9 +161,9 @@ class SubProcessTool(QObject): """ states = { - QProcess.ProcessState.NotRunning: "非运行", - QProcess.ProcessState.Starting: "启动中……", - QProcess.ProcessState.Running: "正在运行中……", + QProcess.ProcessState.NotRunning: "The process is not running.", + QProcess.ProcessState.Starting: "The process is starting...", + QProcess.ProcessState.Running: "The process is running...", } state_name = states[state] self.output.emit((self.STATE, state_name)) @@ -174,16 +175,17 @@ class SubProcessTool(QObject): """ process_error = { - QProcess.ProcessError.FailedToStart: "进程启动失败", - QProcess.ProcessError.Crashed: "进程崩溃", - QProcess.ProcessError.Timedout: "超时", - QProcess.ProcessError.WriteError: "写入错误", - QProcess.ProcessError.ReadError: "读取错误", - QProcess.ProcessError.UnknownError: "未知错误", + QProcess.ProcessError.FailedToStart: "The process failed to start.", + QProcess.ProcessError.Crashed: "The process has crashed.", + QProcess.ProcessError.Timedout: "The process has timed out.", + QProcess.ProcessError.WriteError: "A write error occurred in the process.", + QProcess.ProcessError.ReadError: "A read error occurred in the process", + QProcess.ProcessError.UnknownError: "An unknown error has occurred in the process.", } error_type = process_error[error] if self._process: self.abort_process(0) self.output.emit((self.ERROR, error_type)) + warn(error_type, category=Warning, stacklevel=3) self._process = None diff --git a/src/py2exe_gui/Widgets/center_widget.py b/src/py2exe_gui/Widgets/center_widget.py index 49c40f8..2a467b3 100644 --- a/src/py2exe_gui/Widgets/center_widget.py +++ b/src/py2exe_gui/Widgets/center_widget.py @@ -197,8 +197,9 @@ class CenterWidget(QWidget): “运行打包”按钮的槽函数 \n """ - self.parent().packager.run_packaging_process() + # 先显示对话框窗口,后运行子进程,确保调试信息/错误信息能被直观显示 self.parent().subprocess_dlg.show() + self.parent().packager.run_packaging_process() # 连接信号与槽 self.script_browse_btn.clicked.connect(self.script_file_dlg.open) # type: ignore @@ -270,6 +271,7 @@ class CenterWidget(QWidget): if option_key == PyinstallerArgs.script_path: script_path = Path(option_value) + self.icon_file_dlg.setDirectory(str(script_path.parent.resolve())) self.script_path_le.setText(script_path.name) self.parent_widget.statusBar().showMessage( f"打开脚本路径:{str(script_path.resolve())}" diff --git a/src/py2exe_gui/Widgets/subprocess_widget.py b/src/py2exe_gui/Widgets/subprocess_widget.py index a539f22..571906b 100644 --- a/src/py2exe_gui/Widgets/subprocess_widget.py +++ b/src/py2exe_gui/Widgets/subprocess_widget.py @@ -65,7 +65,7 @@ class SubProcessDlg(QDialog): if output_type == SubProcessTool.STATE: self.info_label.setText(output_text) - if output_text == "正在运行中……": + if output_text == "The process is running...": self.multifunction_btn.setText("取消") elif ( output_type == SubProcessTool.STDOUT or output_type == SubProcessTool.STDERR @@ -80,7 +80,7 @@ class SubProcessDlg(QDialog): self.multifunction_btn.setText("取消") elif output_type == SubProcessTool.ERROR: self.info_label.setText("PyInstaller错误!") - self.browser.append(output_text) + self.browser.append(f"PyInstaller 子进程输出信息:{output_text}") self.browser.append("请检查是否已经安装正确版本的 PyInstaller") self.multifunction_btn.setText("关闭") @@ -97,8 +97,8 @@ class SubProcessDlg(QDialog): elif btn_text == "打开输出位置": dist_path = self.parent().packaging_task.script_path.parent / "dist" if PLATFORM.windows == RUNTIME_PLATFORM: - import os # fmt: skip - os.startfile(dist_path) # noqa + from os import startfile as os_startfile # fmt: skip + os_startfile(dist_path) # noqa elif PLATFORM.linux == RUNTIME_PLATFORM: subprocess.call(["xdg-open", dist_path]) elif PLATFORM.macos == RUNTIME_PLATFORM: