Skip to content

Commit f6a8d37

Browse files
authored
[ci] 添加监测中文 apilabel 的 ci (#6226)
1 parent b757028 commit f6a8d37

File tree

4 files changed

+170
-2
lines changed

4 files changed

+170
-2
lines changed

ci_scripts/check_api_cn.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
set -x
33

4+
FLUIDDOCDIR=${FLUIDDOCDIR:=/FluidDoc}
45
OUTPUTDIR=${OUTPUTDIR:=/docs}
56
VERSIONSTR=${VERSIONSTR:=develop}
67

@@ -39,4 +40,4 @@ python check_copy_from_parsed_into_sample_code.py "${OUTPUTDIR}/zh/${VERSIONSTR}
3940
if [ $? -ne 0 ];then
4041
echo "ERROR: Exist COPY-FROM has not been parsed into sample code, please check COPY-FROM in the above files"
4142
exit 1
42-
fi
43+
fi

ci_scripts/check_api_label_cn.py

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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)

ci_scripts/check_api_label_cn.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
set -x
3+
4+
FLUIDDOCDIR=${FLUIDDOCDIR:=/FluidDoc}
5+
6+
DOCROOT=${FLUIDDOCDIR}/docs/
7+
APIROOT=${DOCROOT}/api/
8+
9+
SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
10+
source ${SCRIPT_DIR}/utils.sh
11+
12+
if [ -z ${BRANCH} ]; then
13+
BRANCH="develop"
14+
fi
15+
16+
all_git_files=`git diff --name-only --diff-filter=ACMR upstream/${BRANCH} | sed 's#docs/##g'`
17+
echo $all_git_files
18+
echo "Run API_LABEL Checking"
19+
python check_api_label_cn.py ${DOCROOT} ${APIROOT} $all_git_files
20+
21+
if [ $? -ne 0 ];then
22+
echo "ERROR: api_label is not correct, please check api_label in the above files"
23+
exit 1
24+
fi

ci_scripts/ci_start.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ else
122122
fi
123123
fi
124124

125+
# 5 Chinese api_label check
126+
/bin/bash -x ${DIR_PATH}/check_api_label_cn.sh
127+
if [ $? -ne 0 ];then
128+
EXIT_CODE=1
129+
fi
130+
125131
if [ ${EXIT_CODE} -ne 0 ]; then
126132
set +x
127133
echo "=========================================================================================="
@@ -131,7 +137,7 @@ if [ ${EXIT_CODE} -ne 0 ]; then
131137
exit ${EXIT_CODE}
132138
fi
133139

134-
# 5 Approval check
140+
# 6 Approval check
135141
/bin/bash ${DIR_PATH}/checkapproval.sh
136142
if [ $? -ne 0 ];then
137143
exit 1

0 commit comments

Comments
 (0)