1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# Copyright (c) 2023 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import filecmp 17import json 18import os 19from collections import OrderedDict 20import openpyxl as op 21from coreImpl.parser.parser import parser_include_ast 22from coreImpl.diff.diff_processor_node import judgment_entrance 23from typedef.diff.diff import OutputJson 24 25global_old_dir = '' 26global_new_dir = '' 27diff_info_list = [] 28 29 30def start_diff_file(old_dir, new_dir): 31 result_info_list = global_assignment(old_dir, new_dir) 32 generate_excel(result_info_list) 33 result_json = result_to_json(result_info_list) 34 write_in_txt(result_json, r'./ndk_diff.txt') 35 print(result_json) 36 37 38def generate_excel(result_info_list): 39 data = [] 40 for diff_info in result_info_list: 41 info_data = [] 42 info_data.append(diff_info.api_name) 43 info_data.append(diff_info.api_line) 44 info_data.append(diff_info.api_column) 45 info_data.append(diff_info.api_file_path) 46 info_data.append(diff_info.api_type) 47 info_data.append(diff_info.diff_type.name) 48 info_data.append(diff_info.diff_message) 49 info_data.append(diff_info.old_api_full_text) 50 info_data.append(diff_info.new_api_full_text) 51 result = '是' if diff_info.is_compatible else '否' 52 info_data.append(result) 53 data.append(info_data) 54 wb = op.Workbook() 55 ws = wb['Sheet'] 56 ws.append(['api名称', '所在行', '所在列', '所在文件', '节点类型', 57 '变更类型', '变更信息', '旧版节点内容', '新版节点内容', '兼容']) 58 for i in range(len(data)): 59 d = data[i][0], data[i][1], data[i][2], data[i][3], data[i][4],\ 60 data[i][5], data[i][6], data[i][7], data[i][8], data[i][9] 61 ws.append(d) 62 wb.save('diff.xlsx') 63 64 65def global_assignment(old_dir, new_dir): 66 global diff_info_list 67 diff_info_list = [] 68 global global_old_dir 69 global_old_dir = old_dir 70 global global_new_dir 71 global_new_dir = new_dir 72 do_diff(old_dir, new_dir) 73 return diff_info_list 74 75 76def result_to_json(result_info_list): 77 result_json = [] 78 for diff_info in result_info_list: 79 result_json.append(OutputJson(diff_info)) 80 return json.dumps(result_json, default=lambda obj: obj.__dict__, indent=4) 81 82 83def write_in_txt(check_result, output_path): 84 with open(output_path, 'w', encoding='utf-8') as fs: 85 fs.write(check_result) 86 fs.close() 87 88 89def do_diff(old_dir, new_dir): 90 old_file_list = os.listdir(old_dir) 91 new_file_list = os.listdir(new_dir) 92 diff_list(old_file_list, new_file_list, old_dir, new_dir) 93 94 95def get_file_ext(file_name): 96 return os.path.splitext(file_name)[1] 97 98 99def diff_list(old_file_list, new_file_list, old_dir, new_dir): 100 all_list = set(old_file_list + new_file_list) 101 if len(all_list) == 0: 102 return 103 for target_file in all_list: 104 if (get_file_ext(target_file) != '.h' 105 and get_file_ext(target_file) != ''): 106 continue 107 if (target_file in old_file_list 108 and target_file not in new_file_list): 109 diff_file_path = os.path.join(old_dir, target_file) 110 del_old_file(diff_file_path) 111 if (target_file in new_file_list 112 and target_file not in old_file_list): 113 diff_file_path = os.path.join(new_dir, target_file) 114 add_new_file(diff_file_path) 115 get_same_file_diff(target_file, old_file_list, new_file_list, old_dir, new_dir) 116 117 118def add_new_file(diff_file_path): 119 if os.path.isdir(diff_file_path): 120 add_file(diff_file_path) 121 else: 122 result_map = parse_file_result(parser_include_ast(global_new_dir, [diff_file_path], flag=1)) 123 for new_info in result_map.values(): 124 diff_info_list.extend(judgment_entrance(None, new_info)) 125 126 127def del_old_file(diff_file_path): 128 if os.path.isdir(diff_file_path): 129 del_file(diff_file_path) 130 else: 131 result_map = parse_file_result(parser_include_ast(global_old_dir, [diff_file_path], flag=0)) 132 for old_info in result_map.values(): 133 diff_info_list.extend(judgment_entrance(old_info, None)) 134 135 136def get_same_file_diff(target_file, old_file_list, new_file_list, old_dir, new_dir): 137 if (target_file in old_file_list 138 and target_file in new_file_list): 139 if (os.path.isdir(os.path.join(old_dir, target_file)) 140 and os.path.isdir(os.path.join(new_dir, target_file))): 141 old_child_dir = os.path.join(old_dir, target_file) 142 new_child_dir = os.path.join(new_dir, target_file) 143 do_diff(old_child_dir, new_child_dir) 144 if (os.path.isfile(os.path.join(old_dir, target_file)) 145 and os.path.isfile(os.path.join(new_dir, target_file))): 146 old_target_file = os.path.join(old_dir, target_file) 147 new_target_file = os.path.join(new_dir, target_file) 148 if not filecmp.cmp(old_target_file, new_target_file): 149 get_file_result_diff(old_target_file, new_target_file) 150 151 152def get_file_result_diff(old_target_file, new_target_file): 153 old_file_result_map = parse_file_result(parser_include_ast(global_old_dir, [old_target_file], flag=0)) 154 new_file_result_map = parse_file_result(parser_include_ast(global_new_dir, [new_target_file], flag=1)) 155 merged_dict = OrderedDict(list(old_file_result_map.items()) + list(new_file_result_map.items())) 156 all_key_list = merged_dict.keys() 157 for key in all_key_list: 158 diff_info_list.extend(judgment_entrance(old_file_result_map.get(key), new_file_result_map.get(key))) 159 160 161def del_file(dir_path): 162 file_list = os.listdir(dir_path) 163 for i in file_list: 164 if get_file_ext(i) != '.h' and get_file_ext(i) != '': 165 continue 166 file_path = os.path.join(dir_path, i) 167 if os.path.isdir(file_path): 168 del_file(file_path) 169 if get_file_ext(i) == '.h': 170 result_map = parse_file_result(parser_include_ast(global_old_dir, [file_path], flag=0)) 171 for old_info in result_map.values(): 172 diff_info_list.extend(judgment_entrance(old_info, None)) 173 174 175def add_file(dir_path): 176 file_list = os.listdir(dir_path) 177 for i in file_list: 178 if get_file_ext(i) != '.h' and get_file_ext(i) != '': 179 continue 180 file_path = os.path.join(dir_path, i) 181 if os.path.isdir(file_path): 182 add_file(file_path) 183 if get_file_ext(i) == '.h': 184 result_map = parse_file_result(parser_include_ast(global_new_dir, [file_path], flag=1)) 185 for new_info in result_map.values(): 186 diff_info_list.extend(judgment_entrance(None, new_info)) 187 188 189def parse_file_result(result): 190 result_map = {} 191 for root_node in result: 192 children_list = root_node['children'] 193 for children in children_list: 194 if children["name"] == '': 195 continue 196 result_map.setdefault(f'{children["name"]}-{children["kind"]}', children) 197 del root_node['children'] 198 result_map.setdefault(f'{root_node["name"]}-{root_node["kind"]}', root_node) 199 return result_map 200