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 json 22import stat 23import sys 24 25FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL 26MODES = stat.S_IWUSR | stat.S_IRUSR 27 28 29def get_source_file_list(path): 30 """ 31 获取path路径下源文件路径列表 32 """ 33 file_path_list = [] 34 file_path_list_append = file_path_list.append 35 for root, dirs, files in os.walk(path): 36 for file_name in files: 37 file_path = os.path.join(root, file_name) 38 _, suffix = os.path.splitext(file_name) 39 if suffix in [".c", ".cpp"]: 40 file_path_list_append(file_path) 41 return file_path_list 42 43 44def rewrite_source_file(source_path_list: list): 45 """ 46 源文件加“//LCOV_EXCL_BR_LINE” 47 """ 48 keys = ["if", "while", "switch", "case", "try", "catch"] 49 if not source_path_list: 50 print("no any source file here") 51 return 52 53 print("[********** Start Rewrite Source File **********]") 54 for path in source_path_list: 55 if not os.path.exists(path) or "test" in path: 56 continue 57 with open(path, "r", encoding="utf-8", errors="ignore") as read_fp: 58 code_lines = read_fp.readlines() 59 source_dir, suffix_name = os.path.splitext(path) 60 if os.path.exists(f"{source_dir}_bk{suffix_name}"): 61 os.remove(f"{source_dir}_bk{suffix_name}") 62 with os.fdopen(os.open(f"{source_dir}_bk{suffix_name}", FLAGS, MODES), 'w') as write_fp: 63 for line in code_lines: 64 sign_number = 0 65 for key in keys: 66 sign_number += 1 67 if key in line and line.strip().startswith(key): 68 write_fp.write(line) 69 break 70 elif " //LCOV_EXCL_BR_LINE" not in line and not line.strip().endswith("\\") \ 71 and sign_number == len(keys): 72 write_fp.write("%s //LCOV_EXCL_BR_LINE" % line.strip("\n").strip("\n\r")) 73 write_fp.write("\n") 74 break 75 elif sign_number == len(keys): 76 write_fp.write(line) 77 break 78 79 os.remove(path) 80 subprocess.Popen("mv %s %s" % (f"{source_dir}_bk{suffix_name}", path), 81 shell=True).communicate() 82 print("[********** End Rewrite Source File **********]") 83 84 85def add_lcov(subsystem_config_path): 86 try: 87 with open(subsystem_config_path, "r", encoding="utf-8", errors="ignore") as fp: 88 data_dict = json.load(fp) 89 for key, value in data_dict.items(): 90 if "path" in value.keys(): 91 for path_str in value["path"]: 92 file_path = os.path.join(root_path, path_str) 93 if os.path.exists(file_path): 94 source_file_path = get_source_file_list(file_path) 95 rewrite_source_file(source_file_path) 96 else: 97 print("The directory does not exist.", file_path) 98 except(FileNotFoundError, AttributeError, ValueError, KeyError): 99 print("add LCOV_EXCL_BR_LINE Error") 100 101 102def get_part_config_json(part_list, system_info_path, part_path): 103 if os.path.exists(system_info_path): 104 new_json_text = {} 105 for part in part_list: 106 with open(system_info_path, "r") as system_text: 107 system_text_json = json.load(system_text) 108 if part in system_text_json: 109 new_json_text[part] = system_text_json[part] 110 else: 111 print("Error: part not in all_subsystem_config.json") 112 new_json = json.dumps(new_json_text, indent=4) 113 if os.path.exists(part_path): 114 os.remove(part_path) 115 with os.fdopen(os.open(part_path, FLAGS, MODES), 'w') as out_file: 116 out_file.write(new_json) 117 else: 118 print("%s not exists.", system_info_path) 119 120 121if __name__ == '__main__': 122 part_name_list = [] 123 if len(sys.argv) == 2: 124 part_name_list.append(sys.argv[1]) 125 print(part_name_list) 126 else: 127 while True: 128 print("For example: run -tp partname\n" 129 " run -tp partname1 partname2") 130 131 # 获取用户输入命令 132 part_name = input("Please enter your command: ") 133 if part_name == "": 134 continue 135 if " -tp " in part_name: 136 part_name_list = part_name.strip().split(" -tp ")[1].split() 137 break 138 else: 139 continue 140 141 current_path = os.getcwd() 142 root_path = current_path.split("/test/testfwk/developer_test")[0] 143 all_system_info_path = os.path.join( 144 root_path, 145 "test/testfwk/developer_test/local_coverage/all_subsystem_config.json") 146 part_info_path = os.path.join( 147 root_path, 148 "test/testfwk/developer_test/local_coverage/restore_comment/part_config.json") 149 150 # 获取要修改的源代码的部件信息 151 get_part_config_json(part_name_list, all_system_info_path, part_info_path) 152 153 # 执行修改 154 add_lcov(part_info_path) 155