-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
377 lines (331 loc) · 14.9 KB
/
main.py
File metadata and controls
377 lines (331 loc) · 14.9 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
# -*- coding: utf-8 -*-
# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, sys, json, shutil, filecmp
if os.name == 'nt':
host_spec = ' HOST=win32'
else:
host_spec = ' HOST=linux'
argv = sys.argv
argc = len(argv)
error_occurred = False
build_log_dir = 'output/log'
build_log_file = 'build.log'
platform_json = 'system/platform/platform.json'
# error process
def error_proc(msg, prompt='CommandError', errtype='EARGV'):
global error_occurred
if not error_occurred:
print("! %s: [%s]: `%s'" % (prompt, errtype, msg))
error_occurred = True
# help cmd process
def help_proc():
print('Usage: helios <action> [<app>] [<at>] [<fw_name>]\n')
print('These are common commands used in various situations:')
print(' menuconfig <module> - Do the project configuration')
print(' make <app> [[<at>] [<fw_name>]] - Do the compilation work')
print(' private_clean - Clean the app private target')
print(' clean - Clean the output directory')
print(' help - Show this help page')
# menuconfig cmd process
def menuconfig_proc(board):
abs_path = os.getcwd()
plat = get_plat_by_board(board)
if plat == '':
print('Not supported {}'.format(board))
return
real_board = board.split('*')[0]
if host_spec == ' HOST=win32':
src_path = abs_path + '\\system\\platform\\' + plat + '\\boards\\' + real_board + '\\config'
dst_path = abs_path + '\\config'
shutil.copy(src_path + '\\config.in', dst_path)
shutil.copy(src_path + '\\config.mk', dst_path)
shutil.copy(src_path + '\\autoconf.h', dst_path)
target_dir = dst_path + '\\include'
src_path_inc = src_path + '\\include'
if os.path.exists(target_dir):
shutil.rmtree(target_dir, ignore_errors=True)
if not os.path.exists(src_path_inc):
os.makedirs(src_path_inc)
if not os.path.exists(target_dir):
shutil.copytree(src_path + '\\include', dst_path + '\\include')
else:
src_path = abs_path + '/system/platform/' + plat + '/boards/' + real_board + '/config'
dst_path = abs_path + '/config'
shutil.copy(src_path + '/config.in', dst_path)
shutil.copy(src_path + '/config.mk', dst_path)
shutil.copy(src_path + '/autoconf.h', dst_path)
target_dir = dst_path + '/include'
src_path_inc = src_path + '/include'
if os.path.exists(target_dir):
shutil.rmtree(target_dir, ignore_errors=True)
if not os.path.exists(src_path_inc):
os.makedirs(src_path_inc)
if not os.path.exists(target_dir):
shutil.copytree(src_path + '/include', dst_path + '/include')
cmd_str = 'make menuconfig PY=' + argv[1] + host_spec
os.system(cmd_str)
if host_spec == ' HOST=win32':
shutil.copy(dst_path + '\\config.in', src_path)
shutil.copy(dst_path + '\\config.mk', src_path)
shutil.copy(dst_path + '\\autoconf.h', src_path)
target_dir = src_path + '\\include'
if os.path.exists(target_dir):
shutil.rmtree(target_dir, ignore_errors=True)
if not os.path.exists(target_dir):
shutil.copytree(dst_path + '\\include', src_path + '\\include')
else:
shutil.copy(dst_path + '/config.in', src_path)
shutil.copy(dst_path + '/config.mk', src_path)
shutil.copy(dst_path + '/autoconf.h', src_path)
target_dir = src_path + '/include'
if os.path.exists(target_dir):
shutil.rmtree(target_dir, ignore_errors=True)
if not os.path.exists(target_dir):
shutil.copytree(dst_path + '/include', src_path + '/include')
# clean cmd process
def clean_proc():
if os.path.exists("output\\tmp\\app_record.tmp"):
with open("output\\tmp\\app_record.tmp", 'r', encoding='utf-8') as tmp_app_entry_file:
old_app_entry_str = (tmp_app_entry_file.read()).strip()
if old_app_entry_str == "services/microPython" or old_app_entry_str == "services/micropython":
if not os.path.exists("services\\microPython\\microPython.mk"):
print("!!!Warning!!! Micropython makefile has been deleted, try to fix it !!!")
move_micropython_dependencies(old_app_entry_str)
cmd_str = 'make clean PY=' + argv[1]
os.system(cmd_str)
# clean cmd process
def private_clean_proc():
cmd_str = 'make private_clean PY=' + argv[1]
os.system(cmd_str)
# get plat by board
def get_plat_by_board(board):
if not os.path.exists(platform_json):
error_proc(platform_json, 'NotFoundError', 'ECONF')
return ''
with open(platform_json, 'r', encoding='utf-8') as f:
platforms = json.load(f)
for k,v in platforms.items():
if board in v:
return k
return ''
def move_micropython_dependencies(app_entry):
abs_path = os.getcwd()
src_path = abs_path + '/tools/micropython/'
dst_path = abs_path + '/../../'
shutil.copy(src_path + '/gen.sh', dst_path)
shutil.copy(src_path + '/micropython.mk', dst_path)
shutil.copy(src_path + '/private.mk', dst_path)
def sdk_version_update(sdk_ver_update, abs_path, arg):
version_x = "1"
version_y = "0"
version_z_str = "1"
version_z = 0
fw_version_cache = None
fw_version = None
with open(abs_path + '\\system\\include\\quectel_version.h', 'r') as file:
for line in file:
data = line.strip()
if "VERSION_MAJOR" in data:
version_x = data.split(' ')[-1]
if "VERSION_MINOR" in data:
version_y = data.split(' ')[-1]
if "VERSION_MICRO" in data:
version_z_str = data.split(' ')[-1]
if "mob_sw_rev" in data:
fw_version_cache = data
version_z = int(version_z_str) + 1
if sdk_ver_update:
fw_version = '#define mob_sw_rev \"' + arg + '\"\n'
else:
fw_version = fw_version_cache + '\n'
version_content='#ifndef __QUECTEL_VERSION_H__\n' + \
'#define __QUECTEL_VERSION_H__\n\n' + \
fw_version + '\n' + \
'#define VERSION_MAJOR ' + version_x + '\n'\
'#define VERSION_MINOR ' + version_y + '\n'\
'#define VERSION_MICRO ' + str(version_z) + '\n'\
'\n\n#endif'
with open(abs_path + '\\system\\include\\quectel_version.h', 'w+') as f:
f.write(version_content)
# make cmd process
def make_proc():
global argv
global argc
got_plat = False
got_noos = False
got_verbose = False
got_seq_params = False
got_fw_name = False
got_qpyver = False
sdk_ver_update = False
app_entry = argv[3].replace('\\', '/')
app_entry_chr_list = list(app_entry)
for i in list(reversed(range(0, len(app_entry_chr_list)))):
if app_entry_chr_list[i-len(app_entry_chr_list)] == '/':
app_entry_chr_list[i-len(app_entry_chr_list)] = ''
else:
break
app_entry = ''.join(app_entry_chr_list)
if not len(app_entry) or not os.path.exists(os.path.abspath(app_entry)):
error_proc(argv[3], 'NotFoundError', 'EARGV')
return
cmd_str = argv[2] + ' APP_ENTRY=' + app_entry
if argc != 4:
for arg in argv[4:]:
if arg.find('@') >= 0: # sequential parameters process
if got_seq_params:
error_proc(arg)
return
else:
seq_params = arg.split('@')
for i in seq_params:
if i == '':
pass
elif get_plat_by_board(i):
if got_plat:
error_proc(i)
return
else:
abs_path = os.getcwd()
real_board = i.split('*')[0]
plat = get_plat_by_board(i)
if host_spec == ' HOST=win32':
src_path = abs_path + '\\system\\platform\\' + plat + '\\boards\\' + real_board + '\\config'
dst_path = abs_path + '\\config'
if not filecmp.cmp(src_path + '\\config.in', dst_path + '\\config.in', shallow=False):
shutil.copy(src_path + '\\config.in', dst_path)
if not filecmp.cmp(src_path + '\\config.mk', dst_path + '\\config.mk', shallow=False):
shutil.copy(src_path + '\\config.mk', dst_path)
if not filecmp.cmp(src_path + '\\autoconf.h', dst_path + '\\autoconf.h', shallow=False):
shutil.copy(src_path + '\\autoconf.h', dst_path)
target_dir = dst_path + '\\include'
src_path_inc = src_path + '\\include'
if os.path.exists(target_dir):
shutil.rmtree(target_dir, ignore_errors=True)
if not os.path.exists(src_path_inc):
os.makedirs(src_path_inc)
if not os.path.exists(target_dir):
shutil.copytree(src_path + '\\include', dst_path + '\\include')
else:
src_path = abs_path + '/system/platform/' + plat + '/boards/' + real_board + '/config'
dst_path = abs_path + '/config'
if not filecmp.cmp(src_path + '/config.in', dst_path + '/config.in', shallow=False):
shutil.copy(src_path + '/config.in', dst_path)
if not filecmp.cmp(src_path + '/config.mk', dst_path + '/config.mk', shallow=False):
shutil.copy(src_path + '/config.mk', dst_path)
if not filecmp.cmp(src_path + '/autoconf.h', dst_path + '/autoconf.h', shallow=False):
shutil.copy(src_path + '/autoconf.h', dst_path)
target_dir = dst_path + '/include'
src_path_inc = src_path + '/include'
if os.path.exists(target_dir):
shutil.rmtree(target_dir, ignore_errors=True)
if not os.path.exists(src_path_inc):
os.makedirs(src_path_inc)
if not os.path.exists(target_dir):
shutil.copytree(src_path + '/include', dst_path + '/include')
cmd_str = cmd_str + ' PLAT=' + plat + ' BOARD=' + real_board
got_plat = True
elif i == 'NOOS':
if got_noos:
error_proc(i)
return
else:
cmd_str = cmd_str + ' NOOS=y'
got_noos = True
elif i == 'verbose':
if got_verbose:
error_proc(i)
return
else:
cmd_str = cmd_str + ' V=1'
got_verbose = True
elif i.startswith('VER='):
if got_qpyver:
error_proc(i)
return
else:
cmd_str = cmd_str + ' ' + i
got_qpyver = True
elif i.startswith('UPD'):
sdk_ver_update = True
else:
error_proc(i)
return
got_seq_params = True
else:
if got_fw_name:
error_proc(arg)
return
else:
cmd_str = cmd_str + ' FW_NAME=' + arg
got_fw_name = True
# if got_fw_name:
# sdk_version_update(sdk_ver_update, abs_path, arg)
cmd_str = cmd_str + host_spec
cmd_str = cmd_str + ' PY=' + argv[1]
cmd_str = cmd_str + ' 2>&1 | tee ' + build_log_dir + '/' + build_log_file
if not os.path.exists(build_log_dir):
os.makedirs(build_log_dir)
move_micropython_dependencies(app_entry)
os.system(cmd_str)
# flash cmd process
def flash_proc():
global argv
global argc
if argc != 4:
error_proc('arguments count error')
return
else:
app_entry = argv[3].replace('\\', '/')
app_entry_chr_list = list(app_entry)
for i in list(reversed(range(0, len(app_entry_chr_list)))):
if app_entry_chr_list[i-len(app_entry_chr_list)] == '/':
app_entry_chr_list[i-len(app_entry_chr_list)] = ''
else:
break
app_entry = ''.join(app_entry_chr_list)
if not len(app_entry) or not os.path.exists(os.path.abspath(app_entry)):
error_proc(argv[3], 'NotFoundError', 'EARGV')
return
cmd_str = 'make flash APP_ENTRY=' + app_entry + ' PY=' + argv[1]
os.system(cmd_str)
# main entry:
if __name__ == '__main__':
if argc > 6:
error_proc(argv[6:], 'EARGC')
exit()
if argc == 2:
help_proc()
elif argc == 3:
if argv[2] == 'help':
help_proc()
elif argv[2] == 'clean':
clean_proc()
elif argv[2] == 'private_clean':
private_clean_proc()
else:
error_proc(argv[2])
elif argc == 4:
if argv[2] == 'menuconfig':
menuconfig_proc(argv[3])
elif argv[2] == 'flash':
flash_proc()
else:
error_proc(argv[2])
else:
if argv[2] == 'make':
make_proc()
else:
error_proc(argv[2])