60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
PROJECT_DIR = Path(__file__).resolve().parent
|
|
DIST_DIR = PROJECT_DIR / "dist"
|
|
BUILD_DIR = PROJECT_DIR / "build"
|
|
SPEC_FILE = PROJECT_DIR / "VideoConvert.spec"
|
|
ENTRY_FILE = PROJECT_DIR / "videoconvert_gui.py"
|
|
EXE_NAME = "VideoConvert"
|
|
|
|
|
|
def remove_if_exists(path: Path) -> None:
|
|
if path.is_dir():
|
|
shutil.rmtree(path, ignore_errors=True)
|
|
elif path.exists():
|
|
path.unlink()
|
|
|
|
|
|
def main() -> int:
|
|
remove_if_exists(DIST_DIR)
|
|
remove_if_exists(BUILD_DIR)
|
|
remove_if_exists(SPEC_FILE)
|
|
|
|
command = [
|
|
sys.executable,
|
|
"-m",
|
|
"PyInstaller",
|
|
"--noconfirm",
|
|
"--clean",
|
|
"--onefile",
|
|
"--windowed",
|
|
"--name",
|
|
EXE_NAME,
|
|
str(ENTRY_FILE),
|
|
]
|
|
|
|
print("开始打包 exe...")
|
|
print(" ".join(f'"{part}"' if " " in part else part for part in command))
|
|
completed = subprocess.run(command, cwd=PROJECT_DIR)
|
|
if completed.returncode != 0:
|
|
return completed.returncode
|
|
|
|
exe_path = DIST_DIR / f"{EXE_NAME}.exe"
|
|
if exe_path.exists():
|
|
print(f"\n打包完成:{exe_path}")
|
|
print("首次启动会自动在 exe 同级目录创建 input/、output/、test.txt、pass_test.txt、logs、reports 等默认结构。")
|
|
else:
|
|
print("\nPyInstaller 已执行,但没有找到生成的 exe。")
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|