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