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