1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2022 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import sys 19import os 20import json 21 22from core.utils import get_build_output_path 23from core.common import is_open_source_product 24from core.config.config_manager import UserConfigManager 25 26 27############################################################################## 28############################################################################## 29 30class ParsePartsConfig(object): 31 def __init__(self, productform): 32 self.productform = productform 33 self.subsystem_infos, self.part_infos = self.get_infos_data() 34 35 # 获取配置文件地址:~/OpenHarmony/out/rk3568/build_configs/infos_for_testfwk.json(以rk3568举例) 36 def get_config_file_path(self): 37 manager = UserConfigManager() 38 # 获取user_config.xml文件中的配置的<test_cases>(编译好的测试用例地址) 39 testcase_dir = manager.get_test_cases_dir() 40 41 # 如果没有在developtertest/config/user_config里配置test_cases路径, 42 # 就到OpenHarmony/out/rk3568/build_configs/infos_for_testfwk.json里查找 43 if testcase_dir == "": 44 if sys.source_code_root_path != "": 45 config_filepath = os.path.join( 46 get_build_output_path(self.productform), 47 "build_configs", 48 "infos_for_testfwk.json") 49 else: 50 config_filepath = "" 51 52 # 如果在developtertest/config/user_config里配置了test_cases路径,就在这个路径下的infos_for_testfwk.json里查找 53 else: 54 config_filepath = os.path.join( 55 testcase_dir, 56 "infos_for_testfwk.json") 57 return config_filepath 58 59 def get_infos_data(self): 60 config_filepath = self.get_config_file_path() 61 62 # 检验给出的路径是否真地存在 63 if not os.path.exists(config_filepath): 64 return None, None 65 66 data_dic = None 67 with open(config_filepath, 'r') as file_handle: 68 data_dic = json.load(file_handle) 69 if not data_dic: 70 print("Error: json file load error.") 71 return None, None 72 73 # open source branch, the part form of all product is "phone" 74 if is_open_source_product(self.productform): 75 product_data_dic = data_dic.get("phone", None) 76 else: 77 product_data_dic = data_dic.get(self.productform, None) 78 # product_data_dic:infos_for_testfwk.json配置文件中“phone”节点数据 79 if product_data_dic is None: 80 print("Error: product_data_dic is None.") 81 return None, None 82 # subsystem_infos(系统中定义的子系统列表):infos_for_testfwk.json配置文件中“phone”节点下“subsystem_infos”节点数据 83 subsystem_infos = product_data_dic.get("subsystem_infos", None) 84 # subsystem_infos(系统中定义的部件信息列表):infos_for_testfwk.json配置文件中“phone”节点下“part_infos”节点数据 85 part_infos = product_data_dic.get("part_infos", None) 86 return subsystem_infos, part_infos 87 88 def get_subsystem_infos(self): 89 return self.subsystem_infos 90 91 def get_part_infos(self): 92 return self.part_infos 93 94 def get_subsystem_name_list(self): 95 subsystem_name_list = [] 96 if self.subsystem_infos: 97 for item in self.subsystem_infos: 98 subsystem_name_list.append(item) 99 return subsystem_name_list 100 101 # 获取部件列表 102 def get_part_list(self, subsystemlist, partlist): 103 # 如果options参数中的partlist不为空,直接返回partlist 104 if len(partlist) != 0: 105 return partlist 106 # 如果infos_for_testfwk.json配置文件的subsystem_infos为None,返回options参数中的subsystemlist 107 if self.subsystem_infos is None: 108 return subsystemlist 109 110 part_name_list = [] 111 # 遍历options参数中的子系统列表,并且将infos_for_testfwk.json配置文件的subsystem_infos中的对应子系统的部件列表加入到part_name_list中 112 if len(subsystemlist) != 0: 113 for item in subsystemlist: 114 parts = self.subsystem_infos.get(item, []) 115 part_name_list.extend(parts) 116 return part_name_list 117 118 119############################################################################## 120############################################################################## 121