• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 json
17import os.path
18import openpyxl as op
19from pathlib import Path
20from typedef.check.check import FileDocInfo, check_command_message, CheckErrorMessage
21from typedef.check.check_compatibility import check_compatibility_command_message
22from coreImpl.check.check_doc import process_comment, process_file_doc_info
23from coreImpl.check.check_name import check_file_name, check_api_name
24from coreImpl.parser.parser import parser_include_ast
25from coreImpl.check.check_compatibility import check_compatibility_entrance
26
27
28def process_api_json(api_info, file_doc_info, api_result_info_list, parent_kind, command_list):
29    for command in command_list:
30        # 对非文件名校验
31        if 'NAME' in command and CheckErrorMessage.API_NAME_UNIVERSAL_14.name != command:
32            api_result_info_list.extend(check_api_name(api_info, parent_kind))
33            break
34    if 'comment' in api_info.keys():
35        comment = api_info['comment']
36        api_result_info_list.extend(process_comment(comment, file_doc_info, api_info))
37    kind = api_info['kind']
38    child_node_list = get_api_info_child(api_info)
39    for child_node in child_node_list:
40        process_api_json(child_node, file_doc_info, api_result_info_list, kind, command_list)
41
42
43def get_api_info_child(api_info):
44    if 'children' in api_info.keys():
45        return api_info['children']
46    if 'members' in api_info.keys():
47        return api_info['members']
48    if 'parm' in api_info.keys():
49        return api_info['parm']
50    return []
51
52
53def process_file_json(file_info, api_result_info_list, command_list):
54    # 校验文件名
55    if CheckErrorMessage.API_NAME_UNIVERSAL_14.name in command_list:
56        api_result_info_list.extend(check_file_name(file_info))
57    apis = file_info['children']
58    kind = file_info['kind']
59    file_doc_info = FileDocInfo()
60    # 校验Doc信息
61    api_result_info_list.extend(process_comment(file_info["comment"], file_doc_info, file_info))
62    for api in apis:
63        process_api_json(api, file_doc_info, api_result_info_list, kind, command_list)
64    api_result_info_list.extend(process_file_doc_info(file_doc_info, file_info, -1))
65
66
67def process_all_json(python_obj, command_list):
68    api_result_info_list = []
69    for file_info in python_obj:
70        process_file_json(file_info, api_result_info_list, command_list)
71    return api_result_info_list
72
73
74def collect_node_api_change(check_result):
75    check_result_data = []
76    for api_check_info in check_result:
77        info_data = [
78            api_check_info.analyzerName,
79            api_check_info.buggyFilePath,
80            api_check_info.codeContextStartLine,
81            api_check_info.defectLevel,
82            api_check_info.defectType,
83            api_check_info.description,
84            api_check_info.language,
85            api_check_info.mainBuggyCode,
86            api_check_info.mainBuggyLine
87        ]
88        check_result_data.append(info_data)
89
90    return check_result_data
91
92
93def generate_excel(check_result_list, output_path):
94    data = collect_node_api_change(check_result_list)
95    wb = op.Workbook()
96    ws = wb['Sheet']
97    ws.title = 'check信息'
98    ws.append(['工具名', '文件路径', '错误注释内容开始行', '等级',
99               '错误类型', '错误信息描述', '语言', '节点内容', '错误行'])
100    for title in data:
101        d = title[0], title[1], title[2], title[3], title[4],\
102            title[5], title[6], title[7], title[8]
103        ws.append(d)
104    out_path = os.path.dirname(output_path)
105    output_path_xlsx = os.path.abspath(os.path.join(out_path, 'check_result.xlsx'))
106    wb.save(output_path_xlsx)
107
108
109def write_in_txt(check_result, output_path):
110    result_json = result_to_json(check_result)
111    with open(output_path, 'w', encoding='utf-8') as fs:
112        fs.write(result_json)
113        fs.close()
114
115
116def result_to_json(check_result):
117    return json.dumps(check_result, default=lambda obj: obj.__dict__, indent=4, ensure_ascii=False)
118
119
120def get_file_path(txt_file):    # 路径装在txt文件用的--获取.h文件路径
121    include_path = []
122    with open(txt_file, 'r', encoding='utf-8') as fd:
123        for line in fd:
124            include_path.append(line.replace('\n', ''))
125    return include_path
126
127
128def curr_entry(files_path, command: str, output):
129    file_list = get_files(files_path)
130    # 如果要校验的路径下没有interface_sdk_c文件夹,不做检测
131    if 'interface_sdk_c' not in file_list[0].split(os.path.sep):
132        return
133    root_path = os.path.join(file_list[0].split('interface_sdk_c')[0], 'interface_sdk_c')
134    # 如果根目录下没有 third_party/musl 文件夹,则证明不是 sdk_c 仓库,不做检测
135    if not os.path.exists(os.path.abspath(os.path.join(root_path, 'third_party/musl'))):
136        return
137    if command == 'all':
138        command_list = check_command_message
139        command_of_compatibility_list = check_compatibility_command_message
140    else:
141        command_list = command.split(',')
142        command_of_compatibility_list = command_list
143    check_result_list = []
144    if len(file_list) > 0:
145        check_result_list = get_check_result_list(file_list, root_path, command_list)
146    result_list = []
147    if command != 'all':
148        for result in check_result_list:
149            if result.defectType in command_list:
150                result_list.append(result)
151    else:
152        result_list = check_result_list
153    old_dir = r''
154    new_dir = r''
155    if old_dir and new_dir:
156        compatibility_data = check_compatibility_entrance(old_dir, new_dir, command_of_compatibility_list)
157        result_list.extend(compatibility_data)
158    write_in_txt(result_list, output)
159
160
161def get_check_result_list(file_list, root_path, command_list):
162    check_result_list = []
163    python_obj = parser_include_ast(root_path, file_list)
164    check_result_list.extend(process_all_json(python_obj, command_list))
165    return check_result_list
166
167
168def get_files(files_path: str):
169    return files_path.split(',')
170
171
172def get_file_type(file_path):
173    return Path(file_path).suffix
174