1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-2021 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 os 20import json 21from xdevice import platform_logger 22from core.utils import get_file_list_by_postfix 23from core.utils import get_build_output_path 24from core.config.parse_parts_config import ParsePartsConfig 25 26LOG = platform_logger("SelectTargets") 27 28 29############################################################################## 30############################################################################## 31 32class SelectTargets(object): 33 def __init__(self, project_rootpath): 34 self.project_rootpath = project_rootpath 35 36 @classmethod 37 def _get_mlf_data_from_file(cls, filepath): 38 data_list = [] 39 if os.path.exists(filepath): 40 with open(filepath, 'r') as mlf_file: 41 data_list = json.load(mlf_file) 42 if not data_list: 43 LOG.warning("The %s file load error." % filepath) 44 data_list = [] 45 return data_list 46 47 @classmethod 48 def _get_part_path_data(cls, productform): 49 part_path_dic = {} 50 parser = ParsePartsConfig(productform) 51 52 part_infos = parser.get_part_infos() 53 if part_infos is None: 54 LOG.error("part_infos is None.") 55 return part_path_dic 56 57 for part_name in part_infos: 58 part_info = part_infos.get(part_name, None) 59 if part_info is None: 60 continue 61 62 origin_part_name = part_info.get("origin_part_name") 63 build_out_dir = part_info.get("build_out_dir") 64 65 part_path_list = [] 66 default_part_path = os.path.join( 67 get_build_output_path(productform), 68 "module_list_files", 69 origin_part_name) 70 if os.path.exists(default_part_path): 71 part_path_list.append(default_part_path) 72 73 if build_out_dir != ".": 74 product_part_path = os.path.join( 75 get_build_output_path(productform), 76 build_out_dir, 77 "module_list_files", 78 origin_part_name) 79 if os.path.exists(product_part_path): 80 part_path_list.append(product_part_path) 81 part_path_dic[part_name] = part_path_list 82 return part_path_dic 83 84 def _get_target_list_from_path(self, typelist, check_path): 85 target_list = [] 86 if os.path.exists(check_path): 87 mlf_file_list = get_file_list_by_postfix( 88 check_path, ".mlf") 89 for filepath in mlf_file_list: 90 mlf_info_list = self._get_mlf_data_from_file(filepath) 91 for data in mlf_info_list: 92 test_type = data.get("test_type") 93 target_path = data.get("label") 94 if "ALL" in typelist: 95 target_list.append(target_path) 96 continue 97 if test_type in typelist: 98 target_list.append(target_path) 99 return target_list 100 101 def _get_target_list_by_type(self, productform, typelist): 102 target_list = [] 103 part_path_dic = self._get_part_path_data(productform) 104 for item in part_path_dic: 105 part_path_list = part_path_dic.get(item) 106 for part_path in part_path_list: 107 print("part_path = %s" % part_path) 108 temp_list = self._get_target_list_from_path(typelist, 109 part_path) 110 target_list.extend(temp_list) 111 return target_list 112 113 def _get_target_list_by_part(self, productform, typelist, partlist): 114 target_list = [] 115 part_path_dic = self._get_part_path_data(productform) 116 for partname in partlist: 117 part_path_list = part_path_dic.get(partname, []) 118 for part_path in part_path_list: 119 temp_list = self._get_target_list_from_path(typelist, 120 part_path) 121 target_list.extend(temp_list) 122 return target_list 123 124 def _get_target_list_by_module(self, productform, typelist, partlist, 125 testmodule): 126 target_list = [] 127 part_path_dic = self._get_part_path_data(productform) 128 for partname in partlist: 129 part_path_list = part_path_dic.get(partname, []) 130 for part_path in part_path_list: 131 module_path = os.path.join(part_path, testmodule) 132 LOG.info("module_path = %s." % module_path) 133 if os.path.exists(module_path): 134 temp_list = self._get_target_list_from_path(typelist, 135 module_path) 136 target_list.extend(temp_list) 137 return target_list 138 139 def get_build_targets(self, productform, typelist, partlist, testmodule): 140 target_list = [] 141 142 if productform == "" or len(typelist) == 0: 143 LOG.warning("Error: productform or typelist is empty.") 144 return [] 145 146 if len(partlist) == 0 and testmodule != "": 147 LOG.warning( 148 "The part cannot be empty When the module is not empty.") 149 return [] 150 151 if len(partlist) == 0 and testmodule == "": 152 target_list = self._get_target_list_by_type(productform, typelist) 153 return target_list 154 155 if len(partlist) != 0 and testmodule == "": 156 target_list = self._get_target_list_by_part(productform, typelist, 157 partlist) 158 return target_list 159 160 if len(partlist) != 0 and testmodule != "": 161 target_list = self._get_target_list_by_module(productform, 162 typelist, 163 partlist, 164 testmodule) 165 166 return target_list 167 168 def filter_build_targets(self, para): 169 productform = para.productform 170 typelist = para.testtype 171 partlist = para.partname_list 172 testmodule = para.testmodule 173 174 print("partlist = %s" % str(partlist)) 175 target_list = self.get_build_targets(productform, typelist, 176 partlist, testmodule) 177 return target_list 178 179 180############################################################################## 181############################################################################## 182