|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import re |
| 4 | +import logging |
| 5 | +import argparse |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +logger = logging.getLogger() |
| 9 | +if logger.handlers: |
| 10 | + # we assume the first handler is the one we want to configure |
| 11 | + console = logger.handlers[0] |
| 12 | +else: |
| 13 | + console = logging.StreamHandler() |
| 14 | + logger.addHandler(console) |
| 15 | +console.setFormatter( |
| 16 | + logging.Formatter( |
| 17 | + "%(asctime)s - %(funcName)s:%(lineno)d - %(levelname)s - %(message)s" |
| 18 | + ) |
| 19 | +) |
| 20 | +logger.setLevel(logging.INFO) |
| 21 | + |
| 22 | +# check file's api_label |
| 23 | +def check_api_label(rootdir, file): |
| 24 | + real_file = Path(rootdir) / file |
| 25 | + with open(real_file, 'r', encoding='utf-8') as f: |
| 26 | + first_line = f.readline().strip() |
| 27 | + return first_line == generate_en_label_by_path(file) |
| 28 | + |
| 29 | + |
| 30 | +# path -> api_label (the first line's style) |
| 31 | +def generate_en_label_by_path(file): |
| 32 | + result = file.removesuffix('_cn.rst') |
| 33 | + result = result.replace('/', '_') |
| 34 | + result = f'.. _cn_{result}:' |
| 35 | + return result |
| 36 | + |
| 37 | + |
| 38 | +# traverse doc/api to append api_label in list |
| 39 | +def find_all_api_labels_in_dir(rootdir): |
| 40 | + all_api_labels = [] |
| 41 | + for root, dirs, files in os.walk(rootdir + API): |
| 42 | + for file in files: |
| 43 | + real_path = Path(root) / file |
| 44 | + path = str(real_path).removeprefix(rootdir) |
| 45 | + if not should_test(path): |
| 46 | + continue |
| 47 | + for label in find_api_labels_in_one_file(real_path): |
| 48 | + all_api_labels.append(label) |
| 49 | + return all_api_labels |
| 50 | + |
| 51 | + |
| 52 | +# api_labels in a file |
| 53 | +def find_api_labels_in_one_file(file_path): |
| 54 | + api_labels_in_one_file = [] |
| 55 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 56 | + lines = f.readlines() |
| 57 | + for line in lines: |
| 58 | + line = re.search(".. _([a-zA-Z0-9_]+)", line) |
| 59 | + if not line: |
| 60 | + continue |
| 61 | + api_labels_in_one_file.append(line.group(1)) |
| 62 | + return api_labels_in_one_file |
| 63 | + |
| 64 | + |
| 65 | +# api doc for checking |
| 66 | +def should_test(file): |
| 67 | + return ( |
| 68 | + file.endswith("_cn.rst") |
| 69 | + and not file.endswith("Overview_cn.rst") |
| 70 | + and not file.endswith("index_cn.rst") |
| 71 | + and file.startswith(API) |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def run_cn_api_label_checking(rootdir, files): |
| 76 | + for file in files: |
| 77 | + if should_test(file) and not check_api_label(rootdir, file): |
| 78 | + logger.error( |
| 79 | + f"The first line in {rootdir}/{file} is not avaiable, please re-check it!" |
| 80 | + ) |
| 81 | + sys.exit(1) |
| 82 | + valid_api_labels = find_all_api_labels_in_dir(rootdir) |
| 83 | + for file in files: |
| 84 | + if not file.endswith('.rst'): |
| 85 | + continue |
| 86 | + with open(Path(rootdir) / file, 'r', encoding='utf-8') as f: |
| 87 | + pattern = f.read() |
| 88 | + matches = re.findall(r":ref:`([^`]+)`", pattern) |
| 89 | + for match in matches: |
| 90 | + api_label = match |
| 91 | + if api_label_match := re.match( |
| 92 | + r".+<(?P<api_label>.+?)>", api_label |
| 93 | + ): |
| 94 | + api_label = api_label_match.group("api_label") |
| 95 | + if ( |
| 96 | + api_label.startswith('cn_api_paddle') |
| 97 | + and api_label not in valid_api_labels |
| 98 | + ): |
| 99 | + logger.error( |
| 100 | + f"Found api label {api_label} in {rootdir}/{file}, but it is not a valid api label, please re-check it!" |
| 101 | + ) |
| 102 | + sys.exit(1) |
| 103 | + print("All api_label check success in PR !") |
| 104 | + |
| 105 | + |
| 106 | +def parse_args(): |
| 107 | + """ |
| 108 | + Parse input arguments |
| 109 | + """ |
| 110 | + parser = argparse.ArgumentParser(description='cn api_label checking') |
| 111 | + parser.add_argument( |
| 112 | + 'rootdir', |
| 113 | + help='the dir DOCROOT', |
| 114 | + type=str, |
| 115 | + default='/FluidDoc/docs/', |
| 116 | + ) |
| 117 | + |
| 118 | + parser.add_argument( |
| 119 | + 'apiroot', |
| 120 | + type=str, |
| 121 | + help='the dir APIROOT', |
| 122 | + default='/FluidDoc/docs/api/', |
| 123 | + ) |
| 124 | + parser.add_argument( |
| 125 | + 'all_git_files', |
| 126 | + type=str, |
| 127 | + nargs='+', |
| 128 | + help='files need to check', |
| 129 | + ) |
| 130 | + args = parser.parse_args() |
| 131 | + return args |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + args = parse_args() |
| 136 | + API = args.apiroot.removeprefix(args.rootdir + '/') |
| 137 | + run_cn_api_label_checking(args.rootdir, args.all_git_files) |
0 commit comments