-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathbump-version.py
More file actions
executable file
·78 lines (58 loc) · 1.91 KB
/
bump-version.py
File metadata and controls
executable file
·78 lines (58 loc) · 1.91 KB
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
#!/usr/bin/env python
import re
import sys
def get_old_version():
with open("pyproject.toml") as fp:
content = fp.read()
match = re.search(r'^version = "(\d+\.\d+\.\d+)"$', content, flags=re.MULTILINE)
assert match
return match.group(1)
def replace_version(filename, formatstr, old_version, new_version):
print(f"updating {filename}")
with open(filename) as fp:
content = fp.read()
old_str = formatstr.format(old_version)
new_str = formatstr.format(new_version)
content = content.replace(old_str, new_str)
with open(filename, "w") as fp:
fp.write(content)
def update_changelog(new_version):
print("updating CHANGELOG.rst")
with open("CHANGELOG.rst") as fp:
content = fp.read()
vendor_marker = ".. vendor-insert-here"
content = re.sub(
r"""
Unreleased
----------
(\s*\n)+""" + re.escape(vendor_marker),
f"""
Unreleased
----------
{vendor_marker}
{new_version}
{"-" * len(new_version)}""",
content,
)
with open("CHANGELOG.rst", "w") as fp:
fp.write(content)
def parse_version(s):
vals = s.split(".")
assert len(vals) == 3
return tuple(int(x) for x in vals)
def comparse_versions(old_version, new_version):
assert parse_version(new_version) > parse_version(old_version)
def main():
if len(sys.argv) != 2:
sys.exit(2)
new_version = sys.argv[1]
old_version = get_old_version()
print(f"old = {old_version}, new = {new_version}")
comparse_versions(old_version, new_version)
replace_version("pyproject.toml", 'version = "{}"', old_version, new_version)
replace_version("README.md", "rev: {}", old_version, new_version)
replace_version("docs/precommit_usage.rst", "rev: {}", old_version, new_version)
replace_version("docs/optional_parsers.rst", "rev: {}", old_version, new_version)
update_changelog(new_version)
if __name__ == "__main__":
main()