-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbump_version.py
64 lines (54 loc) · 1.98 KB
/
bump_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
# Replaces old version number for the new one
import argparse
import re
from pathlib import Path
version_sub_re = re.compile(r"__version__ = \(\d+, \d+, \d+\)")
version_re = re.compile(r"\d+\.\d+\.\d+")
def version_num(value: str):
if not version_re.match(value):
raise argparse.ArgumentTypeError(
f'"{value}" is an invalid version number format'
)
return value
def run(src_file: str, new_version: str):
temp_path = Path(f"{src_file}.temp")
src_path = Path(src_file)
with src_path.open("r", encoding="utf-8") as f_in, temp_path.open(
"w", encoding="utf-8"
) as f_out:
while True:
line = f_in.readline()
if not line:
break
mobj = version_sub_re.search(line)
if not mobj:
f_out.write(line)
continue
version_tuple = new_version.split(".")
new_line = version_sub_re.sub(
f'__version__ = ({", ".join(version_tuple)})',
line,
)
print(
f"{src_file}:\n"
f"* before:\t{line.strip()}"
f"\n* after:\t{new_line.strip()}"
)
f_out.write(new_line)
temp_path.rename(src_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Bump version")
parser.add_argument(
"new_version", type=version_num, help="New version number. Format d.d.d"
)
args = parser.parse_args()
src_files = ("calibre-plugin/__init__.py",)
for src in src_files:
run(src, args.new_version)
print(f'\nROLLBACK git commands:\ngit restore {" ".join(src_files)}\n')
print(
f'COMMIT git commands:\ngit add -u {" ".join(src_files)} calibre-plugin/translations/ translate.sh'
)
print(f"git commit -m 'Bump version to {args.new_version}'")
print(f"git tag --sign -a 'v{args.new_version}' -m 'Release v{args.new_version}'")
print("git push && git push --tags\n")