1#!/usr/bin/env python 2# coding:utf-8 3 4# 5# Copyright (C) 2022 Huawei Technologies Co., Ltd. 6# Licensed under the Mulan PSL v2. 7# You can use this software according to the terms and conditions of the Mulan 8# PSL v2. 9# You may obtain a copy of Mulan PSL v2 at: 10# http://license.coscl.org.cn/MulanPSL2 11# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY 12# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 13# NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14# See the Mulan PSL v2 for more details. 15# 16 17import string 18import os 19import logging 20from defusedxml import ElementTree as ET 21 22 23type_trans = {"TYPE_NONE": "-1", 24 "TYPE_CLASS": "0", 25 "TYPE_BOOL": "1", 26 "TYPE_INT": "2", 27 "TYPE_CHAR": "3"} 28 29type_dict = {} 30manifest_dict = {} 31 32 33def get_csv_size(path): 34 35 with open(path, "r", encoding="utf-8") as csvfile: 36 lines = csvfile.readlines() 37 return len(lines) 38 return 0 39 40 41def get_csv_data(path, lnum, rnum): 42 with open(path, "r", encoding="utf-8") as csvfile: 43 count = 0 44 lines = csvfile.readlines() 45 for line in lines: 46 if count == lnum: 47 return str(line.split(",")[rnum]).strip() 48 count = count + 1 49 return "" 50 51 52def classify_tag(tag): 53 54 while len(tag) < 3: 55 tag = "0%s" % (tag) 56 57 return tag 58 59 60# save tag type and manifest item dict 61def handle_manifest_tag_dict(path): 62 for index in range(0, get_csv_size(path)): 63 dyn_sym = get_csv_data(path, index, 0) 64 type_dict[dyn_sym] = type_trans.get(get_csv_data(path, index, 2)) 65 manifest_dict[dyn_sym] = get_csv_data(path, index, 3) 66 67 68def process_xml_to_manifest(config_xml_file_path, manifest_path): 69 tree = ET.parse(config_xml_file_path) 70 root = tree.getroot() 71 #Layer 1 node name 72 old_item = root.tag 73 attrs = "" 74 write_data = False 75 76 #write items to manifest.txt 77 manifest_fd = os.open(manifest_path, os.O_CREAT | os.O_RDWR, 0o600) 78 manifest_fp = os.fdopen(manifest_fd, "wb") 79 80 #Traversing the second layer of the xml file 81 for child in root: 82 child_item = "{}/{}".format(old_item, child.tag) 83 #Traversing the third layer of the xml file 84 for children in child: 85 children_item = "{}/{}".format(child_item, children.tag) 86 dyn_type = type_dict.get(children_item + attrs) 87 manifest_item_name = manifest_dict.get(children_item + attrs) 88 if dyn_type == type_trans.get("TYPE_CHAR"): 89 value = "{}: {}\n".format(manifest_item_name, children.text) 90 manifest_fp.write(value.encode()) 91 write_data = True 92 93 #close manifest.txt file 94 manifest_fp.close() 95 if write_data is False: 96 os.remove(manifest_path) 97 98 99def trans_xml_to_manifest(config_xml_file_path, manifest_path): 100 if not os.path.exists(config_xml_file_path): 101 logging.error("config xml file doesn't exist") 102 return 103 if not os.path.exists("./manifest_tag_parse_dict.csv"): 104 logging.error("config manifest_tag_parse_dict.csv file doesn't exist") 105 return 106 if os.path.exists(manifest_path): 107 return 108 109 handle_manifest_tag_dict("./manifest_tag_parse_dict.csv") 110 process_xml_to_manifest(config_xml_file_path, manifest_path) 111