-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_exe.py
More file actions
295 lines (247 loc) · 8.32 KB
/
build_exe.py
File metadata and controls
295 lines (247 loc) · 8.32 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
IAR Firmware Publish Tool - Build Script
Build executable from Python source code
"""
import os
import sys
import subprocess
import shutil
import re
from pathlib import Path
# Constants
SPEC_NAME_BASE = "IAR_Firmware_Publish_Tool"
RELEASE_DIR = "release"
def check_pyinstaller():
"""Check if PyInstaller is installed"""
try:
import PyInstaller
print(f"[OK] PyInstaller installed, version: {PyInstaller.__version__}")
return True
except ImportError:
print("[ERROR] PyInstaller not installed")
return False
def install_pyinstaller():
"""Install PyInstaller"""
print("Installing PyInstaller...")
try:
subprocess.run([sys.executable, "-m", "pip", "install", "pyinstaller"],
check=True, capture_output=True, text=True)
print("[OK] PyInstaller installed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"[ERROR] PyInstaller installation failed: {e}")
return False
def increment_version():
"""Increment tool version number"""
print("Incrementing tool version...")
try:
from tool_version_manager import ToolVersionManager
# Create version manager instance
version_manager = ToolVersionManager()
# Increment and update version
success, new_version = version_manager.increment_and_update_advanced()
if success and new_version:
print(f"[OK] Tool version incremented to: {new_version}")
return new_version
else:
print("[ERROR] Version increment failed")
return None
except Exception as e:
print(f"[ERROR] Version increment failed: {e}")
return None
def update_version_file(version):
"""Update version in version.py"""
try:
version_file_path = "version.py"
if not os.path.exists(version_file_path):
print(f"[ERROR] File not found: {version_file_path}")
return False
with open(version_file_path, 'r', encoding='utf-8') as f:
content = f.read()
pattern = r'VERSION\s*=\s*["\']([^"\']+)["\'](?:\s*#.*)?'
replacement = f'VERSION = "{version}"'
if re.search(pattern, content):
new_content = re.sub(pattern, replacement, content)
with open(version_file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"[OK] Updated version to: {version}")
return True
else:
print("[ERROR] Version pattern not found in version.py")
return False
except Exception as e:
print(f"[ERROR] Failed to update version: {e}")
return False
def get_current_version():
"""Get current version from version.py"""
try:
with open("version.py", "r", encoding="utf-8") as f:
content = f.read()
match = re.search(r'VERSION\s*=\s*["\']([^"\']+)["\']', content)
if match:
return match.group(1)
else:
print("[ERROR] Failed to get version: Version pattern not found in version.py")
return None
except Exception as e:
print(f"[ERROR] Failed to get version: {e}")
return None
def create_spec_file(spec_name, exe_name):
"""Create PyInstaller spec file"""
spec_content = f'''# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=[
('user_config.example.json', '.'),
('docs', 'docs'),
],
hiddenimports=[],
hookspath=[],
hooksconfig={{}},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='{exe_name}',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=None,
)
'''
# Skip backup spec file creation
try:
with open(f"{spec_name}.spec", "w", encoding="utf-8") as f:
f.write(spec_content)
print(f"[OK] Spec file created: {spec_name}.spec")
return True
except Exception as e:
print(f"[ERROR] Spec file creation failed: {e}")
return False
def build_exe_from_spec(spec_name, exe_name):
"""Build exe file from spec file"""
print(f"Building exe from spec file...")
try:
# Run PyInstaller with spec file
cmd = [sys.executable, "-m", "PyInstaller", "--clean", f"{spec_name}.spec"]
result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8')
if result.returncode == 0:
print(f"[OK] Exe file built successfully: {exe_name}.exe")
return True
else:
print(f"[ERROR] Exe file build failed")
print(f"Error output: {result.stderr}")
return False
except Exception as e:
print(f"[ERROR] Exe file build failed: {e}")
return False
def build_exe():
"""Build exe file"""
print("Starting exe build...")
# Get current version
current_version = get_current_version()
if not current_version:
print("Failed to get current version")
return False
# Increment version
new_version = increment_version()
if not new_version:
print("Failed to increment version")
return False
# Update version file
update_version_file(new_version)
# Create spec file
spec_name = SPEC_NAME_BASE
exe_name = f"{SPEC_NAME_BASE}_v{new_version}"
if not create_spec_file(spec_name, exe_name):
return False
# Build exe
if not build_exe_from_spec(spec_name, exe_name):
return False
return new_version
def create_release_package(final_version):
"""Create release package"""
print("Creating release package...")
# Create release directory
release_dir = Path(RELEASE_DIR)
release_dir.mkdir(exist_ok=True)
# Copy exe file
exe_name = f"{SPEC_NAME_BASE}_v{final_version}"
exe_path = Path("dist") / f"{exe_name}.exe"
if exe_path.exists():
shutil.copy2(exe_path, release_dir / f"{exe_name}.exe")
print(f"[OK] Exe file copied to release directory: {exe_name}")
else:
print("[ERROR] Exe file not found")
return False
# 只拷贝exe文件,不拷贝其他文件
print("[INFO] Only copying exe file to release directory")
# Create usage guide
usage_guide = f"""# IAR Firmware Publish Tool v{final_version}
## Usage
1. Run {exe_name}.exe
2. Configure settings in the application
3. Start building
## Configuration
The application will create a user_config.json file automatically when you first run it.
You can modify the settings through the application's settings interface.
## Version
{final_version}
"""
with open(release_dir / "README.txt", "w", encoding="utf-8") as f:
f.write(usage_guide)
print("[OK] Created usage guide")
print(f"[OK] Release package created in: {release_dir.absolute()}")
return True
def main():
"""Main function"""
print("=" * 50)
print("IAR Firmware Publish Tool - Build Script")
print("=" * 50)
# Check PyInstaller
if not check_pyinstaller():
if not install_pyinstaller():
print("Cannot install PyInstaller, please install manually: pip install pyinstaller")
return False
# Build exe
final_version = build_exe()
if not final_version:
print("Build failed")
return False
# Create release package
if not create_release_package(final_version):
print("Release package creation failed")
return False
print("[OK] Build completed!")
print(f"Release files located in {RELEASE_DIR}/ directory")
print(f"Executable: {RELEASE_DIR}/{SPEC_NAME_BASE}_v{final_version}.exe")
print(f"Version: {final_version}")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)