1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#本文件位于\AliOS-Things\platform\mcu\haas1000\release\auto_build_tool文件夹下
4#本文件用于适配python3.x版本,在3.7.3版本下测试通过。其他版本未测试。
5#修改本文件的目的是解决3.x版本下,使用aos make命令编译固件时发生的两个错误
6#第一个错误是“missing parentheses in call to 'print'”
7#第二个错误是“module ‘string’ has no attribute 'find'"
8
9#与阿里云提供的haashaas1000_genbin.py原版相比,本文件一共修改了4处,详见注释。
10#花生,2020年10月6日
11
12import os
13import sys
14#import struct
15import time
16#import yaml
17import string
18import subprocess
19#from time import strftime,gmtime
20import os,os.path
21import zipfile
22import platform
23import shutil
24import glob
25
26osstr = platform.system()
27
28def pack_dir(zip_dir,zip_file):
29    filelist = []
30    if os.path.isfile(zip_dir):
31        filelist.append(zip_dir)
32    else :
33        filelist = os.listdir(zip_dir)
34    zf = zipfile.ZipFile(zip_file, "w", zipfile.zlib.DEFLATED)
35    print('zip file')
36    for file in filelist:
37        #print arcname
38        print(zip_dir + '/' + file)
39        zf.write(file)
40    zf.close()
41
42flag = sys.argv[1]
43f_cfg = open('haas_genbin_config.yaml')
44
45output_dir = sys.argv[2]
46cfg_dict = {}
47print("%s" % output_dir)
48dict1_name_list = []
49dict2_name_list = []
50
51num_of_first_items = -1
52num_of_second_items = -1
53is_list = False
54file_num = -1
55sign_file_tmp = []
56
57while True:
58    line = f_cfg.readline()
59    if not line:
60        break
61
62    print("%s" %(line))
63
64    arry_parm = line.split('-', 1 )
65
66    if len(arry_parm) == 2:
67        is_list = True
68    else :
69        arry_parm = line.split(':', 1 )
70        is_list = False
71
72    para_key = ''
73    para_val = ''
74    if len(arry_parm) == 2:
75        para_key = arry_parm[0]
76        para_val = arry_parm[1]
77
78    print("para_key %s"  % para_key)
79    print("para_val %s"  % para_val)
80
81    if para_key[0:1].isspace() == False:
82        num_of_first_items = num_of_first_items + 1
83        if para_val != '\r\n' and para_val != '\n':
84            #花生,2020年10月6日17:32:39,下面的语句,原版本是print "format wrong",要想在3.x版本支持,需要加括号。
85            #否则会提示"missing parentheses in call to print"错误。下面若干个print也是相同处理方式
86            print ("format wrong")
87            break
88
89        dict1_name_list.append(para_key);
90
91        cfg_dict[dict1_name_list[num_of_first_items]] = {}
92
93        if dict1_name_list[num_of_first_items] == 'SIGN_FILE_LIST':
94            cfg_dict[dict1_name_list[num_of_first_items]] = []
95
96    else:
97
98        cfg_tmp = cfg_dict[dict1_name_list[num_of_first_items]]
99
100        if dict1_name_list[num_of_first_items] == 'SIGN_FILE_LIST':
101            if is_list == True and para_val.strip()[-1:] == ':':
102                file_num = file_num + 1
103                cfg_tmp.append({})
104                continue
105
106            if para_key.strip() == 'SWITCH':
107                cfg_tmp[file_num][para_key.strip()] = para_val.strip()
108                continue
109
110            # print para_key.strip()
111            # print para_val.strip()[1:-1]
112            cfg_tmp[file_num][para_key.strip()] = para_val.strip()[1:-1]
113            continue
114
115        if para_val == '\r\n' or para_val == '\n':
116            is_list = True
117            num_of_second_items = num_of_second_items + 1
118            dict2_name_list.append(para_key[2:])
119            cfg_tmp[dict2_name_list[num_of_second_items]] = []
120            #print cfg_dict
121            continue
122
123        if is_list == True & para_key[:3].isspace() == True :
124            if para_val[0].isspace() == True:
125
126                cfg_tmp[dict2_name_list[num_of_second_items]].append(para_val.strip()[1:-1])
127                #print cfg_dict
128                continue
129           #花生,2020年10月6日17:30:53,str在原版本是string,在2.x版本中是可以的,但是3.x版本中,string没有find属性
130        if str.find(para_key,'SWITCH') != -1:
131            cfg_tmp[para_key[2:]] = para_val.strip()
132            continue
133
134        cfg_tmp[para_key[2:]] = para_val.strip()[1:-1]
135        #print cfg_tmp
136        #print cfg_dict
137
138f_cfg.close()
139
140
141# BES_KEY
142dict_bes_key = cfg_dict['BES_KEY']
143in_key_dir = dict_bes_key['IN_KEY_DIR']
144out_key_dir = dict_bes_key['OUT_KEY_DIR']
145pub_key_name = dict_bes_key['PUB_KEY_NAME']
146pri_key_name = dict_bes_key['PRI_KEY_NAME']
147out_key_name = dict_bes_key['OUT_KEY_NAME']
148extract_dir = dict_bes_key['EXTRACT_DIR']
149extract_cmd = dict_bes_key['EXTRACT_CMD']
150key_dir = dict_bes_key['KEY_DIR']
151sign_cmd = dict_bes_key['SIGN_CMD']
152sign_dir = dict_bes_key['SIGN_DIR']
153sign_file_dir = dict_bes_key['SIGN_FILE_DIR']
154binary_dir = dict_bes_key['BINARY_DIR']
155rtos_res_file_name = dict_bes_key['RTOS_RES_FILE_NAME']
156rtos_file_name = dict_bes_key['RTOS_FILE_NAME']
157release_file_dir = dict_bes_key['RELEASE_BIN_DIR']
158pack_file_name = dict_bes_key['PACK_FILE_NAME']
159ota_bin_dir = dict_bes_key['OTA_BIN_DIR']
160gui_bin_dir = dict_bes_key['GUI_BIN_DIR']
161download_tools_dir = dict_bes_key['DLOWNLOAD_TOOLS_DIR']
162gui_tools_dir = dict_bes_key['GUI_TOOLS_DIR']
163log_swich = dict_bes_key['LOG_SWITCH']
164chmod_swich = dict_bes_key['CHMOD_SWITCH']
165extract_switch = dict_bes_key['EXTRACT_SWITCH']
166pub_otp_switch = dict_bes_key['PUB_OTP_SWITCH']
167sign_file_switch = dict_bes_key['SIGN_FILE_SWITCH']
168pack_switch = dict_bes_key['PACK_SWITCH']
169dld_cfg_dir = dict_bes_key['GUI_CFG_YAML_DIR']
170dld_cfg_file = dict_bes_key['GUI_CFG_YAML_FILE']
171pro_dld_cfg_file = dict_bes_key['PRO_CFG_YAML_FILE']
172
173# PUB_OTP
174dict_pub_otp = cfg_dict['PUB_OTP']
175outkey_dir = dict_pub_otp['OUTKEY_DIR']
176outkey_file_name = dict_pub_otp['OUTKEY_FILE_NAME']
177pub_otp_name = dict_pub_otp['PUB_OTP_FILE_NAME']
178
179
180#print cfg_dict
181
182# chmod sign_dir
183if chmod_swich == 'ON' and (osstr =="Linux"):
184    cmd_list = []
185    cmd_list.append('chmod')
186    cmd_list.append('777')
187    cur_dir = os.getcwd()
188    #花生,2020年10月6日17:32:39,下面的语句,原版本是print cur_dir,要想在3.x版本支持,需要加括号。
189    #否则会提示"missing parentheses in call to print"错误。下面若干个print也是相同处理方式
190    print (cur_dir)
191    cmd_list.append(cur_dir + '/' + sign_dir)
192    if log_swich == "ON":
193        print(cmd_list)
194    child = subprocess.Popen(cmd_list)
195    is_fst = True
196    while True:
197        status = child.poll()
198        if status == 0:
199            break
200        if status == 1:
201            break
202        if str(status) == 'None' and is_fst:
203            print('Start chmod sign dir...')
204            is_fst = False
205    print("chmod sign dir done.\n")
206#make littlefs
207if False:
208    os.system("echo \"Start make littlefs\"")
209    #print val
210    flag = sys.argv[1]
211    if flag == '1':
212        cmd_list = ['./s700_genfs.sh']
213        if osstr == "Windows":
214            cmd_list = ['.\\s700_genfs.bat']
215    else:
216        cmd_list = ['./haas1000_genfs.sh']
217        if osstr == "Windows":
218            cmd_list = ['.\\haas1000_genfs.bat']
219    child = subprocess.Popen(cmd_list)
220    while True:
221        status = child.poll()
222        if status != None:
223            break;
224    print("genfs done. ")
225    size = os.path.getsize("../../prebuild/littlefs.bin")
226    print("Littlefs code size:%d"%size)
227    if size<4882432:
228        time.sleep(5)
229    os.system("echo \"Make littlefs done\"")
230# extract pub_key
231if extract_switch == 'ON':
232    cmd_list = []
233    cmd_list = extract_cmd
234    cur_dir = os.getcwd()
235    #花生,2020年10月6日17:32:39,下面的语句,原版本是print cur_dir,要想在3.x版本支持,需要加括号。
236    #否则会提示"missing parentheses in call to print"错误。下面若干个print也是相同处理方式
237    print (cur_dir)
238    if osstr == "Windows":
239        cmd_list = []
240        cmd_list.append(cur_dir + '/' + sign_dir + '/' + 'bes_sign.bat')
241    cmd_list.append(cur_dir + '/' + sign_dir + '/' + in_key_dir + '/' + pub_key_name)
242    cmd_list.append(cur_dir + '/' + sign_dir + '/' + out_key_dir + '/' + out_key_name)
243    print(os.getcwd())
244    if log_swich == 'ON':
245        print(cmd_list)
246    os.chdir(cur_dir + '/' + sign_dir + '/' + extract_dir)
247    print(os.getcwd())
248    child = subprocess.Popen(cmd_list)
249    is_fst = True
250    while True:
251        status = child.poll()
252        if status == 0:
253            break
254        if status == 1:
255            break
256        if str(status) == 'None' and is_fst:
257            print('Start extract the public key in C code format...')
258            is_fst = False
259    os.chdir(cur_dir)
260    print("Extract the public key done.\n")
261
262# makedir release_bin ota_bin download_tools
263if True:
264    isExists=os.path.exists(release_file_dir)
265    if isExists == False:
266        os.makedirs(release_file_dir)
267    isExists = os.path.exists(ota_bin_dir)
268    if isExists == False:
269        os.makedirs(ota_bin_dir)
270    isExists = os.path.exists(gui_bin_dir)
271    if isExists == False:
272        os.makedirs(gui_bin_dir)
273    isExists = os.path.exists(download_tools_dir)
274    if isExists == False:
275        os.makedirs(download_tools_dir)
276    isExists = os.path.exists(gui_tools_dir)
277    if isExists == False:
278        os.makedirs(gui_tools_dir)
279# Make pub_otp
280if pub_otp_switch == "ON":
281    cmd_list = []
282    #./best_sign -k key/OutKey.bin key/pub_flash2.bin
283    cur_dir = os.getcwd()
284    if osstr == "Windows":
285        sign_cmd = sign_cmd + '.exe'
286    cmd_list.append(sign_dir + '/' + sign_cmd)
287    cmd_list.append('-k')
288    cmd_list.append(sign_dir + '/' + outkey_dir + '/' + outkey_file_name)
289    cmd_list.append(release_file_dir + '/' + pub_otp_name)
290    if log_swich == "ON":
291        print(cmd_list)
292
293    child = subprocess.Popen(cmd_list)
294    is_fst = True
295    while True:
296        status = child.poll()
297        if status == 0:
298            break
299        if status == 1:
300            break
301        if str(status) == 'None' and is_fst:
302            print('Start make %s...'%(pub_otp_name))
303            is_fst = False
304    print("Make %s done.\n"%(pub_otp_name))
305    cmd_cp_list = []
306    cmd_cp_list.append('cp')
307    cmd_cp_list.append(release_file_dir + '/' + pub_otp_name)
308    cmd_cp_list.append(ota_bin_dir + '/')
309    if log_swich == "ON":
310        print(cmd_cp_list)
311    shutil.copy(release_file_dir + '/' + pub_otp_name, ota_bin_dir + '/')
312 #   child = subprocess.Popen(cmd_cp_list)
313    cmd_cp_list2 = []
314    cmd_cp_list2.append('cp')
315    cmd_cp_list2.append(release_file_dir + '/' + pub_otp_name)
316    cmd_cp_list2.append(gui_bin_dir + '/')
317    if log_swich == "ON":
318        print(cmd_cp_list2)
319    shutil.copy(release_file_dir + '/' + pub_otp_name, gui_bin_dir + '/')
320#    child = subprocess.Popen(cmd_cp_list2)
321    is_fst = True
322    while True:
323        status = child.poll()
324        if status == 0:
325            break
326        if status == 1:
327            break
328        if str(status) == 'None' and is_fst:
329            print('Cp %s from dir release_bin to dir ota_bin...' % (pub_otp_name))
330            is_fst = False
331    print("Cp %s done.\n" % (pub_otp_name))
332
333#  cp prebuild/*.bin  release_bin/
334if True:
335    if (osstr =="Linux"):
336        os.system('chmod 777 ' + release_file_dir + '/')
337    path = sign_file_dir
338    file_list = os.listdir(path)
339    for i in range(len(file_list)):
340        res_file_path = sign_file_dir + '/' + file_list[i]
341        des_path = release_file_dir + '/'
342        cmd_str = 'cp -f ' + res_file_path + ' ' + des_path
343        if log_swich == "ON":
344            print(cmd_str)
345        if os.path.isfile(res_file_path):
346            shutil.copy(res_file_path, des_path)
347#        os.system(cmd_str)
348
349#  cp out/helloworld_demo@haas1000/binary/helloworld_demo@haas1000.bin release_bin
350if True:
351    res_file_path = binary_dir + '/' + rtos_res_file_name
352    print("yxy %s" % os.getcwd())
353    print("yxy %s" % res_file_path)
354    if not glob.glob(res_file_path):
355        print("yxy %s" % glob.glob(res_file_path))
356        res_file_path = os.path.join(output_dir, "binary", rtos_res_file_name)
357    res_file_path = sys.argv[2]
358    des_path = release_file_dir + '/' + rtos_file_name
359    cmd_str = 'cp ' + res_file_path + ' ' + des_path
360    if log_swich == "ON":
361        print(cmd_str)
362        print(glob.glob(res_file_path)[0])
363    shutil.copy(glob.glob(res_file_path)[0], des_path)
364
365#  mv release_bin/helloworld_demo@haas1000.bin release_bin/ota_rtos.bin
366'''
367if True:
368    res_file_path = release_file_dir + '/' + rtos_res_file_name
369    des_file_path = release_file_dir + '/' + rtos_file_name
370    cmd_str = 'mv ' + res_file_path + ' ' + des_file_path
371    if log_swich == "ON":
372        print(cmd_str)
373    os.system(cmd_str)
374'''
375
376# cp ../write_flash_gui/dld_cfg to ../write_flash_gui/
377if False:
378    res_file_path = dld_cfg_dir + '/' + pro_dld_cfg_file
379    des_path = gui_tools_dir + '/'
380    cmd_str = 'cp -f ' + res_file_path + ' ' + des_path
381    if log_swich == "ON":
382        print(cmd_str)
383    shutil.copy(res_file_path, des_path)
384    #os.system(cmd_str)
385
386# mv ../write_flash_gui/dld_cfg/haas1000_dld_cfg_*.yaml to ../write_flash_gui/haas1000_dld_cfg.yaml
387if False:
388    res_file_path = gui_tools_dir + '/' + pro_dld_cfg_file
389    des_file_path = gui_tools_dir + '/' + dld_cfg_file
390    cmd_str = 'cp -f ' + res_file_path + ' ' + des_file_path
391    if log_swich == "ON":
392        print(cmd_str)
393    if (not os.path.isfile(des_file_path)) or (pro_dld_cfg_file != dld_cfg_file):
394        shutil.copy(res_file_path, des_path)
395    #os.system(cmd_str)
396
397# sign release_bin/*.bin --- according to configuration(bes_sign_cfg.yaml)
398if sign_file_switch == "ON":
399    #./ best_sign  key/pri.pem before_sign/noapp_test.bin
400    dict_sign_file = cfg_dict['SIGN_FILE_LIST']
401    print (dict_sign_file)
402    list_items_file = dict_sign_file
403    file_itme_len = len(list_items_file)
404    #print "file_itme_len %d " % file_itme_len
405    for index in range(file_itme_len):
406        sign_file_cmd_str = []
407        file_item = {}
408        #print "list_items_file[index] %s" %list_items_file[index]
409        file_item = list_items_file[index]
410        tmp_file_item = file_item;
411        if tmp_file_item:
412            print(str(file_item))
413            sign_file_name = tmp_file_item['FILE_NAME']
414            sign_switch = tmp_file_item['SWITCH']
415            #print "sign_file_name %s" % sign_file_name
416            #print "sign_switch %s" % sign_switch
417            if sign_switch and sign_file_name:
418                cmd_list = []
419                #os.system('chmod 777 ' + release_file_dir + '/' + sign_file_name)
420                cmd_list.append(sign_dir + '/' + sign_cmd)
421                cmd_list.append(sign_dir + '/' + in_key_dir + '/' + pri_key_name)
422                cmd_list.append(release_file_dir + '/' + sign_file_name)
423                if log_swich == "ON":
424                    print(cmd_list)
425                child = subprocess.Popen(cmd_list)
426                is_fst = True
427                while True:
428                    status = child.poll()
429                    if status == 0:
430                        break
431                    if status == 1:
432                        break
433                    if str(status) == 'None' and is_fst:
434                        print('Start sign file...')
435                        is_fst = False
436                print("Sign file done.\n")
437
438# mv release_bin/programmer2001.bin   write_flash_tool/tools/
439# changed by yanxiaoyong.yxy from True to False
440if False:
441    res_file_path = release_file_dir + '/' + 'programmer2001.bin'
442    des_file_path = download_tools_dir + '/'
443    cmd_str = 'cp -f ' + res_file_path + '  ' + des_file_path
444    if log_swich == "ON":
445        print(cmd_str)
446    shutil.copy(res_file_path, des_path)
447    #os.system(cmd_str)
448    res_file_path = download_tools_dir + '/' + 'programmer2001.bin'
449    des_file_path = gui_tools_dir + '/'
450    cmd_str = 'cp -f ' + res_file_path + '  ' + des_file_path
451    if log_swich == "ON":
452        print(cmd_str)
453    shutil.copy(res_file_path, des_path)
454    #os.system(cmd_str)
455
456# cp release_bin/*.bin ota_bin
457if True:
458    path = release_file_dir
459    file_list = os.listdir(path)
460    for i in range(len(file_list)):
461        res_file_path = release_file_dir + '/' + file_list[i]
462        des_path = ota_bin_dir + '/'
463        cmd_str = 'cp -f ' + res_file_path + ' ' + des_path
464        if log_swich == "ON":
465            print(cmd_str)
466        shutil.copy(res_file_path, des_path)
467        #os.system(cmd_str)
468        des_path = gui_bin_dir + '/'
469        cmd_str = 'cp -f ' + res_file_path + ' ' + des_path
470        if log_swich == "ON":
471            print(cmd_str)
472        shutil.copy(res_file_path, des_path)
473        #os.system(cmd_str)
474print('all files done.')
475
476if pack_switch == 'ON':
477    print('Pack files start...')
478    res_dir = cur_dir + '/' + sign_dir + '/' + sign_file_dir + '/*.bin'
479    dst_dir = release_file_dir + '/'
480    os.system('cp "' + res_dir + '" "' + dst_dir + '"')
481    cur_dir = os.getcwd()
482    to_pack_dir = os.getcwd() + '/' + release_file_dir
483    os.chdir(to_pack_dir)
484    changed_dir = os.getcwd()
485    to_pack_file_name = pack_file_name
486    pack_dir(changed_dir,to_pack_file_name)
487    os.chdir(cur_dir)
488    print('Pack files done.')
489
490