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