• 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
17from pathlib import Path
18from typedef.check.check import FileDocInfo, OutputTxt
19from coreImpl.check.check_doc import process_comment, process_file_doc_info
20from coreImpl.check.check_name import check_file_name, check_ndk_name
21from coreImpl.parser.parser import parser_include_ast
22from coreImpl.check.check_syntax import check_syntax
23
24
25def process_api_json(api_info, file_doc_info, api_result_info_list):
26    api_result_info_list.extend(check_ndk_name(api_info))
27    if 'comment' in api_info.keys():
28        comment = api_info['comment']
29        api_result_info_list.extend(
30            process_comment(comment, file_doc_info, api_info))
31    child_node_list = get_api_info_child(api_info)
32    for child_node in child_node_list:
33        process_api_json(child_node, file_doc_info, api_result_info_list)
34
35
36def get_api_info_child(api_info):
37    if 'children' in api_info.keys():
38        return api_info['children']
39    if 'members' in api_info.keys():
40        return api_info['members']
41    if 'parm' in api_info.keys():
42        return api_info['parm']
43    return []
44
45
46def process_file_json(file_info, api_result_info_list):
47    api_result_info_list.extend(check_file_name(file_info['name']))
48    apis = file_info['children']
49    file_doc_info = FileDocInfo()
50    api_result_info_list.extend(process_comment(file_info["comment"], file_doc_info, file_info))
51    for api in apis:
52        process_api_json(api, file_doc_info, api_result_info_list)
53    api_result_info_list.extend(process_file_doc_info(file_doc_info, file_info))
54
55
56def process_all_json(python_obj):
57    api_result_info_list = []
58    for file_info in python_obj:
59        process_file_json(file_info, api_result_info_list)
60    return api_result_info_list
61
62
63def write_in_txt(check_result, output_path):
64    result_json = result_to_json(check_result)
65    with open(output_path, 'w', encoding='utf-8') as fs:
66        fs.write(result_json)
67        fs.close()
68
69
70def result_to_json(check_result):
71    txt_resul = []
72    for result in check_result:
73        location = f'{result.location}(line:{result.location_line}, col:{result.location_column})'
74        message = 'API check error of [{}]:{}'.format(result.error_type['description'], result.error_info)
75        txt_resul.append(OutputTxt(result.error_type['id'], result.level, location, result.file_name, message))
76    return json.dumps(txt_resul, default=lambda obj: obj.__dict__, indent=4)
77
78
79def curr_entry(md_files_path):
80    file_list = get_md_files(md_files_path)
81    check_result_list = []
82    if len(file_list) > 0:
83        check_result_list = get_check_result_list(file_list)
84    write_in_txt(check_result_list, r'./Error.txt')
85
86
87def get_check_result_list(file_list):
88    check_result_list = []
89    for file in file_list:
90        root_start = file.split('sdk_c')[0]
91        root_path = f'{root_start}sdk_c'
92        python_obj = parser_include_ast(root_path, [file])
93        check_result_list.extend(process_all_json(python_obj))
94        check_result_list.extend(check_syntax(file))
95    return check_result_list
96
97
98def get_md_files(md_files_path):
99    with open(md_files_path, 'r') as file:
100        file_list = []
101        line = file.readline()
102        while line:
103            file_path = line.splitlines()[0]
104            if file_path.find("sdk_c") != -1 and get_file_type(file_path) == '.h':
105                file_list.append(line.splitlines()[0])
106            line = file.readline()
107        file.close()
108        return file_list
109
110
111def get_file_type(file_path):
112    return Path(file_path).suffix
113