Changing how PyInstaller is called

改变调用 PyInstaller 的方式,从直接调用 PyInstaller 变为通过对应环境的 Python 解释器运行 Python 代码方式。为添加 Python 环境选择功能做准备。
参见<https://pyinstaller.org/en/stable/usage.html#running-pyinstaller-from-python-code>。
This commit is contained in:
muzing 2023-12-16 10:44:39 +08:00
parent f3ab71f32c
commit 7a0609707b
2 changed files with 27 additions and 7 deletions

View File

@ -12,8 +12,8 @@ from .subprocess_tool import SubProcessTool
class Packaging(QtCore.QObject): class Packaging(QtCore.QObject):
""" """
执行打包子进程的类 执行打包子进程的类负责拼接命令选项设置子进程工作目录启动子进程等
不负责输入参数的检查 \n 不负责输入参数的检查输入参数检查由 PackagingTask 对象进行 \n
""" """
# 自定义信号 # 自定义信号
@ -27,9 +27,9 @@ class Packaging(QtCore.QObject):
super().__init__(parent) super().__init__(parent)
self.args_dict: dict = dict.fromkeys(PyinstallerArgs, "") self.args_dict: dict = dict.fromkeys(PyinstallerArgs, "")
self._args: List[str] = [] self._args: List[str] = [] # PyInstaller 命令
self._subprocess_working_dir: str = "" self._subprocess_working_dir: str = ""
self.subprocess: SubProcessTool = SubProcessTool("pyinstaller", parent=self) self.subprocess: SubProcessTool = SubProcessTool("", parent=self)
@QtCore.Slot(tuple) @QtCore.Slot(tuple)
def set_pyinstaller_args(self, arg: tuple[PyinstallerArgs, str]) -> None: def set_pyinstaller_args(self, arg: tuple[PyinstallerArgs, str]) -> None:
@ -44,9 +44,12 @@ class Packaging(QtCore.QObject):
self._add_pyinstaller_args() self._add_pyinstaller_args()
self._set_subprocess_working_dir() self._set_subprocess_working_dir()
def set_pyinstaller(self, python_path):
self.subprocess.set_program(python_path)
def _add_pyinstaller_args(self) -> None: def _add_pyinstaller_args(self) -> None:
""" """
将命令参数字典中的参数按顺序添加到命令参数列表中 \n 将命令参数字典中的参数按顺序添加到PyInstaller命令参数列表中 \n
""" """
self._args = [] # 避免重复添加 self._args = [] # 避免重复添加
@ -78,6 +81,15 @@ class Packaging(QtCore.QObject):
使用给定的参数启动打包子进程 \n 使用给定的参数启动打包子进程 \n
""" """
# 从 Python 内启动 Pyinstaller
# 参见 https://pyinstaller.org/en/stable/usage.html#running-pyinstaller-from-python-code
cmd = [
"-c",
f"import PyInstaller.__main__;PyInstaller.__main__.run({self._args})",
]
self.subprocess.set_working_dir(self._subprocess_working_dir) self.subprocess.set_working_dir(self._subprocess_working_dir)
self.subprocess.set_arguments(self._args) self.subprocess.set_arguments(cmd)
self.subprocess.start_process(time_out=500) self.subprocess.start_process(
time_out=500, mode=QtCore.QIODeviceBase.OpenModeFlag.ReadOnly
)

View File

@ -93,6 +93,14 @@ class SubProcessTool(QObject):
else: else:
return True return True
def set_program(self, program: str) -> None:
"""
设置子进程程序 \n
:param program: 程序名称
"""
self.program = program
def set_arguments(self, arguments: Sequence[str]) -> None: def set_arguments(self, arguments: Sequence[str]) -> None:
""" """
设置子进程参数 \n 设置子进程参数 \n