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