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 25from xml.etree.ElementTree import ElementTree 26 27XML_PATH="build/config/target_config/<chip>/hdb_config/database_cfg/mk_hdb_xml.json" 28 29def get_msg_root(subsystem): 30 msg_root_element = ET.Element('') 31 for child in subsystem: 32 if child.tag == 'MSG_LOG': 33 msg_root_element = child 34 break 35 if msg_root_element.tag == 'MSG_LOG': 36 temp_attrib = msg_root_element.attrib 37 temp_text = msg_root_element.text 38 temp_tail = msg_root_element.tail 39 msg_root_element.attrib = temp_attrib 40 msg_root_element.text = temp_text 41 msg_root_element.tail = temp_tail 42 return msg_root_element 43 44def get_subsystem_by_name(tree, name): 45 root = tree.getroot() 46 for child in root: 47 if child.attrib["NAME"] == name: 48 return child 49 return None 50 51def merge_db_xml(root, chip, core_name): 52 53 xml_src_file = G_PARAMS['HDB_PRIM_XML_SRC_FILE'].strip() 54 dst_xml_file = G_PARAMS['HDB_PRIM_XML_DST_FILE'].strip() 55 base_dir = G_PARAMS['HDB_XML_TEMP_BASE_DIR'] 56 57 mod_list = [] 58 if core_name == 'bt_core': 59 mod_list = ['bt_core', 'bt_status', 'ota_msg'] #bt_status.xml、ota_msg.xml目前都是在bt编译时生成 60 else: 61 mod_list = [core_name] 62 63 dst_xml_dir = os.path.abspath(os.path.join(dst_xml_file, "..")) 64 if not os.path.isdir(dst_xml_dir): 65 os.makedirs(dst_xml_dir) 66 67 if not os.path.exists(dst_xml_file): 68 shutil.copy(xml_src_file, dst_xml_file) 69 70 for mod in mod_list: 71 if not os.path.isdir(os.path.join(base_dir, mod)): 72 continue 73 tree_dst = ET.parse(dst_xml_file) 74 subsystem = get_subsystem_by_name(tree_dst, mod) 75 if subsystem is None: 76 print("Error: subsystem %s is not in %s" %(mod, dst_xml_file)) 77 continue 78 79 msg_root_element = get_msg_root(subsystem) 80 msg_root_element.clear() 81 82 filelist = os.listdir(os.path.join(base_dir, mod)) 83 for filename in filelist: 84 tmp_xml_file = os.path.join(os.path.join(base_dir, mod), filename) 85 tree = ElementTree() 86 tree.parse(tmp_xml_file) 87 root = tree.getroot() 88 if root.tag == 'MSG_LOG': 89 for child in root: 90 msg_root_element.append(child) 91 else: 92 subsystem_src = get_subsystem_by_name(tree, mod) 93 msg_element_src = get_msg_root(subsystem_src) 94 for child in msg_element_src: 95 msg_root_element.append(child) 96 tree_dst.write(dst_xml_file) 97 98# main 99if __name__ == "__main__": 100 global G_PARAMS 101 102 root = sys.argv[1] 103 chip = sys.argv[2] 104 core_name = sys.argv[3] 105 if len(sys.argv) == 5: 106 hso_en_bt = sys.argv[4] 107 else: 108 hso_en_bt = "" 109 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['HDB_PRIM_XML_DST_FILE'] = os.path.join(root, db_conf["HDB_PRIM_XML_DST_FILE"]) 121 G_PARAMS['HDB_PRIM_XML_SRC_FILE'] = os.path.join(root, db_conf["HDB_PRIM_XML_SRC_FILE"]) 122 123 merge_db_xml(root, chip, core_name) 124 if hso_en_bt == "True": 125 merge_db_xml(root, chip, 'bt_core') 126 127 if ("HDB_XML_PRE_GENERATED_CORE" in db_conf): 128 G_PARAMS['HDB_XML_PRE_GENERATED_CORE'] = db_conf["HDB_XML_PRE_GENERATED_CORE"] 129 core_list = [] 130 core_list = G_PARAMS['HDB_XML_PRE_GENERATED_CORE'] 131 132 for core in core_list: 133 merge_db_xml(root, chip, core) 134