• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2022 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")
27
28# 变量注释 framework_res_dir = OpenHarmony/test/developer_test
29# 变量注释 CONFIG_PATH = OpenHarmony/test/developer_test/config
30CONFIG_PATH = os.path.join(sys.framework_res_dir, "config")
31
32
33# framework_config.xml
34class FrameworkConfigManager(object):
35
36    def __init__(self, filepath=""):
37        if filepath == "":
38
39            # 变量注释 filepath = OpenHarmony/test/developer_test/config/framework_config.xml
40            self.filepath = os.path.abspath(os.path.join(
41                CONFIG_PATH, ConfigFileConst.FRAMECONFIG_FILEPATH))
42        else:
43            self.filepath = filepath
44
45    def get_framework_config(self, target_name):
46        # 获取framework_config.xml中所有name的value,返回列表
47        data_list = []
48        try:
49            if os.path.exists(self.filepath):
50
51                # 读取xml文件
52                tree = ET.parse(self.filepath)
53
54                # 获取根节点
55                root = tree.getroot()
56
57                # 获取节点的名字
58                node = root.find(target_name)
59
60                # 遍历 name 节点 获取value 添加到data_list中
61                for sub in node:
62                    value = sub.attrib.get("name")
63                    if value and value != "":
64                        data_list.append(value)
65        except ET.ParseError as xml_exception:
66            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
67        return data_list
68
69    # 获取framework_config.xml中test_category标签里name、desc、timeout的value,返回字典
70    def get_test_category_info(self, target_name="test_category"):
71        test_type_timeout_dic = {}
72        try:
73            if os.path.exists(self.filepath):
74                tree = ET.parse(self.filepath)
75                root = tree.getroot()
76                node = root.find(target_name)
77                for sub in node:
78                    name = sub.attrib.get("name")
79                    desc = sub.attrib.get("desc")
80                    timeout = sub.attrib.get("timeout")
81                    if name and desc and timeout:
82                        test_type_timeout_dic[name] = (desc, timeout)
83            else:
84                LOG.error("The %s file does not exist." % self.filepath)
85        except ET.ParseError as xml_exception:
86            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
87        return test_type_timeout_dic
88
89    # 获取framework_config.xml中all_category标签里name的value,返回列表 如:unittest、moduletest、systemtest、performance
90    def get_all_category_info(self, target_name="all_category"):
91        return self.get_framework_config(target_name)
92
93
94# filter_config.xml
95class FilterConfigManager(object):
96    def __init__(self, filepath=""):
97        if filepath == "":
98
99            # 变量注释 filepath = OpenHarmony/test/developer_test/config/filter_config.xml
100            self.filepath = os.path.abspath(
101                os.path.join(CONFIG_PATH,
102                             ConfigFileConst.FILTERCONFIG_FILEPATH))
103        else:
104            self.filepath = filepath
105
106    def get_filtering_list(self, target_name, product_form):
107        filter_data_list = []
108        try:
109            if os.path.exists(self.filepath):
110                tree = ET.parse(self.filepath)
111                root = tree.getroot()
112                for child in root:
113                    if child.tag != target_name:
114                        continue
115                    for child2 in child:
116                        if child2.tag.lower() != product_form.lower():
117                            continue
118                        for child3 in child2:
119                            if child3.text != "" and child3.text is not None:
120                                filter_data_list.append(child3.text)
121            else:
122                LOG.error("The %s file does not exist." % self.filepath)
123        except ET.ParseError as xml_exception:
124            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
125        return filter_data_list
126
127    def get_filter_config_path(self):
128        return self.filepath
129
130
131# 这里的filepath不存在
132class ResourceConfigManager(object):
133    def __init__(self, filepath=""):
134        if filepath == "":
135            self.filepath = os.path.abspath(os.path.join(
136                CONFIG_PATH, ConfigFileConst.RESOURCECONFIG_FILEPATH))
137            if not os.path.exists(self.filepath):
138                self.filepath = os.path.abspath(os.path.join(
139                    CONFIG_PATH, ConfigFileConst.CASE_RESOURCE_FILEPATH))
140        else:
141            self.filepath = filepath
142
143    def get_resource_config(self):
144        data_list = []
145        try:
146            if os.path.exists(self.filepath):
147                tree = ET.parse(self.filepath)
148                root = tree.getroot()
149                for child in root:
150                    temp_list = [child.attrib]
151                    for sub in child:
152                        temp_list.append(sub.attrib)
153                    data_list.append(temp_list)
154            else:
155                LOG.error("The %s is not exist." % self.filepath)
156        except ET.ParseError as xml_exception:
157            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
158        return data_list
159
160    def get_resource_config_path(self):
161        return self.filepath
162
163
164class UserConfigManager(object):
165    def __init__(self, config_file=""):
166        if config_file == "":
167
168            # 变量注释 filepath = OpenHarmony/test/developer_test/config/user_config.xml
169            self.filepath = os.path.abspath(os.path.join(
170                CONFIG_PATH, ConfigFileConst.USERCONFIG_FILEPATH))
171        else:
172            if os.path.isabs(config_file):
173                self.filepath = config_file
174            else:
175                self.filepath = os.path.abspath(
176                    os.path.join(CONFIG_PATH, config_file))
177
178    def get_user_config_list(self, tag_name):
179        data_dic = {}
180        try:
181            if os.path.exists(self.filepath):
182                tree = ET.parse(self.filepath)
183                root = tree.getroot()
184                for child in root:
185                    if tag_name == child.tag:
186                        for sub in child:
187                            data_dic[sub.tag] = sub.text
188        except ET.ParseError as xml_exception:
189            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
190        return data_dic
191
192    @classmethod
193    def content_strip(cls, content):
194        return content.strip()
195
196    @classmethod
197    def _verify_duplicate(cls, items):
198        if len(set(items)) != len(items):
199            LOG.warning("find duplicate sn config, configuration incorrect")
200            return False
201        return True
202
203    def _handle_str(self, content):
204        config_list = map(self.content_strip, content.split(';'))
205        config_list = [item for item in config_list if item]
206        if config_list:
207            if not self._verify_duplicate(config_list):
208                return []
209        return config_list
210
211    def get_sn_list(self):
212        sn_select_list = []
213        try:
214            data_dic = {}
215            if os.path.exists(self.filepath):
216                tree = ET.parse(self.filepath)
217                root = tree.getroot()
218
219                for node in root.findall("environment/device"):
220                    if node.attrib["type"] != "usb-hdc":
221                        continue
222                    for sub in node:
223                        data_dic[sub.tag] = sub.text if sub.text else ""
224                    sn_config = data_dic.get("sn", "")
225                    if sn_config:
226                        sn_select_list = self._handle_str(sn_config)
227                    break
228        except ET.ParseError as xml_exception:
229            LOG.warning("occurs exception:{}".format(xml_exception.args))
230            sn_select_list = []
231        return sn_select_list
232
233    def get_user_config(self, target_name, sub_target=""):
234        data_dic = {}
235        try:
236            if os.path.exists(self.filepath):
237                tree = ET.parse(self.filepath)
238                root = tree.getroot()
239
240                node = root.find(target_name)
241                if not node:
242                    return data_dic
243
244                if sub_target != "":
245                    node = node.find(sub_target)
246                if not node:
247                    return data_dic
248
249                for sub in node:
250                    if sub.text is None:
251                        data_dic[sub.tag] = ""
252                    else:
253                        data_dic[sub.tag] = sub.text
254        except ET.ParseError as xml_exception:
255            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
256        return data_dic
257
258    def get_user_config_flag(self, target_name, sub_target):
259        config_flag = self.get_user_config(target_name).get(sub_target, "")
260        if config_flag == "":
261            return False
262        return True if config_flag.lower() == "true" else False
263
264    def get_device(self, target_name):
265        data_dic = {}
266        if os.path.exists(self.filepath):
267            tree = ET.parse(self.filepath)
268            config_content = tree.getroot()
269            for node in config_content.findall(target_name):
270                for sub in node:
271                    if sub.text is None:
272                        data_dic[sub.tag] = ""
273                    else:
274                        data_dic[sub.tag] = sub.text
275                break
276        return data_dic
277
278    def get_test_cases_dir(self):
279        testcase_path = self.get_user_config("test_cases").get("dir", "")
280        if testcase_path != "":
281            testcase_path = os.path.abspath(testcase_path)
282        return testcase_path
283
284
285class BuildConfigManager(object):
286    def __init__(self, filepath=""):
287        if filepath == "":
288
289            # 变量注释 filepath = OpenHarmony/test/developer_test/config/build_config.xml
290            self.filepath = os.path.abspath(os.path.join(
291                CONFIG_PATH, ConfigFileConst.BUILDCONFIG_FILEPATH))
292        else:
293            self.filepath = filepath
294
295    def get_build_config(self, target_name):
296        data_list = []
297        try:
298            if os.path.exists(self.filepath):
299                tree = ET.parse(self.filepath)
300                root = tree.getroot()
301                node = root.find(target_name)
302                for sub in node:
303                    value = sub.attrib.get("name")
304                    if value and value != "":
305                        data_list.append(value)
306        except ET.ParseError as xml_exception:
307            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
308        return data_list
309
310    def get_build_path(self):
311        return self.filepath
312
313
314class FuzzerConfigManager(object):
315    def __init__(self, config_path=""):
316        if config_path == "":
317
318            # 变量注释 filepath = OpenHarmony/test/developer_test/config/fuzz_config.xml
319            self.filepath = self.filepath = os.path.abspath(os.path.join(
320                CONFIG_PATH, ConfigFileConst.FUZZCONFIG_FILEPATH))
321        else:
322            self.filepath = config_path
323
324    def get_fuzzer_config(self, target_name):
325        config_list = []
326        try:
327            LOG.info("fuzzer config file :%s" % self.filepath)
328            if os.path.exists(self.filepath):
329                tree = ET.parse(self.filepath)
330                root = tree.getroot()
331                node = root.find(target_name)
332                LOG.info("before for")
333                for sub in node:
334                    if sub.text is not None:
335                        config_list.append(sub.text)
336        except ET.ParseError as xml_exception:
337            LOG.error(("Parse %s fail!" % self.filepath) + xml_exception.args)
338        return config_list
339