-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_pyinstaller.py
More file actions
127 lines (108 loc) · 3.34 KB
/
Copy pathbuild_pyinstaller.py
File metadata and controls
127 lines (108 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
"""
PyBuilder - PyInstaller 构建脚本
版本: 1.0.0
"""
import sys
import os
import subprocess
import shutil
import time
import platform
# ANSI 颜色代码
class Color:
RESET = "\033[0m"
BOLD = "\033[1m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
CYAN = "\033[96m"
GRAY = "\033[90m"
# 构建配置
PROJECT_NAME = "PyBuilder"
VERSION = "1.0.0"
COMPANY_NAME = "ASLant"
ENTRY_FILE = "main.py"
ICON_FILE = "assets/app.ico"
OUTPUT_DIR = "build"
def build():
"""执行 PyInstaller 构建"""
# 获取平台信息
os_type = platform.system()
is_windows = os_type == "Windows"
is_macos = os_type == "Darwin"
# 获取终端宽度
width = shutil.get_terminal_size().columns
separator = "-" * width
start_time = time.time()
print(
f"{Color.CYAN}{Color.BOLD}Building {PROJECT_NAME} v{VERSION} on {os_type}{Color.RESET}"
)
print(separator)
# 添加数据文件(根据操作系统使用不同分隔符)
data_separator = ";" if is_windows else ":"
# 构建 PyInstaller 命令
cmd = [
sys.executable,
"-m",
"PyInstaller",
f"--distpath={OUTPUT_DIR}",
"--workpath=build/temp",
f"--name={PROJECT_NAME}",
"--contents-directory=lib",
"--clean",
"--noconfirm",
"--log-level=WARN",
]
# 图标文件(仅Windows和macOS平台)
if is_windows or is_macos:
cmd.append(f"--icon={ICON_FILE}")
# 收集子模块
cmd.append("--collect-submodules=textual")
# 添加数据文件
cmd.append(
f"--add-data={os.path.join('src', 'style')}{data_separator}{os.path.join('src', 'style')}"
)
cmd.append(
f"--add-data={os.path.join('assets', 'pyfiglet')}{data_separator}{'pyfiglet'}"
)
cmd.append(f"--add-data={'docs'}{data_separator}{'docs'}")
# 添加入口文件
cmd.append(ENTRY_FILE)
# 执行构建
print(f"{Color.GRAY}Command:{Color.RESET}")
print(f"{Color.GRAY}" + " ".join(cmd) + f"{Color.RESET}")
print(separator)
print(f"{Color.YELLOW}Building, please wait...{Color.RESET}")
print()
try:
subprocess.run(cmd, check=True)
print(separator)
elapsed_time = time.time() - start_time
minutes = int(elapsed_time // 60)
seconds = int(elapsed_time % 60)
print(f"{Color.GREEN}{Color.BOLD}Build successful!{Color.RESET}")
# 清理 .spec 文件
spec_file = f"{PROJECT_NAME}.spec"
if os.path.exists(spec_file):
os.remove(spec_file)
print(f"{Color.GRAY}Cleaned: {spec_file}{Color.RESET}")
abs_output = os.path.abspath(OUTPUT_DIR)
print(f"{Color.GREEN}Output: {abs_output}{Color.RESET}")
if minutes > 0:
print(f"{Color.CYAN}Build time: {minutes}m {seconds}s{Color.RESET}")
else:
print(f"{Color.CYAN}Build time: {seconds}s{Color.RESET}")
return 0
except subprocess.CalledProcessError as e:
print(separator)
print(
f"{Color.RED}{Color.BOLD}Build failed: {Color.RESET}{Color.RED}{e}{Color.RESET}"
)
return 1
except Exception as e:
print(separator)
print(f"{Color.RED}{Color.BOLD}Error: {Color.RESET}{Color.RED}{e}{Color.RESET}")
return 1
if __name__ == "__main__":
sys.exit(build())