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# 18 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_file_list(find_path, postfix=""): 28 file_names = os.listdir(find_path) 29 file_list = [] 30 if len(file_names) > 0: 31 for fn in file_names: 32 if postfix != "": 33 if fn.find(postfix) != -1 and fn[-len(postfix):] == postfix: 34 file_list.append(fn) 35 else: 36 file_list.append(fn) 37 return file_list 38 39 40def get_file_list_by_postfix(path, postfix=""): 41 file_list = [] 42 for dirs in os.walk(path): 43 files = get_file_list(find_path=dirs[0], postfix=postfix) 44 for file_name in files: 45 if "" != file_name and -1 == file_name.find(__file__): 46 file_name = os.path.join(dirs[0], file_name) 47 if os.path.isfile(file_name): 48 file_list.append(file_name) 49 return file_list 50 51 52def recover_source_file(cpp_list, keys): 53 if not cpp_list: 54 print("no any .cpp file here") 55 return 56 57 for path in cpp_list: 58 if not os.path.exists(path): 59 return 60 for key in keys: 61 with open(path, "r") as read_fp: 62 code_lines = read_fp.readlines() 63 if os.path.exists(f"{path.split('.')[0]}_bk.html"): 64 os.remove(f"{path.split('.')[0]}_bk.html") 65 with os.fdopen(os.open(f"{path.split('.')[0]}_bk.html", FLAGS, MODES), 'w') as write_fp: 66 for line in code_lines: 67 if key in line: 68 write_fp.write(line.strip("\n").strip("\n\r").replace(" //LCOV_EXCL_BR_LINE", "")) 69 write_fp.write("\n") 70 else: 71 write_fp.write(line) 72 73 os.remove(path) 74 subprocess.Popen("mv %s %s" % (f"{path.split('.')[0]}_bk.html", path), 75 shell=True).communicate() 76 77 78if __name__ == '__main__': 79 current_path = os.getcwd() 80 root_path = current_path.split("/test/testfwk/developer_test")[0] 81 html_path = os.path.join( 82 root_path, 83 "test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html") 84 cpp_arr_list = get_file_list_by_postfix(html_path, ".html") 85 recover_source_file(cpp_arr_list, keys=[" //LCOV_EXCL_BR_LINE"]) 86