1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2024 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 os 19import sys 20import json 21from Utils import ChangeFileEntity, XTSTargetUtils, PathUtils, MatchConfig, HOME 22from ci_manager import ComponentManager, XTSManager, WhitelistManager, OldPreciseManager, GetInterfaceData 23 24 25class AccurateTarget: 26 27 def __init__(self, xts_root_dir, change_info_file, device_type): 28 self._xts_root_dir = xts_root_dir 29 self._code_root_dir = os.path.realpath(os.path.join(xts_root_dir, "../../..")) 30 self._change_info_file = change_info_file 31 self._testsuite = None 32 self.util_class_list = [] 33 self._device_type = "phone" 34 if "_watch_" in device_type: 35 self._device_type = "watch" 36 elif "_pc_" in device_type: 37 self._device_type = "pc" 38 39 if self._xts_root_dir.endswith('acts'): 40 # 测试套件仓修改,只查看当前编译套件仓 41 self.xts_manager = XTSManager(self._xts_root_dir, self._code_root_dir) 42 # 部件仓修改 43 self.com_manager = ComponentManager(self._xts_root_dir, self._code_root_dir) 44 # 白名单计算 45 self.wlist_manager = WhitelistManager(self._xts_root_dir, self._code_root_dir) 46 # 原精准方案兜底计算 47 self.old_manager = OldPreciseManager(self._xts_root_dir, self._code_root_dir) 48 # interface 仓 49 self.interface_manager = GetInterfaceData(self._xts_root_dir, self._code_root_dir) 50 51 self.util_list = [ 52 self.xts_manager, 53 self.interface_manager, 54 self.com_manager, 55 self.wlist_manager, 56 self.old_manager 57 ] 58 else: # hats/dcts/hits 59 # 测试套件仓修改,只查看当前编译套件仓 60 self.xts_manager = XTSManager(self._xts_root_dir, self._code_root_dir) 61 # 部件仓修改 62 self.com_manager = ComponentManager(self._xts_root_dir, self._code_root_dir) 63 # 白名单计算 64 self.wlist_manager = WhitelistManager(self._xts_root_dir, self._code_root_dir) 65 # interface 仓 66 self.interface_manager = GetInterfaceData(self._xts_root_dir, self._code_root_dir) 67 68 self.util_list = [ 69 self.xts_manager, 70 self.interface_manager, 71 self.com_manager, 72 self.wlist_manager, 73 ] 74 75 # def _get_full_target(self, xts_suitename): 76 77 def _get_change_info(self): 78 try: 79 with open(self._change_info_file, 'r') as file: 80 # 读取文件内容并解析为Python字典 81 data = json.load(file) 82 except Exception as e: 83 print("warning: Because the change_info_file {} was not read, compile full testsuites".format( 84 self._change_info_file)) 85 return 1 86 87 # 存储新增/修改/删除的文件 88 change_list = [] 89 for item in data: 90 changeFileEntity = ChangeFileEntity(name=data[item]["name"], path=item) 91 if "added" in data[item]["changed_file_list"]: 92 changeFileEntity.addAddPaths(data[item]["changed_file_list"]["added"]) 93 if "modified" in data[item]["changed_file_list"]: 94 changeFileEntity.addModifiedPaths(data[item]["changed_file_list"]["modified"]) 95 if "rename" in data[item]["changed_file_list"]: 96 changeFileEntity.addRenamePathsto(data[item]["changed_file_list"]["rename"]) 97 if "deleted" in data[item]["changed_file_list"]: 98 changeFileEntity.addDeletePaths(data[item]["changed_file_list"]["deleted"]) 99 if changeFileEntity.isEmpty(): 100 print("warning: Failed to read change_info_file, no changed files were found.") 101 return 1 102 change_list.append(changeFileEntity) 103 self._change_list = change_list 104 return 0 105 106 def get_targets(self): 107 ret = self._get_change_info() 108 if ret == 1: 109 # changeinfo读取失败-全量编译 110 print("warning: The list of files to be modifed was not obtained, compile full testsuites.") 111 xts_suite = os.path.basename(self._xts_root_dir) 112 relative_path = os.path.relpath(self._xts_root_dir, HOME) 113 targets = [f"{relative_path}:xts_{xts_suite}"] 114 else: 115 # 处理结果 116 target_paths = set() 117 targets = set() 118 # 执行全部并获取结果 119 for manager in self.util_list: 120 retcode = manager.get_targets_from_change(self._change_list) 121 if retcode == 1: 122 print("error: Execution of {} failed.".format(manager.__class__.__name__)) 123 manager.write_result(target_paths, targets) 124 125 # 处理target_paths, 去除子目录\重复目录 126 sum_path = PathUtils.removeSubandDumpPath(list(target_paths)) 127 128 for path in sum_path: 129 targets.update(set(XTSTargetUtils.getTargetfromPath(self._xts_root_dir, path))) 130 131 ci_target = XTSTargetUtils.del_uncompile_target(self._xts_root_dir, self._device_type, targets) 132 133 return 0, ci_target 134 135 136def generate(xts_root_dir, change_info_file, build_target, device_type = "phone"): 137 print("{}:{}: build_target={}".format(__file__, sys._getframe().f_lineno, build_target)) 138 if not os.path.exists(change_info_file): 139 print("warning: {} not exist".format(change_info_file)) 140 return 0, build_target 141 142 obj = AccurateTarget(xts_root_dir, change_info_file, device_type) 143 ret, accurate_targets = obj.get_targets() 144 return ret, accurate_targets 145