|
| 1 | +# python main.py [source dir] [target dir] |
| 2 | +# python main.py ../paddle . |
| 3 | + |
| 4 | + |
| 5 | +import CppHeaderParser |
| 6 | +import json |
| 7 | +import os |
| 8 | +import traceback |
| 9 | +import sys |
| 10 | +import re |
| 11 | + |
| 12 | +from utils_helper import func_helper, class_helper, generate_overview |
| 13 | +from utils import get_PADDLE_API_class, get_PADDLE_API_func |
| 14 | + |
| 15 | + |
| 16 | +# 解析所有的函数, 类, 枚举, 返回一个字典 |
| 17 | +# 多线程使用并不安全, 请不要使用多线程 |
| 18 | +def analysis_file(path): |
| 19 | + header = CppHeaderParser.CppHeader(path, encoding='utf8') |
| 20 | + data = json.loads(header.toJSON()) |
| 21 | + return data |
| 22 | + |
| 23 | + |
| 24 | +# 生成文件 |
| 25 | +# 根据给定的list内容,生成对应的文档信息 |
| 26 | +def generate_docs( |
| 27 | + all_funcs, all_class, cpp2py_api_list, save_dir, LANGUAGE="cn" |
| 28 | +): |
| 29 | + for item in all_funcs: |
| 30 | + path = item["filename"].replace("../", "").replace(".h", "") |
| 31 | + dir_path = os.path.join(save_dir, LANGUAGE, path) |
| 32 | + if not os.path.exists(dir_path): |
| 33 | + os.makedirs(dir_path) |
| 34 | + |
| 35 | + # 这个反斜杠需要单独处理, 在 linux 下 |
| 36 | + func_name = item["name"].replace("/", "") |
| 37 | + |
| 38 | + # Note: 操作符仅不生成rst,实际上在Overview列表依然会呈现以提示存在此操作符 |
| 39 | + if func_name.startswith('operator'): |
| 40 | + checkwords = func_name.replace('operator', '', 1) |
| 41 | + if re.search(r"\w", checkwords) == None: |
| 42 | + continue # 跳过操作符声明 |
| 43 | + rst_dir = os.path.join(save_dir, LANGUAGE, path, func_name + ".rst") |
| 44 | + # avoid a filename such as operate*.rst, only windows |
| 45 | + try: |
| 46 | + helper = func_helper(item, cpp2py_api_list) |
| 47 | + helper.create_and_write_file(rst_dir, LANGUAGE) |
| 48 | + except: |
| 49 | + print(traceback.format_exc()) |
| 50 | + print('FAULT GENERATE:' + rst_dir) |
| 51 | + |
| 52 | + for item in all_class: |
| 53 | + path = item["filename"].replace("../", "").replace(".h", "") |
| 54 | + dir_path = os.path.join(save_dir, LANGUAGE, path) |
| 55 | + if not os.path.exists(dir_path): |
| 56 | + os.makedirs(dir_path) |
| 57 | + |
| 58 | + func_name = item["name"].replace("PADDLE_API", "") |
| 59 | + rst_dir = os.path.join(save_dir, LANGUAGE, path, func_name + ".rst") |
| 60 | + try: |
| 61 | + helper = class_helper(item) |
| 62 | + helper.create_and_write_file(rst_dir, LANGUAGE) |
| 63 | + except: |
| 64 | + print(traceback.format_exc()) |
| 65 | + print('FAULT GENERATE:' + rst_dir) |
| 66 | + |
| 67 | + |
| 68 | +# cpp 对应 python api |
| 69 | +# 用于存储 api 的名称, 用于后续生成对应python api文档链接 |
| 70 | +def cpp2py(data: dict): |
| 71 | + cpp2py_api_list = [] |
| 72 | + for i in data["using"]: |
| 73 | + cpp2py_api_list.append(i.replace("paddle::", "")) |
| 74 | + |
| 75 | + return cpp2py_api_list |
| 76 | + |
| 77 | + |
| 78 | +# 运行主函数,主要流程如下 |
| 79 | +# 1. 确定生成的目录 |
| 80 | +# 2. 提取待生成文档的PADDLE_API list |
| 81 | +# 3. 生成文档 |
| 82 | +if __name__ == "__main__": |
| 83 | + root_dir = '' |
| 84 | + save_dir = '.' # 默认保存在当前目录 |
| 85 | + if len(sys.argv) == 3: |
| 86 | + root_dir = sys.argv[1] |
| 87 | + save_dir = sys.argv[2] |
| 88 | + |
| 89 | + if root_dir == '': |
| 90 | + try: |
| 91 | + import paddle |
| 92 | + import inspect |
| 93 | + |
| 94 | + root_dir = os.path.dirname(inspect.getsourcefile(paddle)) |
| 95 | + except: |
| 96 | + # for simple run |
| 97 | + root_dir = '../paddle' |
| 98 | + save_dir = '.' # 默认保存在当前目录 |
| 99 | + |
| 100 | + all_funcs = [] |
| 101 | + all_class = [] |
| 102 | + cpp2py_api_list = [] |
| 103 | + overview_list = [] |
| 104 | + for home, dirs, files in os.walk(root_dir): |
| 105 | + for file_name in files: |
| 106 | + # 跳过不需要处理的文件 |
| 107 | + if file_name.split(".")[-1] not in ["cc", "cu", "h"]: |
| 108 | + continue |
| 109 | + |
| 110 | + file_path = os.path.join(home, file_name) |
| 111 | + # 处理 cpp 和 py api对应的文件, 目前只有这个文件内的 cpp api和 python api是对应的 |
| 112 | + if file_name == "tensor_compat.h": |
| 113 | + cpp2py_data = analysis_file(file_path) |
| 114 | + cpp2py_api_list = cpp2py(cpp2py_data).copy() |
| 115 | + |
| 116 | + # 跳过文件中未包含PADDLE_API |
| 117 | + with open(file_path, encoding='utf-8') as f: |
| 118 | + if 'PADDLE_API ' not in f.read(): |
| 119 | + continue |
| 120 | + |
| 121 | + print("Parsing: ", file_path) |
| 122 | + data = analysis_file(file_path) |
| 123 | + |
| 124 | + # 信息抽取 |
| 125 | + current_func = get_PADDLE_API_func(data) |
| 126 | + current_class = get_PADDLE_API_class(data) |
| 127 | + |
| 128 | + # 信息记录 |
| 129 | + all_funcs.extend(current_func) |
| 130 | + all_class.extend(current_class) |
| 131 | + overview_list.append( |
| 132 | + { |
| 133 | + 'h_file': file_path, |
| 134 | + 'class': current_class, |
| 135 | + 'function': current_func, |
| 136 | + } |
| 137 | + ) |
| 138 | + |
| 139 | + # 生成文档 |
| 140 | + generate_docs(all_funcs, all_class, cpp2py_api_list, save_dir, "cn") |
| 141 | + generate_docs(all_funcs, all_class, cpp2py_api_list, save_dir, "en") |
| 142 | + |
| 143 | + # 生成 overview |
| 144 | + generate_overview(overview_list, save_dir, "cn") |
| 145 | + generate_overview(overview_list, save_dir, "en") |
| 146 | + |
| 147 | + # 统计信息 |
| 148 | + print("PADDLE_API func count: ", len(all_funcs)) |
| 149 | + print("PADDLE_API class count: ", len(all_class)) |
| 150 | + print("cpp2py api count: ", len(cpp2py_api_list)) |
0 commit comments