build_all.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import sys
  3. from winpty import PtyProcess
  4. class BuildAll:
  5. def __init__(self):
  6. self.current_dir = os.path.dirname(os.path.abspath(__file__))
  7. @staticmethod
  8. def __get_python_env_name_from_args():
  9. for arg in sys.argv[1:]:
  10. if arg.startswith("-python_env_name="):
  11. return arg.split("=")[1]
  12. return None
  13. def build_all(self):
  14. python_env_name = self.__get_python_env_name_from_args()
  15. if not python_env_name:
  16. python_env_name = input("请输入conda python环境的名称(例如:base): ")
  17. # confirm_remove = input("是否删除输出目录及其所有内容? (y/N) ").strip().lower()
  18. confirm_remove = "y"
  19. pyinstaller_confirm_flag = "-y" if confirm_remove == 'y' else ""
  20. commands = [
  21. (f"conda activate {python_env_name} && Pyinstaller {pyinstaller_confirm_flag} -D launcher.py",
  22. self.current_dir),
  23. (f"conda activate {python_env_name} && Pyinstaller {pyinstaller_confirm_flag} -D server.py",
  24. os.path.join(self.current_dir, "server")),
  25. ("npm run build:win", os.path.join(self.current_dir, "client"))
  26. ]
  27. for cmd, cwd in commands:
  28. if not self.__pre_execute_check(cmd, cwd):
  29. return
  30. self.__execute_cmd_with_pty(cmd, cwd)
  31. @staticmethod
  32. def __pre_execute_check(cmd, cwd):
  33. if "Pyinstaller" in cmd:
  34. script_name = cmd.split(" ")[-1]
  35. absolute_path = os.path.join(cwd, script_name)
  36. if not os.path.exists(absolute_path):
  37. print(f"Error: Python脚本 '{absolute_path}' 不存在!")
  38. return False
  39. elif cmd.startswith("npm "):
  40. pass
  41. return True
  42. @staticmethod
  43. def __execute_cmd_with_pty(cmd, cwd=None):
  44. process = PtyProcess.spawn(["cmd", "/c", cmd], cwd=cwd)
  45. while process.isalive():
  46. try:
  47. output = process.read()
  48. print(output, end='')
  49. except EOFError:
  50. break # 当伪终端关闭时跳出循环
  51. if __name__ == "__main__":
  52. builder = BuildAll()
  53. builder.build_all()