Skip to content

Commit 0e3a20c

Browse files
committed
Optimize code
优化子进程相关的代码:重命名部分类与函数、重构部分__main__中的功能至具体模块中、修复部分缺失的父子控件关系;
1 parent 344e0d8 commit 0e3a20c

3 files changed

Lines changed: 42 additions & 19 deletions

File tree

src/py2exe_gui/Core/packaging.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from PySide6 import QtCore
44
from PySide6.QtCore import QObject
55

6-
from .subprocess_tool import QSubProcessTool
6+
from .subprocess_tool import SubProcessTool
77

88

99
class Packaging(QObject):
@@ -23,9 +23,9 @@ def __init__(self, parent: Optional[QObject] = None):
2323
]
2424
self.args_dict: dict = dict.fromkeys(pyinstaller_args, "")
2525
self._args: List[str] = []
26-
self.subprocess: QSubProcessTool = QSubProcessTool()
26+
self._subprocess: SubProcessTool = SubProcessTool(self)
2727

28-
def get_pyinstaller_args(self, arg: tuple[str, str]) -> None:
28+
def set_pyinstaller_args(self, arg: tuple[str, str]) -> None:
2929
"""
3030
解析传递来的PyInstaller运行参数,并添加至命令参数字典 \n
3131
:param arg: 运行参数
@@ -34,9 +34,9 @@ def get_pyinstaller_args(self, arg: tuple[str, str]) -> None:
3434
arg_key, arg_value = arg
3535
if arg_key in self.args_dict.keys():
3636
self.args_dict[arg_key] = arg_value
37-
self.set_pyinstaller_args()
37+
self._add_pyinstaller_args()
3838

39-
def set_pyinstaller_args(self) -> None:
39+
def _add_pyinstaller_args(self) -> None:
4040
"""
4141
将命令参数字典中的参数按顺序添加到命令参数列表中 \n
4242
:return: None
@@ -67,5 +67,27 @@ def run_packaging_process(self) -> None:
6767
:return: None
6868
"""
6969

70-
# self.subprocess.output.connect(lambda val: print(val)) # 测试用
71-
self.subprocess.start_process("pyinstaller", self._args)
70+
# self._subprocess.output.connect(lambda val: print(val)) # 测试用
71+
self._subprocess.start_process("pyinstaller", self._args)
72+
73+
def abort_process(self) -> int:
74+
"""
75+
紧急终止打包进程 \n
76+
:return: 子进程返回值
77+
"""
78+
79+
if self._subprocess.process:
80+
result = 0
81+
82+
def handel(output: tuple):
83+
"""处理子进程的输出,获取进程结束的返回值"""
84+
nonlocal result
85+
if output[0] == 1:
86+
result = int(output[1])
87+
88+
self._subprocess.output.connect(handel)
89+
self._subprocess.process.terminate()
90+
self._subprocess.process.waitForFinished(10000)
91+
return result
92+
else:
93+
return 0

src/py2exe_gui/Core/subprocess_tool.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from PySide6 import QtCore
55

66

7-
class QSubProcessTool(QtCore.QObject):
7+
class SubProcessTool(QtCore.QObject):
88
"""辅助使用QProcess创建并管理子进程的工具类"""
99

10-
# 自定义信号,参数为 (output_type: QSubProcessTool.output_type, output_text: str)
10+
# 自定义信号,参数为 tuple[output_type: SubProcessTool.output_type, output_text: str]
1111
output = QtCore.Signal(tuple)
1212

1313
# output_types
@@ -17,7 +17,7 @@ class QSubProcessTool(QtCore.QObject):
1717
STDERR = 3
1818

1919
def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
20-
super(QSubProcessTool, self).__init__(parent)
20+
super(SubProcessTool, self).__init__(parent)
2121
self.process: Optional[QtCore.QProcess] = None
2222

2323
def start_process(self, program: str, arguments: Sequence[str]) -> None:
@@ -46,13 +46,15 @@ def _process_started(self) -> None:
4646
"""
4747
pass
4848

49-
def _process_finished(self) -> None:
49+
def _process_finished(
50+
self, exit_code: int, exit_status: QtCore.QProcess.ExitStatus
51+
) -> None:
5052
"""
5153
处理子进程的槽 \n
5254
:return: None
5355
"""
5456

55-
self.output.emit((self.FINISHED, "Subprocess finished."))
57+
self.output.emit((self.FINISHED, str(exit_code)))
5658
self.process = None
5759

5860
def _handle_stdout(self) -> None:

src/py2exe_gui/__main__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@ class MainApp(MainWindow):
1313
def __init__(self, *args, **kwargs) -> None:
1414
super(MainApp, self).__init__(*args, **kwargs)
1515

16-
self.packager = Packaging()
16+
self.packager = Packaging(self)
1717

1818
self.packager.args_settled.connect(
1919
lambda val: self.center_widget.pyinstaller_args_browser.enrich_args_text(
2020
val
2121
)
2222
)
2323

24-
self.center_widget.option_selected.connect(self.packager.get_pyinstaller_args)
24+
self.center_widget.option_selected.connect(self.packager.set_pyinstaller_args)
2525

26-
self.subprocess_dlg = SubProcessDlg()
26+
self.subprocess_dlg = SubProcessDlg(self)
2727

2828
def run_packaging():
2929
self.packager.run_packaging_process()
3030
self.subprocess_dlg.show()
3131

3232
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
3333

34-
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
34+
# TODO 在 Packaging 类中增加处理输出的方法,将所有 output 信号连接至该方法处理
35+
self.packager._subprocess.output.connect(self.subprocess_dlg.handle_output)
3536

3637
self.status_bar.showMessage("就绪")
3738

@@ -40,9 +41,7 @@ def closeEvent(self, event):
4041
重写关闭事件,进行收尾清理 \n
4142
"""
4243

43-
if self.packager.subprocess.process:
44-
self.packager.subprocess.process.terminate() # 终止尚未结束的子进程
45-
self.packager.subprocess.process.waitForFinished()
44+
self.packager.abort_process()
4645
super(MainApp, self).closeEvent(event)
4746

4847

0 commit comments

Comments
 (0)