-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_executable.py
More file actions
204 lines (165 loc) · 5.15 KB
/
build_executable.py
File metadata and controls
204 lines (165 loc) · 5.15 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
"""
Build script for creating cross-platform executables
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
def build_frontend():
"""Build React frontend for production"""
print("Building frontend...")
frontend_dir = Path("frontend")
if not frontend_dir.exists():
print("Error: frontend directory not found")
return False
# Install dependencies if needed
if not (frontend_dir / "node_modules").exists():
print("Installing frontend dependencies...")
subprocess.run(["npm", "install"], cwd=frontend_dir, check=True)
# Build frontend
subprocess.run(["npm", "run", "build"], cwd=frontend_dir, check=True)
return True
def copy_frontend_build():
"""Copy frontend build to backend static directory"""
print("Copying frontend build...")
frontend_dist = Path("frontend/dist")
backend_static = Path("backend/static")
# Remove old static directory
if backend_static.exists():
shutil.rmtree(backend_static)
# Copy new build
shutil.copytree(frontend_dist, backend_static)
return True
def create_spec_file():
"""Create PyInstaller spec file. In CI, UAPUFOResearch path may be missing."""
datas_entries = [
"('backend', 'backend'),",
"('config.yaml', '.'),",
]
uap_path = Path("../UAPUFOResearch/UAPUFOResearch")
if uap_path.resolve().exists():
datas_entries.append("('../UAPUFOResearch/UAPUFOResearch', 'UAPUFOResearch/UAPUFOResearch'),")
else:
print("Note: UAPUFOResearch path not found; omitting from bundle (OK for CI).")
datas_block = "[\n " + "\n ".join(datas_entries) + "\n ]"
spec_content = """
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['startup.py'],
pathex=[],
binaries=[],
datas=""" + datas_block + """,
hiddenimports=[
'uvicorn.logging',
'uvicorn.loops',
'uvicorn.loops.auto',
'uvicorn.protocols',
'uvicorn.protocols.http',
'uvicorn.protocols.http.auto',
'uvicorn.protocols.websockets',
'uvicorn.protocols.websockets.auto',
'uvicorn.lifespan',
'uvicorn.lifespan.on',
],
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,
[],
exclude_binaries=True,
name='RawHorse',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='RawHorse',
)
"""
with open("rawhorse.spec", "w") as f:
f.write(spec_content)
print("Created PyInstaller spec file")
return True
def build_executable():
"""Build executable using PyInstaller"""
print("Building executable with PyInstaller...")
# Install PyInstaller if not present
try:
import PyInstaller
except ImportError:
print("Installing PyInstaller...")
subprocess.run([sys.executable, "-m", "pip", "install", "pyinstaller"], check=True)
# Build executable
subprocess.run([
"pyinstaller",
"--clean",
"rawhorse.spec"
], check=True)
print("Executable built successfully!")
print(f"Output directory: {Path('dist/RawHorse').absolute()}")
return True
def main():
"""Main build process"""
print("=" * 60)
print("Project RawHorse - Build Script")
print("=" * 60)
# Check if we're in the right directory
if not Path("startup.py").exists():
print("Error: Must run from project root directory")
return 1
try:
# Step 1: Build frontend
if not build_frontend():
print("Failed to build frontend")
return 1
# Step 2: Copy frontend build
if not copy_frontend_build():
print("Failed to copy frontend build")
return 1
# Step 3: Create spec file
if not create_spec_file():
print("Failed to create spec file")
return 1
# Step 4: Build executable
if not build_executable():
print("Failed to build executable")
return 1
print("\n" + "=" * 60)
print("Build completed successfully!")
print("=" * 60)
print("\nExecutable location:")
print(f" dist/RawHorse/RawHorse{'.exe' if sys.platform == 'win32' else ''}")
print("\nTo create a distributable package:")
print(" 1. Copy the entire dist/RawHorse directory")
print(" 2. Distribute as a ZIP or create an installer")
return 0
except Exception as e:
print(f"\nBuild failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())