-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupdate_doc_version.py
executable file
·104 lines (89 loc) · 3.47 KB
/
update_doc_version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import argparse
import pathlib
import difflib
import re
# Table of file names to strings to match and replace.
# The special $$VERSION$$ string will be replaced with the version number
# during both matching and replacing.
# All paths are relative to the root of the repository.
MATCHERS = {
'README.md':
["rust-maven-plugin:$$VERSION$$:build",
"<version>$$VERSION$$</version>"],
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('version', help='New version number')
parser.add_argument('-p', '--preview', action='store_true',
help='Preview changes without making them')
parser.add_argument('-n', '--num-lines', type=int, default=3,
help='Number of lines of context to show in diff')
parser.add_argument('--no-color', action='store_true',
help='Disable color-coded diff output')
return parser.parse_args()
def get_old_vers():
with open('README.md', 'r', encoding='utf-8') as f:
contents = f.read()
pat = re.compile(r'.*rust-maven-plugin:(.*?):build.*')
for line in contents.splitlines():
match = pat.search(line)
if match:
return match.group(1)
raise RuntimeError('Could not find old version in README.md')
def main():
args = parse_args()
old_vers = get_old_vers()
new_vers = args.version
old_contents = {}
new_contents = {}
for filename, matchers in MATCHERS.items():
file_path = pathlib.Path(filename)
with open(file_path, 'r', encoding='utf-8') as f:
contents = f.read()
old_contents[file_path] = contents
for matcher in matchers:
old_version_text = matcher.replace('$$VERSION$$', old_vers)
new_version_text = matcher.replace('$$VERSION$$', new_vers)
if old_version_text not in contents:
raise RuntimeError(
'Could not find "{}" in file "{}"'.format(
old_version_text, file_path))
contents = contents.replace(old_version_text, new_version_text)
new_contents[file_path] = contents
for file_path, new_text in new_contents.items():
if args.preview:
old_text = old_contents[file_path]
filename = str(file_path)
colors = not args.no_color
if colors:
green = '\x1b[38;5;16;48;5;2m'
red = '\x1b[38;5;16;48;5;1m'
end = '\x1b[0m'
else:
green = ''
red = ''
end = ''
diff = difflib.unified_diff(
old_text.splitlines(keepends=True),
new_text.splitlines(keepends=True),
fromfile=filename,
tofile=filename,
lineterm='',
n=args.num_lines)
if colors:
for line in diff:
if line.startswith('+') and not line.startswith('+++'):
print(green + line + end, end='')
elif line.startswith('-') and not line.startswith('---'):
print(red + line + end, end='')
else:
print(line, end='')
else:
for line in diff:
print(line, end='')
else:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_text)
if __name__ == '__main__':
main()