Skip to content

Commit b80b6df

Browse files
committed
Update subprocess
增强 `SubProcessTool` 调试能力,添加当子进程发生错误时向终端输出的警告信息; 调整启动子进程与显示对话框窗口的顺序,确保子进程输出信息能够被捕获; 增加将 `IconFileDlg` 默认打开目录自动设置为脚本所在目录的功能;
1 parent b63134e commit b80b6df

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/py2exe_gui/Core/subprocess_tool.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from sys import getdefaultencoding
66
from typing import Optional, Sequence, Union
7+
from warnings import warn
78

89
from PySide6.QtCore import QIODeviceBase, QObject, QProcess, Signal
910

@@ -160,9 +161,9 @@ def _handle_state(self, state: QProcess.ProcessState) -> None:
160161
"""
161162

162163
states = {
163-
QProcess.ProcessState.NotRunning: "非运行",
164-
QProcess.ProcessState.Starting: "启动中……",
165-
QProcess.ProcessState.Running: "正在运行中……",
164+
QProcess.ProcessState.NotRunning: "The process is not running.",
165+
QProcess.ProcessState.Starting: "The process is starting...",
166+
QProcess.ProcessState.Running: "The process is running...",
166167
}
167168
state_name = states[state]
168169
self.output.emit((self.STATE, state_name))
@@ -174,16 +175,17 @@ def _handle_error(self, error: QProcess.ProcessError) -> None:
174175
"""
175176

176177
process_error = {
177-
QProcess.ProcessError.FailedToStart: "进程启动失败",
178-
QProcess.ProcessError.Crashed: "进程崩溃",
179-
QProcess.ProcessError.Timedout: "超时",
180-
QProcess.ProcessError.WriteError: "写入错误",
181-
QProcess.ProcessError.ReadError: "读取错误",
182-
QProcess.ProcessError.UnknownError: "未知错误",
178+
QProcess.ProcessError.FailedToStart: "The process failed to start.",
179+
QProcess.ProcessError.Crashed: "The process has crashed.",
180+
QProcess.ProcessError.Timedout: "The process has timed out.",
181+
QProcess.ProcessError.WriteError: "A write error occurred in the process.",
182+
QProcess.ProcessError.ReadError: "A read error occurred in the process",
183+
QProcess.ProcessError.UnknownError: "An unknown error has occurred in the process.",
183184
}
184185
error_type = process_error[error]
185186

186187
if self._process:
187188
self.abort_process(0)
188189
self.output.emit((self.ERROR, error_type))
190+
warn(error_type, category=Warning, stacklevel=3)
189191
self._process = None

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ def run_packaging() -> None:
197197
“运行打包”按钮的槽函数 \n
198198
"""
199199

200-
self.parent().packager.run_packaging_process()
200+
# 先显示对话框窗口,后运行子进程,确保调试信息/错误信息能被直观显示
201201
self.parent().subprocess_dlg.show()
202+
self.parent().packager.run_packaging_process()
202203

203204
# 连接信号与槽
204205
self.script_browse_btn.clicked.connect(self.script_file_dlg.open) # type: ignore
@@ -270,6 +271,7 @@ def handle_option_set(self, option: tuple[PyinstallerArgs, str]) -> None:
270271

271272
if option_key == PyinstallerArgs.script_path:
272273
script_path = Path(option_value)
274+
self.icon_file_dlg.setDirectory(str(script_path.parent.resolve()))
273275
self.script_path_le.setText(script_path.name)
274276
self.parent_widget.statusBar().showMessage(
275277
f"打开脚本路径:{str(script_path.resolve())}"

src/py2exe_gui/Widgets/subprocess_widget.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def handle_output(self, subprocess_output: tuple[int, str]) -> None:
6565

6666
if output_type == SubProcessTool.STATE:
6767
self.info_label.setText(output_text)
68-
if output_text == "正在运行中……":
68+
if output_text == "The process is running...":
6969
self.multifunction_btn.setText("取消")
7070
elif (
7171
output_type == SubProcessTool.STDOUT or output_type == SubProcessTool.STDERR
@@ -80,7 +80,7 @@ def handle_output(self, subprocess_output: tuple[int, str]) -> None:
8080
self.multifunction_btn.setText("取消")
8181
elif output_type == SubProcessTool.ERROR:
8282
self.info_label.setText("PyInstaller错误!")
83-
self.browser.append(output_text)
83+
self.browser.append(f"PyInstaller 子进程输出信息:{output_text}")
8484
self.browser.append("请检查是否已经安装正确版本的 PyInstaller")
8585
self.multifunction_btn.setText("关闭")
8686

@@ -97,8 +97,8 @@ def handle_multifunction(self) -> None:
9797
elif btn_text == "打开输出位置":
9898
dist_path = self.parent().packaging_task.script_path.parent / "dist"
9999
if PLATFORM.windows == RUNTIME_PLATFORM:
100-
import os # fmt: skip
101-
os.startfile(dist_path) # noqa
100+
from os import startfile as os_startfile # fmt: skip
101+
os_startfile(dist_path) # noqa
102102
elif PLATFORM.linux == RUNTIME_PLATFORM:
103103
subprocess.call(["xdg-open", dist_path])
104104
elif PLATFORM.macos == RUNTIME_PLATFORM:

0 commit comments

Comments
 (0)