1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2023 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 19 20import os 21import sys 22import json 23import shutil 24import subprocess 25from shutil import copyfile 26 27from utils import get_product_name, coverage_command 28 29 30def get_subsystem_config(part_list, developer_path): 31 all_system_info_path = os.path.join( 32 developer_path, "localCoverage/all_subsystem_config.json" 33 ) 34 system_info_path = os.path.join( 35 developer_path, "localCoverage/codeCoverage/subsystem_config.json" 36 ) 37 if os.path.exists(all_system_info_path): 38 new_json_text = {} 39 for part in part_list: 40 with open(all_system_info_path, "r", encoding="utf-8") as system_text: 41 system_text_json = json.load(system_text) 42 if part in system_text_json: 43 new_json_text[part] = system_text_json[part] 44 else: 45 print("part not in all_subsystem_config.json") 46 47 new_json = json.dumps(new_json_text, indent=4) 48 with open(system_info_path, "w") as out_file: 49 out_file.write(new_json) 50 else: 51 print("%s not exists.", all_system_info_path) 52 53 54def copy_coverage(developer_path): 55 print("*" * 40, "Start TO Get Code Coverage Report", "*" * 40) 56 coverage_path = os.path.join(developer_path, "reports/coverage") 57 code_path = os.path.join( 58 developer_path, "localCoverage/codeCoverage/results/coverage" 59 ) 60 if os.path.exists(code_path): 61 shutil.rmtree(code_path) 62 shutil.copytree(coverage_path, code_path) 63 64 65def remove_thrd_gcda(developer_path): 66 print("remove thirdparty gcda") 67 gcda_dir_path = os.path.join(developer_path, "localCoverage/codeCoverage/results/coverage/data/cxx") 68 if os.path.exists(gcda_dir_path): 69 for i in os.listdir(gcda_dir_path): 70 remove_out = os.path.join(gcda_dir_path, i, "obj/out") 71 remove_thrd = os.path.join(gcda_dir_path, i, "obj/third_party") 72 if os.path.exists(remove_out): 73 print("remove {}".format(remove_out)) 74 shutil.rmtree(remove_out) 75 if os.path.exists(remove_thrd): 76 print("remove {}".format(remove_thrd)) 77 shutil.rmtree(remove_thrd) 78 79 80def generate_coverage_rc(developer_path): 81 coverage_rc_path = os.path.join( 82 developer_path, "localCoverage/codeCoverage/coverage_rc" 83 ) 84 lcovrc_cov_template_path = os.path.join(coverage_rc_path, "lcovrc_cov_template") 85 for num in range(16): 86 tmp_cov_path = os.path.join(coverage_rc_path, f"tmp_cov_{num}") 87 lcovrc_cov_path = os.path.join(coverage_rc_path, f"lcovrc_cov_{num}") 88 if not os.path.exists(tmp_cov_path): 89 os.mkdir(tmp_cov_path) 90 if not os.path.exists(os.path.join(tmp_cov_path, "ex.txt")): 91 with open(os.path.join(tmp_cov_path, "ex.txt"), mode="w") as f: 92 f.write("") 93 94 copyfile(lcovrc_cov_template_path, lcovrc_cov_path) 95 with open(lcovrc_cov_path, mode="a") as f: 96 f.write("\n\n") 97 f.write("# Location for temporary directories\n") 98 f.write(f"lcov_tmp_dir = {tmp_cov_path}") 99 100 101def execute_code_cov_tools(developer_path): 102 llvm_gcov_path = os.path.join( 103 developer_path, "localCoverage/codeCoverage/llvm-gcov.sh" 104 ) 105 coverage_command("dos2unix %s" % llvm_gcov_path) 106 coverage_command("chmod 777 %s" % llvm_gcov_path) 107 tools_path = os.path.join( 108 developer_path, "localCoverage/codeCoverage/mutilProcess_CodeCoverage.py" 109 ) 110 coverage_command("python3 %s" % tools_path) 111 112 113def get_subsystem_name(part_list, product_name): 114 if product_name: 115 testfwk_json_path = os.path.join( 116 root_path, "out", product_name, "build_configs/infos_for_testfwk.json" 117 ) 118 if os.path.exists(testfwk_json_path): 119 with open(testfwk_json_path, "r", encoding="utf-8") as json_text: 120 system_json = json.load(json_text) 121 subsystem_info = system_json.get("phone").get("subsystem_infos") 122 subsystem_list = [] 123 for part in part_list: 124 for key in subsystem_info.keys(): 125 if part in subsystem_info.get(key) and key not in subsystem_list: 126 subsystem_list.append(key) 127 subsystem_str = ','.join(list(map(str, subsystem_list))) 128 return subsystem_str 129 else: 130 print("%s not exists.", testfwk_json_path) 131 return "" 132 else: 133 print("product_name is not null") 134 return "" 135 136 137def execute_interface_cov_tools(partname_str, developer_path): 138 print("*" * 40, "Start TO Get Interface Coverage Report", "*" * 40) 139 innerkits_json_path = os.path.join( 140 developer_path, 141 "localCoverage/interfaceCoverage/get_innerkits_json.py" 142 ) 143 coverage_command("python3 %s" % innerkits_json_path) 144 145 interface_path = os.path.join( 146 developer_path, 147 "localCoverage/interfaceCoverage/interfaceCoverage_gcov_lcov.py" 148 ) 149 subprocess.run("python3 %s %s" % (interface_path, partname_str), shell=True) 150 151 152if __name__ == '__main__': 153 testpart_args = sys.argv[1] 154 test_part_list = testpart_args.split("testpart=")[1].split(",") 155 156 current_path = os.getcwd() 157 root_path = current_path.split("/test/testfwk/developer_test")[0] 158 developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") 159 160 # 获取产品形态 161 product_names = get_product_name(root_path) 162 163 # copy gcda数据到覆盖率工具指定位置 164 copy_coverage(developer_test_path) 165 generate_coverage_rc(developer_test_path) 166 remove_thrd_gcda(developer_test_path) 167 168 # 获取部件位置信息config 169 if len(test_part_list) > 0: 170 get_subsystem_config(test_part_list, developer_test_path) 171 172 # 执行代码覆盖率 173 execute_code_cov_tools(developer_test_path) 174 # 报备 175 keyword_path = os.path.join( 176 developer_test_path, "localCoverage/keyword_registration/keyword_filter.py") 177 subprocess.run("python3 %s" % keyword_path, shell=True) 178 179 # 执行接口覆盖率 180 if len(test_part_list) > 0: 181 execute_interface_cov_tools(testpart_args, developer_test_path) 182 else: 183 print("subsystem or part without!") 184 185 # 源代码还原 186 after_lcov_branch_path = os.path.join( 187 developer_test_path, "localCoverage/restore_comment/after_lcov_branch.py") 188 if os.path.exists(after_lcov_branch_path): 189 subprocess.run("python3 %s " % after_lcov_branch_path, shell=True) 190 restore_source_code_path = os.path.join( 191 developer_test_path, "localCoverage/restore_comment/restore_source_code.py") 192 subprocess.run("python3 %s" % restore_source_code_path, shell=True) 193 194 print(r"See the code coverage report in: " 195 r"/test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") 196 print(r"See the interface coverage report in: " 197 r"/test/testfwk/developer_test/localCoverage/interfaceCoverage/results/coverage/interface_kits") 198