Skip to content

Commit cc497a2

Browse files
authored
chore: get English duplicated release notes (#15140)
1 parent 872df99 commit cc497a2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

scripts/get-en-duplicated-notes.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# This script helps you get duplicated release notes in English after duplicated release notes in Chinese are ready.
2+
# Before running this script, you need to first copy the duplicated release notes in Chinese to your target English release note file. Then this script can replace Chinese duplicated release notes with the corresponding English translation according to the release and issue number information of the duplicated release notes.
3+
4+
import re, os
5+
from tempfile import mkstemp
6+
from shutil import move
7+
from os import remove
8+
9+
ext_path = r'/Users/userid/Documents/GitHub/mygithubid/docs/releases' # Specify the directory of the English release notes folder
10+
main_path = r'/Users/userid/Documents/GitHub/mygithubid/docs/releases/release-7.1.2.md' # Specify the directory of the English release note file that you are preparing
11+
12+
# Get existing release notes from the English release notes folder
13+
def store_exst_rn(ext_path,main_path):
14+
15+
exst_notes = []
16+
exst_issue_nums = []
17+
18+
for maindir, subdir, files in os.walk(ext_path):
19+
for afile in files:
20+
file_path = (os.path.join(maindir, afile))
21+
if file_path.endswith('.md') and file_path != main_path: # Exclude duplicate notes that are in the current release file
22+
with open(file_path,'r', encoding='utf-8') as fp:
23+
level1 = level2 = level3 = ""
24+
for line in fp:
25+
exst_issue_num = re.search(r'https://github.com/(pingcap|tikv)/[\w-]+/(issues|pull)/\d+', line)
26+
authors = re.findall(r'@\[([^\]]+)\]', line) # Get the list of authors in this line
27+
if exst_issue_num:
28+
if exst_issue_num.group() not in exst_issue_nums:
29+
note_level = level1 + level2 + level3
30+
note_pair = [exst_issue_num.group(),line.strip(),afile, note_level, authors]
31+
exst_issue_nums.append(exst_issue_num.group())
32+
exst_notes.append(note_pair)
33+
else:
34+
continue
35+
elif line.startswith("##"):
36+
level1 = "> " + line.replace("##","").strip()
37+
level2 = level3 = ""
38+
elif (line.startswith ("+") or line.startswith ("-")) and (not authors):
39+
level2 = "> " + line.replace("+","").replace("-","").strip()
40+
level3 = ""
41+
elif (line.startswith (" +") or line.startswith (" -")) and (not authors):
42+
level3 = "> " + line.replace(" +","").replace(" -","").strip()
43+
else:
44+
continue
45+
else:
46+
pass
47+
48+
if len(exst_issue_nums) != 0:
49+
return exst_notes
50+
else:
51+
return 0
52+
53+
# Replace Chinese duplicated release notes with the corresponding English translation
54+
def replace_zh_dup_with_en_dup(note_pairs, main_path):
55+
DupNum = 0
56+
NoteNum = 0
57+
target_file_path = mkstemp()[1]
58+
source_file_path = main_path
59+
with open(target_file_path, 'w', encoding='utf-8') as target_file:
60+
with open(source_file_path, 'r', encoding='utf-8') as source_file:
61+
LineNum = 0
62+
for line in source_file:
63+
newline = line
64+
LineNum += 1
65+
original_release_file = re.search('release-\d+\.\d+\.\d+\.md', line)
66+
issue_num = re.search('https://github.com/(pingcap|tikv)/\w+/(issues|pull)/\d+', line)
67+
if issue_num and original_release_file:
68+
NoteNum +=1
69+
for note_pair in note_pairs:
70+
if issue_num.group() == note_pair[0] and "(dup)" in line and original_release_file.group() == note_pair[2]:
71+
print('A duplicated note is found in line ' + str(LineNum) + " from " + note_pair[2] + note_pair[1])
72+
dup_note = '- (dup): {} {} {}'.format(note_pair[2], note_pair[3], note_pair[1]).strip()
73+
newline = re.sub(r'- \(dup\): release-\d+\.\d+\.\d+\.md.*?\n',r'{}\n'.format(dup_note),line)
74+
print('The duplicated note is replaced with ' + newline)
75+
DupNum += 1
76+
else:
77+
continue
78+
break
79+
target_file.write(newline)
80+
81+
remove(source_file_path)
82+
move(target_file_path, source_file_path)
83+
DupRate = "%.0f%%" % (DupNum/NoteNum*100)
84+
print (str(DupNum) + " duplicated notes are found in " + str(NoteNum) + " notes. The duplicated rate is " + str(DupRate) + ".")
85+
86+
87+
if __name__ == "__main__":
88+
89+
note_pairs = store_exst_rn(ext_path,main_path)
90+
replace_zh_dup_with_en_dup(note_pairs, main_path)

0 commit comments

Comments
 (0)