1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2020 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 def get_config_file_path(self): 36 manager = UserConfigManager() 37 testcase_dir = manager.get_test_cases_dir() 38 39 if testcase_dir == "": 40 if sys.source_code_root_path != "": 41 config_filepath = os.path.join( 42 get_build_output_path(self.productform), 43 "build_configs", 44 "infos_for_testfwk.json") 45 else: 46 config_filepath = "" 47 else: 48 config_filepath = os.path.join( 49 testcase_dir, 50 "infos_for_testfwk.json") 51 return config_filepath 52 53 def get_infos_data(self): 54 config_filepath = self.get_config_file_path() 55 if not os.path.exists(config_filepath): 56 print("Error: %s is not exist." % config_filepath) 57 return None, None 58 59 data_dic = None 60 with open(config_filepath, 'r') as file_handle: 61 data_dic = json.load(file_handle) 62 if not data_dic: 63 print("Error: json file load error.") 64 return None, None 65 66 # open source branch, the part form of all product is "phone" 67 if is_open_source_product(self.productform): 68 product_data_dic = data_dic.get("phone", None) 69 else: 70 product_data_dic = data_dic.get(self.productform, None) 71 if product_data_dic is None: 72 print("Error: product_data_dic is None.") 73 return None, None 74 75 subsystem_infos = product_data_dic.get("subsystem_infos", None) 76 part_infos = product_data_dic.get("part_infos", None) 77 return subsystem_infos, part_infos 78 79 def get_subsystem_infos(self): 80 return self.subsystem_infos 81 82 def get_part_infos(self): 83 return self.part_infos 84 85 def get_subsystem_name_list(self): 86 subsystem_name_list = [] 87 if self.subsystem_infos: 88 for item in self.subsystem_infos: 89 subsystem_name_list.append(item) 90 return subsystem_name_list 91 92 def get_part_list(self, subsystemlist, partlist): 93 if len(partlist) != 0: 94 return partlist 95 96 if self.subsystem_infos is None: 97 return subsystemlist 98 99 part_name_list = [] 100 if len(subsystemlist) != 0: 101 for item in subsystemlist: 102 parts = self.subsystem_infos.get(item, []) 103 part_name_list.extend(parts) 104 return part_name_list 105 106 107############################################################################## 108############################################################################## 109