1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2020-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# 18import json 19import os 20import subprocess 21import stat 22 23FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL 24MODES = stat.S_IWUSR | stat.S_IRUSR 25 26 27def get_source_file_list(path): 28 """ 29 获取path路径下源文件路径列表 30 """ 31 file_path_list = [] 32 file_path_list_append = file_path_list.append 33 for root, dirs, files in os.walk(path): 34 for file_name in files: 35 file_path = os.path.join(root, file_name) 36 _, suffix = os.path.splitext(file_name) 37 if suffix in [".c", ".cpp", ".html"]: 38 file_path_list_append(file_path) 39 return file_path_list 40 41 42def recover_source_file(source_path_list, keys): 43 if not source_path_list: 44 print("no any source file here") 45 return 46 47 for path in source_path_list: 48 source_dir, suffix_name = os.path.splitext(path) 49 if not os.path.exists(path): 50 continue 51 if suffix_name != ".html" and "test" in path: 52 continue 53 with open(path, "r", encoding="utf-8", errors="ignore") as read_fp: 54 code_lines = read_fp.readlines() 55 if os.path.exists(f"{source_dir}_bk{suffix_name}"): 56 os.remove(f"{source_dir}_bk{suffix_name}") 57 with os.fdopen(os.open(f"{source_dir}_bk{suffix_name}", FLAGS, MODES), 'w') as write_fp: 58 for line in code_lines: 59 for key in keys: 60 if key in line: 61 write_fp.write(line.replace(key, "")) 62 else: 63 write_fp.write(line) 64 65 os.remove(path) 66 subprocess.Popen("mv %s %s" % (f"{source_dir}_bk{suffix_name}", path), 67 shell=True).communicate() 68 69 70def recover_cpp_file(part_name_path): 71 try: 72 if os.path.exists(part_name_path): 73 with open(part_name_path, "r", encoding="utf-8", errors="ignore") as fp: 74 data_dict = json.load(fp) 75 for key, value in data_dict.items(): 76 if "path" in value.keys(): 77 for path_str in value["path"]: 78 file_path = os.path.join(root_path, path_str) 79 if os.path.exists(file_path): 80 cpp_list = get_source_file_list(file_path) 81 recover_source_file(cpp_list, keys=[" //LCOV_EXCL_BR_LINE"]) 82 else: 83 print("The directory does not exist.", file_path) 84 os.remove(part_name_path) 85 except(FileNotFoundError, AttributeError, ValueError, KeyError): 86 print("recover LCOV_EXCL_BR_LINE Error") 87 88 89if __name__ == '__main__': 90 current_path = os.getcwd() 91 root_path = current_path.split("/test/testfwk/developer_test")[0] 92 html_path = os.path.join( 93 root_path, 94 "test/testfwk/developer_test/local_coverage/code_coverage/results/coverage/reports/cxx/html") 95 html_arr_list = get_source_file_list(html_path) 96 97 print("[************* start Recover Source File *************]") 98 recover_source_file(html_arr_list, keys=[" //LCOV_EXCL_BR_LINE"]) 99 100 part_info_path = os.path.join( 101 root_path, 102 "test/testfwk/developer_test/local_coverage/restore_comment/part_config.json") 103 recover_cpp_file(part_info_path) 104 105 print("[************** End Recover Source File **************]") 106