1#!/usr/bin/env python3 2# coding=utf-8 3# Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import re 18import sys 19import platform 20import string 21import shutil 22import time 23import json 24import xml.etree.ElementTree as ET 25 26py_version = platform.python_version() 27 28G_PARAMS = {} 29XML_PATH="build/config/target_config/<chip>/hdb_config/database_cfg/mk_hdb_xml.json" 30ota_msg_list = [] 31 32def gen_ota_msg_dict_fun(datalines): 33 global ota_msg_list 34 # 遍历行内容 35 for line in datalines: 36 line = line.replace('\r\n', '') 37 line = line.replace('\t', ' ') 38 line = line.replace('\\r', "") 39 line = line.replace('\\n', "") 40 line = line.replace('\r', "") 41 line = line.replace('\n', "") 42 line = line.replace("\\", "") 43 if len(line) == 0: 44 continue 45 m = re.search(r'^\/\/', line.strip()) 46 if m is not None: 47 continue 48 m = re.search(r'\[.*(START|END)\]', line.strip()) 49 if m is not None: 50 continue 51 52 member = line.strip().split(" ") 53 if len(member) == 2: 54 data = {} 55 data["NAME"] = member[1] 56 data["ID"] = member[0] 57 ota_msg_list.append(data) 58 59def add_content_to_xml(data, msg_root_element): 60 new_element = ET.Element('') 61 new_element.tag = 'MSG' 62 new_element.attrib['NAME'] = data["NAME"] 63 new_element.attrib['ID'] = hex((2 << 16) | (0xffff & int(data["ID"], 16))) # OM_MSG_TYPE_OTA = 2 64 new_element.tail = '\n\t\t\t' 65 msg_root_element.append(new_element) 66 67def write_ota_msg_prim_xml_file(): 68 global G_PARAMS 69 70 base_file_dir = os.path.join(G_PARAMS['HDB_XML_TEMP_BASE_DIR'], "ota_msg") 71 if not os.path.isdir(base_file_dir): 72 os.makedirs(base_file_dir) 73 74 dst_xml_full_name = os.path.join(base_file_dir, "ota_msg.xml") 75 76 msg_root_element = ET.Element('MSG_LOG') 77 for ota_msg in ota_msg_list: 78 add_content_to_xml(ota_msg, msg_root_element) 79 80 tree = ET.ElementTree(msg_root_element) 81 tree.write(dst_xml_full_name) 82 83def generate_db_file(): 84 global G_PARAMS 85 ota_msg_dir = G_PARAMS["OTA_MSG_DIR"] 86 87 ota_msg_file = os.path.join(ota_msg_dir, "OTA_MSG_List.txt") 88 try: 89 if py_version.startswith("3"): 90 try: 91 with open(ota_msg_file, 'r', encoding="UTF-8") as fd_src: 92 datalines = [line for line in fd_src] 93 except Exception: 94 with open(ota_msg_file, 'r', encoding="ISO-8859-1") as fd_src: 95 datalines = [line for line in fd_src] 96 else: 97 with open(ota_msg_file, 'r') as fd_src: 98 datalines = [line for line in fd_src] 99 except Exception: 100 print("open file %s failed." % cfilepath) 101 return 102 103 gen_ota_msg_dict_fun(datalines) 104 write_ota_msg_prim_xml_file() 105 106def generate_db(): 107 global G_PARAMS 108 root = sys.argv[1] 109 chip = sys.argv[2] 110 in_path = XML_PATH 111 in_path = in_path.replace('<chip>', chip) 112 113 db_conf = None 114 conf = os.path.join(root, in_path) 115 with open(conf, 'r') as f: 116 db_conf = json.load(f) 117 118 G_PARAMS = {} 119 G_PARAMS['HDB_XML_TEMP_BASE_DIR'] = os.path.join(root, db_conf["HDB_XML_TEMP_BASE_DIR"]) 120 G_PARAMS["OTA_MSG_DIR"] = os.path.join(root, db_conf["OTA_MSG_DIR"]) 121 122 generate_db_file() 123 124# main 125if __name__ == "__main__": 126 generate_db() 127