• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# coding=utf-8
3##############################################
4# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16##############################################
17
18import json
19import os
20import openpyxl
21
22
23def compare_json_file(js_file1, js_file2):  # 获取对比结果
24    with open(js_file1, 'r', encoding='utf-8') as js1:
25        data1 = json.load(js1)
26    with open(js_file2, 'r') as js2:
27        data2 = json.load(js2)
28    compare_result = []
29    only_file1 = []  # 装file1独有的
30    result_api = filter_compare(data1)
31    for it in result_api:
32        name1 = it["name"]
33        key = 0
34        for item in data2:
35            if item["name"] == name1:
36                key = 1
37                compare_result.append(it)
38                break
39        if key == 0:
40            only_file1.append(it)
41    only_file2 = get_difference_data(compare_result, data2)  # 获取file2独有的
42    js1.close()
43    js2.close()
44    return compare_result, only_file1, only_file2
45
46
47def get_difference_data(compare_result, data2):
48    only_file2 = []
49    for item in data2:
50        name2 = item["name"]
51        key = 0
52        for it in compare_result:
53            name1 = it["name"]
54            if name2 == name1:
55                key = 1
56                break
57        if key == 0:
58            only_file2.append(item)
59    return only_file2
60
61
62def filter_compare(data1):  # 获取函数和变量
63    result_api = []
64    for it in data1:
65        get_result_api(it, result_api)
66    return result_api
67
68
69def get_result_api(file_data, result_api):
70    if 'children' in file_data:
71        for item1 in file_data["children"]:  # 抛开根节点
72            if (item1["kind"] == 'FUNCTION_DECL' or item1["kind"] == 'VAR_DECL') and item1["is_extern"]:
73                item = filter_func(item1)
74                result_api.append(item)
75
76
77def get_parm(item, parm):
78    if item["parm"]:
79        for i in range(len(item["parm"])):
80            if item["parm"][i]["kind"] != 'PARM_DECL':
81                continue
82            else:
83                str_parm = '{} {}'.format(item["parm"][i]["type"], item["parm"][i]["name"])
84                parm.append(str_parm)
85        item["parm"] = parm
86
87
88def filter_func(item):
89    del item["is_extern"]  # 剔除is_extern键值对,过滤后都是extern
90    del item["comment"]
91    if "type_ref" in list(item.keys()):
92        del item["type_ref"]
93    if "children" in list(item.keys()):
94        del item["children"]
95
96    item["location_path"] = item["location"]["location_path"]
97    item["location"] = item["location"]["location_line"]
98    if item["kind"] == 'FUNCTION_DECL':
99        item["kind"] = '函数类型'
100        parm = []  # 装函数参数
101        if "parm" in item:
102            get_parm(item, parm)
103    else:
104        item["kind"] = '变量类型'
105        del item["is_const"]
106    return item
107
108
109def generate_excel(array, name, only_file1, only_file2):
110    # 将列名换为中文名
111    columns_map = {
112        'name': '名称',
113        'kind': '节点类型',
114        'type': '类型',
115        'gn_path': 'gn文件路径',
116        "node_content": '节点内容',
117        'location': '位置行',
118        'return_type': '返回类型',
119        'parm': '参数',
120        'location_path': '文件相对路径',
121    }
122
123    workbook = openpyxl.Workbook()
124    work_sheet1 = workbook.active
125    work_sheet1.title = '对比结果'
126    write_dict_to_worksheet(work_sheet1, array, header=columns_map)
127
128    work_sheet2 = workbook.create_sheet('生成json独有')
129    write_dict_to_worksheet(work_sheet2, only_file1, header=columns_map)
130
131    work_sheet3 = workbook.create_sheet('原有json独有')
132    write_dict_to_worksheet(work_sheet3, only_file2)
133    workbook.save(name)
134
135
136def write_dict_to_worksheet(work_sheet, result_data, header=None):
137    if header is None:
138        header = {
139            'name': '名称'
140        }
141    row_num = 1
142    for col_num, col_value in enumerate(header.values()):
143        work_sheet.cell(row_num, col_num + 1, col_value)
144
145    row_num = 2
146    for data in result_data:
147        for col_num, col_value in enumerate(data.values()):
148            processing_worksheet_data(work_sheet, col_value, row_num, col_num)
149        row_num += 1
150
151
152def processing_worksheet_data(work_sheet, col_value, row_num, col_num):
153    if isinstance(col_value, dict):
154        param_data = []
155        for dict_value in col_value.values():
156            if isinstance(dict_value, int):
157                dict_value = str(dict_value)
158            param_data.append(dict_value)
159        param_str = ','.join(param_data)
160        work_sheet.cell(row_num, col_num + 1, param_str)
161    elif isinstance(col_value, list):
162        list_data = ','.join(col_value)
163        work_sheet.cell(row_num, col_num + 1, list_data)
164    else:
165        work_sheet.cell(row_num, col_num + 1, col_value)
166
167
168def del_repetition_value(only_file_list, compare_list):
169    data = []
170    for item in only_file_list:
171        if item not in compare_list:
172            data.append(item)
173    return data
174
175
176def get_json_file(json_file_new, json_file):  # 获取生成的json文件
177    json_file1 = r'{}'.format(json_file_new)  # 获取要对比的json文件
178    json_file2 = json_file
179    head_name = os.path.splitext(json_file1)  # 去掉文件名后缀
180    head_name = head_name[0] + '.xlsx'  # 加后缀
181    result_list = []
182    only_file1 = []
183    only_file2 = []
184    for item in json_file2:  # 对比每一个json(目录下的)
185        # 对比两个json文件
186        result_list_part, only_file1_part, only_file2_part = compare_json_file(json_file1, item)
187        result_list.extend(result_list_part)
188        only_file1.extend(only_file1_part)
189        only_file2.extend(only_file2_part)
190    only_file1_new = del_repetition_value(only_file1, result_list)
191    return result_list, head_name, only_file1_new, only_file2  # 返回对比数据,和所需表格名
192