1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2024 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 json
20import os
21import subprocess
22import sys
23
24
25def get_subsystem_config(part_str, developer_path):
26    all_system_info_path = os.path.join(
27        developer_path, "local_coverage/all_subsystem_config.json"
28    )
29    if os.path.exists(all_system_info_path):
30        new_json_text = {}
31        with open(all_system_info_path, "r", encoding="utf-8") as system_text:
32            system_text_json = json.load(system_text)
33            if part_str in system_text_json:
34                new_json_text[part_str] = system_text_json[part_str]
35            else:
36                print(f"Error: {part_str} not in all_subsystem_config.json")
37        return new_json_text
38    else:
39        print(f"{all_system_info_path} not exists!")
40        return {}
41
42
43def get_system_or_vendor(code_path):
44    repo_config_path = os.path.join(code_path, ".repo/manifests.git/.repo_config.json")
45    if os.path.exists(repo_config_path):
46        with open(repo_config_path, "r", encoding="utf-8") as fp:
47            text_json = json.load(fp)
48            if "manifest.filename" in text_json:
49                text = text_json["manifest.filename"][0]
50                if text.startswith("system"):
51                    return "system"
52                if text.startswith("vendor"):
53                    return "vendor"
54            else:
55                return "blue"
56    else:
57        print(f"Error: {repo_config_path} not exist!")
58        return "Error"
59
60
61def get_bundle_json(part_str, developer_path, code_path):
62    part_json = get_subsystem_config(part_str, developer_path)
63    system_or_vendor = get_system_or_vendor(code_path)
64    if system_or_vendor == "system":
65        command = ["./build_system.sh", "--abi-type", "generic_generic_arm_64only", "--device-type",
66                   "general_all_phone_standard", "--ccache", "--build-variant", "root"]
67    elif system_or_vendor == "vendor":
68        command = ["./build_vendor.sh", "--abi-type", "generic_generic_arm_64only", "--device-type",
69                   "general_8425L_phone_standard", "--ccache", "--build-variant", "root",
70                   "--gn-args", "uefi_enable=true", "--gn-args", "USE_HM_KERNEL=true"]
71    elif system_or_vendor == "blue":
72        command = ["./build.sh", "--product-name", "rk3568", "--ccache"]
73    else:
74        return False
75
76    if part_json.get(part_str):
77        bundle_json_path = os.path.join(code_path, part_json.get(part_str, {}).get("path", [""])[0], "bundle.json")
78        if os.path.exists(bundle_json_path):
79            with open(bundle_json_path, "r", encoding="utf-8") as bundle_json_text:
80                bundle_json = json.load(bundle_json_text)
81            os.chdir(code_path)
82            part_name = bundle_json["component"]["name"]
83            command.append("--build-target")
84            command.append(part_name)
85
86            if bundle_json["component"]["build"].get("test"):
87                test_path = bundle_json["component"]["build"]["test"]
88                test_str = " ".join([i.strip("//") for i in test_path])
89                command.append("--build-target")
90                command.append(test_str)
91
92            command.append("--gn-args")
93            command.append("use_clang_coverage=true")
94            print(command)
95            if subprocess.call(command) == 0:
96                build_result = True
97            else:
98                build_result = False
99            os.chdir(developer_path)
100            return build_result
101
102        else:
103            print(f"{bundle_json_path}不存在,不能获取编译参数,请检查该部件的bundle.json文件!")
104            return False
105
106
107def execute_case(developer_test, part_name):
108    start_path = os.path.join(developer_test, "start.sh")
109    run_cmd = f"run -t UT -tp {part_name} -cov coverage \n"
110    print(run_cmd)
111    with os.popen(start_path, "w") as finput:
112        finput.write("1\n")
113        finput.write(run_cmd)
114        finput.write("quit\n")
115        finput.write("exit(0)\n")
116
117
118if __name__ == '__main__':
119    test_part_str = sys.argv[1]
120    current_path = os.getcwd()
121    root_path = current_path.split("/test/testfwk/developer_test")[0]
122    developer_test_path = os.path.join(root_path, "test/testfwk/developer_test")
123    build_before_path = os.path.join(
124        developer_test_path,
125        "local_coverage/restore_comment/build_before_generate.py"
126    )
127    subprocess.run("python3 %s %s" % (build_before_path, test_part_str), shell=True)
128    build_success = get_bundle_json(test_part_str, developer_test_path, root_path)
129    if build_success:
130        execute_case(developer_test_path, test_part_str)
131