-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_build.py
329 lines (256 loc) · 10.5 KB
/
make_build.py
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
# ********************************************************************************
# Copyright © 2020-2021, ETH Zurich, D-BSSE, Andreas P. Cuny & Aaron Ponti
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0
# which accompanies this distribution, and is available at
# http://www.gnu.org/licenses/gpl
#
# Contributors:
# * Andreas P. Cuny - initial API and implementation
# * Aaron Ponti - initial API and implementation
# *******************************************************************************
import os
import platform
import site
import sys
import shutil
from pathlib import Path
import json
import unittest
# Try adding the python devel libs to LD_LIBRARY_PATH
if sys.platform == "linux":
# Python library path
python_dir = site.getsitepackages()
packages_root_dir = python_dir[0]
python_library_path = str(Path(packages_root_dir, '../..').resolve())
# Keep track of possible interpreter restart
restart_required = False
# If necessary, add to the LD_LIBRARY_PATH
if 'LD_LIBRARY_PATH' not in os.environ:
# LD_LIBRARY_PATH is not set. Add python_library_path
os.environ['LD_LIBRARY_PATH'] = python_library_path
# Flag for restart
restart_required = True
else:
# Get current list
paths = os.environ['LD_LIBRARY_PATH']
path_list = paths.split(':')
# Is the python library path in the list?
if python_library_path not in path_list:
# Append python_library_path
paths = paths + ':' + python_library_path
# Update LD_LIBRARY_PATH in the environment
os.environ['LD_LIBRARY_PATH'] = paths
# Flag for restart
restart_required = True
if restart_required:
# Inform
print(f"Adding {python_library_path} to "
f"LD_LIBRARY_PATH and restarting the interpreter.")
try:
os.execv(sys.executable, ['python'] + sys.argv)
except Exception as exc:
print(f"Failed re-exec: {exc}")
sys.exit(1)
try:
from pypocquant.manual import build_manual
except:
pypocquant_path = Path(__file__).resolve().parent / 'src' / 'main' / 'python' / 'pyPOCQuantUI'
sys.path.insert(0, str(pypocquant_path))
from pypocquant.manual import build_manual, build_quickstart
from ui import versionInfo
def update_application_metadata():
"""Update application metadata before freeze."""
# Get the path to src/build/settings/base.json
current_folder = Path(__file__).parent.resolve()
base_json_path = current_folder / "src" / "build" / "settings" / "base.json"
# Make sure that the file exists
if not base_json_path.is_file():
print(f"Settings file {str(base_json_path)} not found! Cannot update metadata")
return
# Read the file
with open(base_json_path) as json_file:
metadata = json.load(json_file)
# Update the version
metadata["version"] = versionInfo.get_version_string()
# Save the file
with open(base_json_path, 'w') as json_file:
json.dump(metadata, json_file)
def copy_and_print_missing_files(source_root_dir: Path, target_root_dir: Path, list_items: list):
"""Copy missing files before freezing.
"""
# Make sure that both source_root_dir and target_root_dir are Path onjects
source_root_dir = Path(source_root_dir)
target_root_dir = Path(target_root_dir)
# Clean the target
if target_root_dir.is_dir():
shutil.rmtree(target_root_dir)
target_root_dir.mkdir(parents=True, exist_ok=True)
# Now copy the items from the list
for item in list_items:
source = Path(source_root_dir / item)
target = Path(target_root_dir / item)
if source.is_dir():
shutil.copytree(source, target)
item_type = "DIR"
elif source.is_file():
parent_folder = Path(target.parents[0].resolve())
parent_folder.mkdir(parents=True, exist_ok=True)
shutil.copyfile(source, target)
item_type = "FILE"
else:
print(f"[ERROR] '{source}' not found.")
continue
print(f"[{item_type:5s}] Copied from '{source}' to '{target}'.")
def prepare_freezing():
"""Copy files known to be missed by `fbs freeze`."""
# Target root folder
target_root_dir = Path(Path(__file__).parents[0].resolve())
# Create common folders
Path(target_root_dir / 'src/freeze').mkdir(parents=True, exist_ok=True)
Path(target_root_dir / 'src/freeze/windows').mkdir(parents=True, exist_ok=True)
Path(target_root_dir / 'src/freeze/mac').mkdir(parents=True, exist_ok=True)
Path(target_root_dir / 'src/freeze/linux').mkdir(parents=True, exist_ok=True)
# Source root folder
python_dir = site.getsitepackages()
# Build the list of files and folder to copy to the correct `freeze` subfolder
if platform.system() == 'Windows':
packages_root_dir = python_dir[1]
# Build map of files/folders to copy
item_list = [
'cairosvg',
'cssselect2/VERSION',
'tinycss2/VERSION',
'sklearn/utils/_cython_blas.cp36-win_amd64.pyd',
'skimage/feature/_orb_descriptor_positions.py',
'dask/dask.yaml',
'pywt/_extensions/_cwt.cp36-win_amd64.pyd',
'pyzbar',
'scipy/.libs',
'scipy/special/cython_special.cp36-win_amd64.pyd'
]
# Copy missing files
copy_and_print_missing_files(
packages_root_dir,
Path(target_root_dir / 'src/freeze/windows'),
item_list
)
# Copy version info
Path(target_root_dir / 'src/freeze/windows/ui').mkdir(parents=True, exist_ok=True)
shutil.copy(
Path(target_root_dir / 'src/main/python/pyPOCQuantUI/ui/VERSION'),
Path(target_root_dir / 'src/freeze/windows/ui')
)
shutil.copy(
Path(target_root_dir / 'src/main/python/pyPOCQuantUI/ui/VERSION'),
Path(target_root_dir / 'src/freeze/windows')
)
elif platform.system() == 'Darwin':
packages_root_dir = python_dir[0]
# Build map of files/folders to copy
item_list = [
'cairosvg/VERSION',
'cairocffi/VERSION',
'cv2',
'reportlab',
'skimage',
'sklearn',
'pywt',
]
# Copy missing files
copy_and_print_missing_files(
packages_root_dir,
Path(target_root_dir / 'src/freeze/mac/Contents/MacOS'),
item_list
)
# Copy version info
Path(target_root_dir / 'src/freeze/mac/Contents/MacOS/ui').mkdir(parents=True, exist_ok=True)
shutil.copy(
Path(target_root_dir / 'src/main/python/pyPOCQuantUI/ui/VERSION'),
Path(target_root_dir / 'src/freeze/mac/Contents/MacOS/ui')
)
shutil.copy(
Path(target_root_dir / 'src/main/python/pyPOCQuantUI/ui/VERSION'),
Path(target_root_dir / 'src/freeze/mac/Contents/MacOS')
)
elif platform.system() == 'Linux':
packages_root_dir = python_dir[0]
print(f"Packages root dir = {packages_root_dir}")
# Build map of files/folders to copy
item_list = [
'cairosvg/VERSION',
'cairocffi/VERSION',
'sklearn/utils/_weight_vector.cpython-36m-x86_64-linux-gnu.so',
'scipy/special/cython_special.cpython-36m-x86_64-linux-gnu.so',
'pywt/_extensions/_cwt.cpython-36m-x86_64-linux-gnu.so',
'skimage/feature/orb_cy.cpython-36m-x86_64-linux-gnu.so',
'skimage/feature/_orb_descriptor_positions.py',
'sklearn/utils/_cython_blas.cpython-36m-x86_64-linux-gnu.so'
]
# Copy missing files
copy_and_print_missing_files(
packages_root_dir,
Path(target_root_dir / 'src/freeze/linux'),
item_list
)
# Copy version info
Path(target_root_dir / 'src/freeze/linux/ui').mkdir(parents=True, exist_ok=True)
shutil.copy(
Path(target_root_dir / 'src/main/python/pyPOCQuantUI/ui/VERSION'),
Path(target_root_dir / 'src/freeze/linux/ui')
)
shutil.copy(
Path(target_root_dir / 'src/main/python/pyPOCQuantUI/ui/VERSION'),
Path(target_root_dir / 'src/freeze/linux')
)
else:
raise Exception("Platform not supported!")
print('|---------------------------------------------------------------------------------------------------|')
print('| Update application metadata')
print('|---------------------------------------------------------------------------------------------------|')
# First, update application version
update_application_metadata()
print('|---------------------------------------------------------------------------------------------------|')
print('| Run unit tests')
print('|---------------------------------------------------------------------------------------------------|')
from pypocquant import tests
# Run all unit tests
suite = unittest.TestLoader().loadTestsFromModule(tests)
result = unittest.TextTestRunner().run(suite)
# Do not continue with the build if there are errors
num_errors = len(result.errors) + len(result.failures)
if num_errors > 0:
print("*")
print("*")
print("* Unit testing failed! Aborting build!")
print("*")
print("*")
sys.exit(1)
print('|---------------------------------------------------------------------------------------------------|')
print('| Start building pyPOCQUANT')
print('|---------------------------------------------------------------------------------------------------|')
# Build manual
build_manual()
# Build quickstart
build_quickstart()
# clean the app out dir
print('Running: fbs clean')
stream = os.popen('fbs clean')
print(stream.read())
# print('Running: fbs run')
# print('Note: test if app works. Close it to continue')
# stream = os.popen('fbs run')
# print(stream.read())
print('Prepare freezing')
prepare_freezing()
# print('Running: fbs freeze')
stream = os.popen('fbs freeze')
print(stream.read())
#
# print('Running: fbs installer')
stream = os.popen('fbs installer')
print(stream.read())
print('|---------------------------------------------------------------------------------------------------|')
print('| Done.')
print('|---------------------------------------------------------------------------------------------------|')
sys.exit(0)