forked from masonasons/FastGH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
452 lines (356 loc) · 12.2 KB
/
build.py
File metadata and controls
452 lines (356 loc) · 12.2 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python
"""Build script for FastGH using PyInstaller - supports Windows and macOS."""
import os
import subprocess
import sys
import shutil
import tempfile
import platform as platform_mod
from pathlib import Path
from version import APP_NAME, APP_VERSION
APP_DESCRIPTION = "A GitHub desktop client"
APP_COPYRIGHT = "Copyright 2024"
APP_VENDOR = "FastGH"
def get_platform():
"""Get the current platform."""
if sys.platform == "darwin":
return "macos"
elif sys.platform == "win32":
return "windows"
else:
return "linux"
def get_git_commit_sha():
"""Get the current git commit SHA."""
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"],
capture_output=True, text=True
)
if result.returncode == 0:
return result.stdout.strip()
except Exception:
pass
return None
def create_build_info_file(script_dir: Path):
"""Create build_info.txt with commit SHA in script directory for PyInstaller to bundle."""
commit_sha = get_git_commit_sha()
if commit_sha:
build_info_path = script_dir / "build_info.txt"
with open(build_info_path, 'w') as f:
f.write(commit_sha)
print(f"Created build_info.txt: commit {commit_sha[:8]}")
return build_info_path
return None
def cleanup_build_info_file(script_dir: Path):
"""Remove build_info.txt after build to keep source directory clean."""
build_info_path = script_dir / "build_info.txt"
if build_info_path.exists():
build_info_path.unlink()
print("Cleaned up build_info.txt")
def get_hidden_imports():
"""Get list of hidden imports that PyInstaller might miss."""
return [
# wx submodules
"wx.adv",
"wx.html",
"wx.xml",
# Our packages
"models",
"models.repository",
"models.issue",
"models.commit",
"models.user",
"models.workflow",
"models.release",
"models.notification",
"models.event",
"models.discussion",
"GUI",
"GUI.main",
"GUI.view",
"GUI.options",
"GUI.accounts",
"GUI.issues",
"GUI.pullrequests",
"GUI.discussions",
"GUI.commits",
"GUI.actions",
"GUI.releases",
"GUI.search",
"GUI.theme",
# Other modules
"config",
"application",
"github_api",
"version",
# keyboard_handler
"keyboard_handler",
"keyboard_handler.wx_handler",
# requests/urllib
"requests",
"urllib3",
"certifi",
"charset_normalizer",
"idna",
# Other
"json",
"threading",
"datetime",
"webbrowser",
]
def get_data_files(script_dir: Path):
"""Get list of data files to include in the bundle."""
datas = []
# Include build_info.txt if it exists (created before build)
build_info = script_dir / "build_info.txt"
if build_info.exists():
datas.append((str(build_info), "."))
return datas
def get_binaries():
"""Get platform-specific binaries to include."""
binaries = []
if sys.platform == "win32":
# Include keyboard_handler DLLs if present
try:
import keyboard_handler
kh_path = Path(keyboard_handler.__file__).parent
for dll in kh_path.glob("*.dll"):
binaries.append((str(dll), "keyboard_handler"))
except ImportError:
pass
return binaries
def build_windows(script_dir: Path, output_dir: Path) -> tuple:
"""Build for Windows using PyInstaller.
Returns:
Tuple of (success: bool, artifact_path: Path or None)
"""
dist_dir = output_dir / "dist"
build_dir = output_dir / "build"
# Clean previous build
for d in [dist_dir, build_dir]:
if d.exists():
print(f"Cleaning {d}...")
shutil.rmtree(d)
output_dir.mkdir(parents=True, exist_ok=True)
# Create build_info.txt BEFORE building command so get_data_files can include it
create_build_info_file(script_dir)
# Build PyInstaller command
main_script = script_dir / "FastGH.pyw"
cmd = [
sys.executable, "-m", "PyInstaller",
"--name", APP_NAME,
"--windowed", # No console window
"--noconfirm", # Overwrite without asking
f"--distpath={dist_dir}",
f"--workpath={build_dir}",
f"--specpath={output_dir}",
]
# Add hidden imports
for imp in get_hidden_imports():
cmd.extend(["--hidden-import", imp])
# Add data files
for src, dst in get_data_files(script_dir):
cmd.extend(["--add-data", f"{src}{os.pathsep}{dst}"])
# Add binaries
for src, dst in get_binaries():
cmd.extend(["--add-binary", f"{src}{os.pathsep}{dst}"])
# Collect keyboard_handler
cmd.extend(["--collect-all", "keyboard_handler"])
# Add main script
cmd.append(str(main_script))
print(f"Building {APP_NAME} v{APP_VERSION} for Windows...")
print(f"Output: {output_dir}")
print()
try:
result = subprocess.run(cmd, cwd=script_dir)
finally:
# Clean up build_info.txt from source directory
cleanup_build_info_file(script_dir)
if result.returncode != 0:
return False, None
# The output will be in dist_dir / APP_NAME
app_dir = dist_dir / APP_NAME
if not app_dir.exists():
print("Error: Build output not found")
return False, None
# Create zip file for distribution
zip_path = create_windows_zip(output_dir, app_dir)
return True, zip_path
def create_windows_zip(output_dir: Path, app_dir: Path) -> Path:
"""Create a zip file of the Windows build for distribution."""
import zipfile
zip_name = f"{APP_NAME}-{APP_VERSION}-Windows.zip"
zip_path = output_dir / zip_name
if zip_path.exists():
zip_path.unlink()
print(f"Creating zip: {zip_name}...")
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file_path in app_dir.rglob('*'):
if file_path.is_file():
arc_name = Path(APP_NAME) / file_path.relative_to(app_dir)
zipf.write(file_path, arc_name)
zip_size_mb = zip_path.stat().st_size / (1024 * 1024)
print(f"Zip created: {zip_path}")
print(f"Zip size: {zip_size_mb:.1f} MB")
return zip_path
def build_macos(script_dir: Path, output_dir: Path) -> tuple:
"""Build for macOS using PyInstaller.
Returns:
Tuple of (success: bool, artifact_path: Path or None)
"""
import plistlib
dist_dir = output_dir / "dist"
build_dir = output_dir / "build"
# Clean previous build
for d in [dist_dir, build_dir]:
if d.exists():
print(f"Cleaning {d}...")
shutil.rmtree(d)
output_dir.mkdir(parents=True, exist_ok=True)
# Create build_info.txt BEFORE building command so get_data_files can include it
create_build_info_file(script_dir)
# Bundle identifier
bundle_id = f"me.masonasons.{APP_NAME.lower()}"
main_script = script_dir / "FastGH.pyw"
cmd = [
sys.executable, "-m", "PyInstaller",
"--name", APP_NAME,
"--windowed", # Create .app bundle
"--noconfirm",
f"--distpath={dist_dir}",
f"--workpath={build_dir}",
f"--specpath={output_dir}",
f"--osx-bundle-identifier={bundle_id}",
]
# Add hidden imports
for imp in get_hidden_imports():
cmd.extend(["--hidden-import", imp])
# Add data files
for src, dst in get_data_files(script_dir):
cmd.extend(["--add-data", f"{src}{os.pathsep}{dst}"])
# Collect keyboard_handler
cmd.extend(["--collect-all", "keyboard_handler"])
# Add main script
cmd.append(str(main_script))
print(f"Building {APP_NAME} v{APP_VERSION} for macOS...")
print(f"Output: {output_dir}")
print()
try:
result = subprocess.run(cmd, cwd=script_dir)
finally:
# Clean up build_info.txt from source directory
cleanup_build_info_file(script_dir)
if result.returncode != 0:
return False, None
# The app bundle will be in dist_dir
app_path = dist_dir / f"{APP_NAME}.app"
if not app_path.exists():
print("Error: App bundle not found")
return False, None
# Update Info.plist
plist_path = app_path / "Contents" / "Info.plist"
if plist_path.exists():
print("Updating Info.plist...")
with open(plist_path, 'rb') as f:
plist = plistlib.load(f)
plist.update({
'CFBundleName': APP_NAME,
'CFBundleDisplayName': APP_NAME,
'CFBundleIdentifier': bundle_id,
'CFBundleVersion': APP_VERSION,
'CFBundleShortVersionString': APP_VERSION,
'NSHumanReadableCopyright': APP_COPYRIGHT,
'LSMinimumSystemVersion': '10.13',
'NSHighResolutionCapable': True,
})
with open(plist_path, 'wb') as f:
plistlib.dump(plist, f)
# Code sign the app (ad-hoc)
sign_macos_app(app_path)
# Create DMG
dmg_path = create_macos_dmg(output_dir, app_path)
return True, dmg_path
def sign_macos_app(app_path: Path):
"""Sign the macOS app bundle with ad-hoc signature."""
print("Signing app with ad-hoc signature...")
# Clear extended attributes
subprocess.run(["xattr", "-cr", str(app_path)], capture_output=True)
# Sign app bundle
result = subprocess.run(
["codesign", "--force", "--sign", "-", str(app_path)],
capture_output=True, text=True
)
if result.returncode == 0:
print("Code signing successful!")
else:
print(f"Code signing warning: {result.stderr}")
def create_macos_dmg(output_dir: Path, app_path: Path) -> Path:
"""Create a DMG disk image for macOS distribution."""
dmg_name = f"{APP_NAME}-{APP_VERSION}.dmg"
dmg_path = output_dir / dmg_name
if dmg_path.exists():
dmg_path.unlink()
print(f"Creating DMG: {dmg_name}...")
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Copy app
temp_app = temp_path / app_path.name
shutil.copytree(app_path, temp_app, symlinks=True)
# Create Applications symlink
try:
(temp_path / "Applications").symlink_to("/Applications")
except OSError:
pass
# Create DMG
result = subprocess.run([
"hdiutil", "create",
"-volname", APP_NAME,
"-srcfolder", str(temp_path),
"-ov",
"-format", "UDZO",
"-imagekey", "zlib-level=9",
str(dmg_path)
], capture_output=True, text=True)
if result.returncode == 0:
dmg_size_mb = dmg_path.stat().st_size / (1024 * 1024)
print(f"DMG created: {dmg_path}")
print(f"DMG size: {dmg_size_mb:.1f} MB")
else:
print(f"DMG creation failed: {result.stderr}")
return None
return dmg_path
def main():
"""Build FastGH executable using PyInstaller."""
script_dir = Path(__file__).parent.resolve()
platform = get_platform()
print(f"Detected platform: {platform}")
output_dir = Path.home() / "app_dist" / APP_NAME
print(f"Building {APP_NAME} v{APP_VERSION} with PyInstaller...")
print(f"Output: {output_dir}")
print()
if platform == "windows":
success, artifact_path = build_windows(script_dir, output_dir)
elif platform == "macos":
success, artifact_path = build_macos(script_dir, output_dir)
else:
print(f"Unsupported platform: {platform}")
sys.exit(1)
if success:
print()
print("=" * 50)
print("Build completed successfully!")
print(f"Output: {output_dir}")
if artifact_path and artifact_path.exists():
dest_path = script_dir / artifact_path.name
print(f"Copying to source folder: {dest_path}")
shutil.copy2(artifact_path, dest_path)
print(f"Artifact: {dest_path}")
print("=" * 50)
else:
print()
print("=" * 50)
print("Build failed!")
print("=" * 50)
sys.exit(1)
if __name__ == "__main__":
main()