1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-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 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 # 获取infos_for_testfwk.json配置文件中“phone”节点下“part_infos”节点数据 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:~/OpenHarmony/out/rk3568/module_list_files/部件名(以rk3568举例) 67 default_part_path = os.path.join( 68 get_build_output_path(productform), 69 "module_list_files", 70 origin_part_name) 71 if os.path.exists(default_part_path): 72 part_path_list.append(default_part_path) 73 # 如果build_out_dir不是当前目录,将新目录加到part_path_list中 74 if build_out_dir != ".": 75 product_part_path = os.path.join( 76 get_build_output_path(productform), 77 build_out_dir, 78 "module_list_files", 79 origin_part_name) 80 if os.path.exists(product_part_path): 81 part_path_list.append(product_part_path) 82 part_path_dic[part_name] = part_path_list 83 return part_path_dic 84 85 def _get_target_list_from_path(self, typelist, check_path): 86 target_list = [] 87 if os.path.exists(check_path): 88 # 获取部件编译输出目录(~/OpenHarmony/out/rk3568/module_list_files/部件名1)中.mlf文件列表 89 mlf_file_list = get_file_list_by_postfix( 90 check_path, ".mlf") 91 # 遍历mlf_file_list中所有目录下面mlf文件中的label列表 92 for filepath in mlf_file_list: 93 # 获取mlf文件中的JSON数据信息列表 94 mlf_info_list = self._get_mlf_data_from_file(filepath) 95 for data in mlf_info_list: 96 # 举例:"test_type": "moduletest" 97 test_type = data.get("test_type") 98 # 举例:"label": "//base/accessibility/services/test:aams_accessibility_keyevent_filter_test 99 # (//build/toolchain/ohos:ohos_clang_arm)" 100 target_path = data.get("label") 101 if "ALL" in typelist: 102 target_list.append(target_path) 103 continue 104 if test_type in typelist: 105 target_list.append(target_path) 106 return target_list 107 108 def _get_target_list_by_type(self, productform, typelist): 109 target_list = [] 110 # 获取所有部件编译输出信息列表:[{“部件名1”:[~/OpenHarmony/out/rk3568/module_list_files/部件名1]}] 111 # 或者{“部件名1”:[~/OpenHarmony/out/rk3568/module_list_files/部件名1, 112 # ~/OpenHarmony/out/rk3568/编译目录build_out_dir/module_list_files/部件名1]} 113 part_path_dic = self._get_part_path_data(productform) 114 for item in part_path_dic: 115 part_path_list = part_path_dic.get(item) 116 for part_path in part_path_list: 117 print("part_path = %s" % part_path) 118 temp_list = self._get_target_list_from_path(typelist, 119 part_path) 120 target_list.extend(temp_list) 121 return target_list 122 123 def _get_target_list_by_part(self, productform, typelist, partlist): 124 target_list = [] 125 part_path_dic = self._get_part_path_data(productform) 126 for partname in partlist: 127 part_path_list = part_path_dic.get(partname, []) 128 for part_path in part_path_list: 129 temp_list = self._get_target_list_from_path(typelist, 130 part_path) 131 target_list.extend(temp_list) 132 return target_list 133 134 def _get_target_list_by_module(self, productform, typelist, partlist, 135 testmodule): 136 target_list = [] 137 part_path_dic = self._get_part_path_data(productform) 138 for partname in partlist: 139 part_path_list = part_path_dic.get(partname, []) 140 for part_path in part_path_list: 141 module_path = os.path.join(part_path, testmodule) 142 LOG.info("module_path = %s." % module_path) 143 if os.path.exists(module_path): 144 temp_list = self._get_target_list_from_path(typelist, 145 module_path) 146 target_list.extend(temp_list) 147 return target_list 148 149 def get_build_targets(self, productform, typelist, partlist, testmodule): 150 target_list = [] 151 152 if productform == "" or len(typelist) == 0: 153 LOG.warning("Error: productform or typelist is empty.") 154 return [] 155 156 if len(partlist) == 0 and testmodule != "": 157 LOG.warning( 158 "The part cannot be empty When the module is not empty.") 159 return [] 160 # productform不为空,typelist(test type[UT,MST,ST,PERF,ALL])不为空 161 # partlist和testmodule为空,通过testtype获取部件列表 162 if len(partlist) == 0 and testmodule == "": 163 target_list = self._get_target_list_by_type(productform, typelist) 164 return target_list 165 # productform不为空,typelist(test type[UT,MST,ST,PERF,ALL])不为空 166 # partlist不为空,testmodule为空,通过testtype、partlist一起获取部件列表 167 if len(partlist) != 0 and testmodule == "": 168 target_list = self._get_target_list_by_part(productform, typelist, 169 partlist) 170 return target_list 171 # productform不为空,typelist(test type[UT,MST,ST,PERF,ALL])不为空 172 # partlist不为空,testmodule不为空,通过testtype、partlist、testmodule一起获取部件列表 173 if len(partlist) != 0 and testmodule != "": 174 target_list = self._get_target_list_by_module(productform, 175 typelist, 176 partlist, 177 testmodule) 178 179 return target_list 180 181 # 通过infos_for_testfwk.json文件获取所有子部件信息编译目录信息: 182 # [{“部件名1”:[~/OpenHarmony/out/rk3568/module_list_files/部件名1]}] 183 # 然后遍历这些目录中的mlf文件,获取其中定义的label,返回label集合 184 # 遍历时通过testmodule控制遍历的部件指定模块目录,如果不定义,则遍历子部件下面所有模块目录 185 # 遍历时通过partlist控制遍历指定部件目录,如果不定义,则遍历infos_for_testfwk.json文件中定义的所有子部件目录 186 def filter_build_targets(self, para): 187 productform = para.productform 188 typelist = para.testtype 189 partlist = para.partname_list 190 testmodule = para.testmodule 191 192 print("partlist = %s" % str(partlist)) 193 target_list = self.get_build_targets(productform, typelist, 194 partlist, testmodule) 195 return target_list 196 197 198############################################################################## 199############################################################################## 200