• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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):
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
34        if self._xts_root_dir.endswith('acts'):
35            # 测试套件仓修改,只查看当前编译套件仓
36            self.xts_manager = XTSManager(self._xts_root_dir, self._code_root_dir)
37            # 部件仓修改
38            self.com_manager = ComponentManager(self._xts_root_dir, self._code_root_dir)
39            # 白名单计算
40            self.wlist_manager = WhitelistManager(self._xts_root_dir, self._code_root_dir)
41            # 原精准方案兜底计算
42            self.old_manager = OldPreciseManager(self._xts_root_dir, self._code_root_dir)
43            # interface 仓
44            self.get_interface_data = GetInterfaceData(self._xts_root_dir, self._code_root_dir)
45
46            self.util_list = [
47                self.xts_manager,
48                self.get_interface_data,
49                self.com_manager,
50                self.wlist_manager,
51                self.old_manager
52            ]
53        elif self._xts_root_dir.endswith('hats'):
54            # 测试套件仓修改,只查看当前编译套件仓
55            self.xts_manager = XTSManager(self._xts_root_dir, self._code_root_dir)
56            # 原精准方案兜底计算
57            self.old_manager = OldPreciseManager(self._xts_root_dir, self._code_root_dir)
58            self.util_list = [
59                self.xts_manager,
60                self.old_manager
61            ]
62        else:  # dcts/hits
63            # 测试套件仓修改,只查看当前编译套件仓
64            self.xts_manager = XTSManager(self._xts_root_dir, self._code_root_dir)
65            # 部件仓修改
66            self.com_manager = ComponentManager(self._xts_root_dir, self._code_root_dir)
67            # 原精准方案兜底计算
68            self.old_manager = OldPreciseManager(self._xts_root_dir, self._code_root_dir)
69
70            self.util_list = [
71                self.xts_manager,
72                self.com_manager,
73                self.old_manager
74            ]
75
76    # def _get_full_target(self, xts_suitename):
77
78    def _get_change_info(self):
79        try:
80            with open(self._change_info_file, 'r') as file:
81                # 读取文件内容并解析为Python字典
82                data = json.load(file)
83        except Exception as e:
84            print(f"读取change_info_file文件失败,全量编译\nchange_info_file路径: {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(f"读取change_info_file文件失败,未找到修改文件")
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("未获取到修改文件列表,编译全量代码")
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(f"{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 = set()
132            uncompile_suite_list = MatchConfig.get_uncompile_suite_list(self._xts_root_dir)
133            print(f'配置未参与编译用例: {uncompile_suite_list}')
134            for path_target in targets:
135                if path_target not in uncompile_suite_list:
136                    ci_target.add(path_target)
137            print(f'精准编译目标: {ci_target}')
138
139        return 0, list(ci_target)
140
141
142def generate(xts_root_dir, change_info_file, build_target):
143    print("{}:{}: build_target={}".format(__file__, sys._getframe().f_lineno, build_target))
144    if not os.path.exists(change_info_file):
145        print("warning: {} not exist".format(change_info_file))
146        return 0, build_target
147
148    obj = AccurateTarget(xts_root_dir, change_info_file)
149    ret, accurate_targets = obj.get_targets()
150    return ret, accurate_targets
151