1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import os
- import sys
- from winpty import PtyProcess
- class BuildAll:
- def __init__(self):
- self.current_dir = os.path.dirname(os.path.abspath(__file__))
- @staticmethod
- def __get_python_env_name_from_args():
- for arg in sys.argv[1:]:
- if arg.startswith("-python_env_name="):
- return arg.split("=")[1]
- return None
- def build_all(self):
- python_env_name = self.__get_python_env_name_from_args()
- if not python_env_name:
- python_env_name = input("请输入conda python环境的名称(例如:base): ")
- # confirm_remove = input("是否删除输出目录及其所有内容? (y/N) ").strip().lower()
- confirm_remove = "y"
- pyinstaller_confirm_flag = "-y" if confirm_remove == 'y' else ""
- commands = [
- (f"conda activate {python_env_name} && Pyinstaller {pyinstaller_confirm_flag} -D launcher.py",
- self.current_dir),
- (f"conda activate {python_env_name} && Pyinstaller {pyinstaller_confirm_flag} -D server.py",
- os.path.join(self.current_dir, "server")),
- ("npm run build:win", os.path.join(self.current_dir, "client"))
- ]
- for cmd, cwd in commands:
- if not self.__pre_execute_check(cmd, cwd):
- return
- self.__execute_cmd_with_pty(cmd, cwd)
- @staticmethod
- def __pre_execute_check(cmd, cwd):
- if "Pyinstaller" in cmd:
- script_name = cmd.split(" ")[-1]
- absolute_path = os.path.join(cwd, script_name)
- if not os.path.exists(absolute_path):
- print(f"Error: Python脚本 '{absolute_path}' 不存在!")
- return False
- elif cmd.startswith("npm "):
- pass
- return True
- @staticmethod
- def __execute_cmd_with_pty(cmd, cwd=None):
- process = PtyProcess.spawn(["cmd", "/c", cmd], cwd=cwd)
- while process.isalive():
- try:
- output = process.read()
- print(output, end='')
- except EOFError:
- break # 当伪终端关闭时跳出循环
- if __name__ == "__main__":
- builder = BuildAll()
- builder.build_all()
|