1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import sys 20import os 21import xml.etree.ElementTree as ET 22 23from xdevice import platform_logger 24from core.constants import ConfigFileConst 25 26LOG = platform_logger("config_manager") 27CONFIG_PATH = os.path.join(sys.framework_res_dir, "config") 28 29 30class FrameworkConfigManager(object): 31 def __init__(self, filepath=""): 32 if filepath == "": 33 self.filepath = os.path.abspath(os.path.join( 34 CONFIG_PATH, ConfigFileConst.FRAMECONFIG_FILEPATH)) 35 else: 36 self.filepath = filepath 37 38 def get_framework_config(self, target_name): 39 data_list = [] 40 try: 41 if os.path.exists(self.filepath): 42 tree = ET.parse(self.filepath) 43 root = tree.getroot() 44 node = root.find(target_name) 45 for sub in node: 46 value = sub.attrib.get("name") 47 if value and value != "": 48 data_list.append(value) 49 except ET.ParseError as xml_exception: 50 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 51 return data_list 52 53 def get_test_category_info(self, target_name="test_category"): 54 test_type_timeout_dic = {} 55 try: 56 if os.path.exists(self.filepath): 57 tree = ET.parse(self.filepath) 58 root = tree.getroot() 59 node = root.find(target_name) 60 for sub in node: 61 name = sub.attrib.get("name") 62 desc = sub.attrib.get("desc") 63 timeout = sub.attrib.get("timeout") 64 if name and desc and timeout: 65 test_type_timeout_dic[name] = (desc, timeout) 66 else: 67 LOG.error("The %s file does not exist." % self.filepath) 68 except ET.ParseError as xml_exception: 69 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 70 return test_type_timeout_dic 71 72 def get_all_category_info(self, target_name="all_category"): 73 return self.get_framework_config(target_name) 74 75 76class FilterConfigManager(object): 77 def __init__(self, filepath=""): 78 if filepath == "": 79 self.filepath = os.path.abspath( 80 os.path.join(CONFIG_PATH, 81 ConfigFileConst.FILTERCONFIG_FILEPATH)) 82 else: 83 self.filepath = filepath 84 85 def get_filtering_list(self, target_name, product_form): 86 filter_data_list = [] 87 try: 88 if os.path.exists(self.filepath): 89 tree = ET.parse(self.filepath) 90 root = tree.getroot() 91 for child in root: 92 if child.tag != target_name: 93 continue 94 for child2 in child: 95 if child2.tag.lower() != product_form.lower(): 96 continue 97 for child3 in child2: 98 if child3.text != "" and child3.text is not None: 99 filter_data_list.append(child3.text) 100 else: 101 LOG.error("The %s file does not exist." % self.filepath) 102 except ET.ParseError as xml_exception: 103 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 104 return filter_data_list 105 106 def get_filter_config_path(self): 107 return self.filepath 108 109 110class ResourceConfigManager(object): 111 def __init__(self, filepath=""): 112 if filepath == "": 113 self.filepath = os.path.abspath(os.path.join( 114 CONFIG_PATH, ConfigFileConst.RESOURCECONFIG_FILEPATH)) 115 if not os.path.exists(self.filepath): 116 self.filepath = os.path.abspath(os.path.join( 117 CONFIG_PATH, ConfigFileConst.CASE_RESOURCE_FILEPATH)) 118 else: 119 self.filepath = filepath 120 121 def get_resource_config(self): 122 data_list = [] 123 try: 124 if os.path.exists(self.filepath): 125 tree = ET.parse(self.filepath) 126 root = tree.getroot() 127 for child in root: 128 temp_list = [child.attrib] 129 for sub in child: 130 temp_list.append(sub.attrib) 131 data_list.append(temp_list) 132 else: 133 LOG.error("The %s is not exist." % self.filepath) 134 except ET.ParseError as xml_exception: 135 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 136 return data_list 137 138 def get_resource_config_path(self): 139 return self.filepath 140 141 142class UserConfigManager(object): 143 def __init__(self, config_file=""): 144 if config_file == "": 145 self.filepath = os.path.abspath(os.path.join( 146 CONFIG_PATH, ConfigFileConst.USERCONFIG_FILEPATH)) 147 else: 148 if os.path.isabs(config_file): 149 self.filepath = config_file 150 else: 151 self.filepath = os.path.abspath( 152 os.path.join(CONFIG_PATH, config_file)) 153 154 def get_user_config_list(self, tag_name): 155 data_dic = {} 156 try: 157 if os.path.exists(self.filepath): 158 tree = ET.parse(self.filepath) 159 root = tree.getroot() 160 for child in root: 161 if tag_name == child.tag: 162 for sub in child: 163 data_dic[sub.tag] = sub.text 164 except ET.ParseError as xml_exception: 165 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 166 return data_dic 167 168 @classmethod 169 def content_strip(cls, content): 170 return content.strip() 171 172 @classmethod 173 def _verify_duplicate(cls, items): 174 if len(set(items)) != len(items): 175 LOG.warning("find duplicate sn config, configuration incorrect") 176 return False 177 return True 178 179 def _handle_str(self, content): 180 config_list = map(self.content_strip, content.split(';')) 181 config_list = [item for item in config_list if item] 182 if config_list: 183 if not self._verify_duplicate(config_list): 184 return [] 185 return config_list 186 187 def get_sn_list(self): 188 sn_select_list = [] 189 try: 190 data_dic = {} 191 if os.path.exists(self.filepath): 192 tree = ET.parse(self.filepath) 193 root = tree.getroot() 194 195 for node in root.findall("environment/device"): 196 if node.attrib["type"] != "usb-hdc": 197 continue 198 for sub in node: 199 data_dic[sub.tag] = sub.text if sub.text else "" 200 sn_config = data_dic.get("sn", "") 201 if sn_config: 202 sn_select_list = self._handle_str(sn_config) 203 break 204 except ET.ParseError as xml_exception: 205 LOG.warning("occurs exception:{}".format(xml_exception.args)) 206 sn_select_list = [] 207 return sn_select_list 208 209 def get_user_config(self, target_name, sub_target=""): 210 data_dic = {} 211 try: 212 if os.path.exists(self.filepath): 213 tree = ET.parse(self.filepath) 214 root = tree.getroot() 215 216 node = root.find(target_name) 217 if not node: 218 return None 219 220 if sub_target != "": 221 node = node.find(sub_target) 222 if not node: 223 return None 224 225 for sub in node: 226 if sub.text is None: 227 data_dic[sub.tag] = "" 228 else: 229 data_dic[sub.tag] = sub.text 230 except ET.ParseError as xml_exception: 231 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 232 return data_dic 233 234 def get_user_config_flag(self, target_name, sub_target): 235 config_flag = self.get_user_config(target_name).get(sub_target, "") 236 if config_flag == "": 237 return False 238 return True if config_flag.lower() == "true" else False 239 240 def get_device(self, target_name): 241 data_dic = {} 242 if os.path.exists(self.filepath): 243 tree = ET.parse(self.filepath) 244 config_content = tree.getroot() 245 for node in config_content.findall(target_name): 246 for sub in node: 247 if sub.text is None: 248 data_dic[sub.tag] = "" 249 else: 250 data_dic[sub.tag] = sub.text 251 break 252 return data_dic 253 254 def get_test_cases_dir(self): 255 testcase_path = self.get_user_config("test_cases").get("dir", "") 256 if testcase_path != "": 257 testcase_path = os.path.abspath(testcase_path) 258 return testcase_path 259 260 261class BuildConfigManager(object): 262 def __init__(self, filepath=""): 263 if filepath == "": 264 self.filepath = os.path.abspath(os.path.join( 265 CONFIG_PATH, ConfigFileConst.BUILDCONFIG_FILEPATH)) 266 else: 267 self.filepath = filepath 268 269 def get_build_config(self, target_name): 270 data_list = [] 271 try: 272 if os.path.exists(self.filepath): 273 tree = ET.parse(self.filepath) 274 root = tree.getroot() 275 node = root.find(target_name) 276 for sub in node: 277 value = sub.attrib.get("name") 278 if value and value != "": 279 data_list.append(value) 280 except ET.ParseError as xml_exception: 281 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 282 return data_list 283 284 def get_build_path(self): 285 return self.filepath 286 287class FuzzerConfigManager(object): 288 def __init__(self, config_path=""): 289 if config_path == "": 290 self.filepath = self.filepath = os.path.abspath(os.path.join( 291 CONFIG_PATH, ConfigFileConst.FUZZCONFIG_FILEPATH)) 292 else: 293 self.filepath = config_path 294 295 def get_fuzzer_config(self, target_name): 296 config_list = [] 297 try: 298 LOG.info("fuzzer config file :%s" % self.filepath) 299 if os.path.exists(self.filepath): 300 tree = ET.parse(self.filepath) 301 root = tree.getroot() 302 node = root.find(target_name) 303 LOG.info("before for") 304 for sub in node: 305 if sub.text is not None: 306 config_list.append(sub.text) 307 except ET.ParseError as xml_exception: 308 LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args) 309 return config_list 310