-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrelease.py
More file actions
executable file
·129 lines (98 loc) · 4.17 KB
/
release.py
File metadata and controls
executable file
·129 lines (98 loc) · 4.17 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
import re
from datetime import datetime
import argparse
def print_status(prefix, total_width=40):
dots_needed = total_width - len(prefix) - len("DONE")
dots = "." * max(dots_needed, 0)
print(f"{prefix} {dots} DONE")
def update_news_md(file_path, version):
pattern = re.compile(r"^(#\s+[a-z]+)\s+[0-9.]+")
replacement = rf"\1 {version}"
with open(file_path, "r") as file:
content = file.read()
if not pattern.search(content):
raise ValueError(f'"No match found in {file_path} .')
new_content = pattern.sub(replacement, content, count=1)
with open(file_path, "w") as file:
file.write(new_content)
print_status(file_path)
def replace_dev_with_date(file_path, version):
today_str = datetime.now().strftime("%d/%m/%Y")
pattern = re.compile(r"Version\s+[0-9.]+\s+\([^)]+\)")
replacement = rf"Version {version} ({today_str})"
with open(file_path, "r") as file:
content = file.read()
if not pattern.search(content):
raise ValueError(f'"Version (dev)" not found in {file_path} .')
new_content = pattern.sub(replacement, content, count=1)
with open(file_path, "w") as file:
file.write(new_content)
print_status(file_path)
def update_package_version(file_path, search, remove="", replace="", append=""):
pattern = re.compile(rf'^({search}\s*[=:]\s*)(["\']?)(.*?)(["\']?)\s*$')
with open(file_path, "r") as file:
lines = file.readlines()
updated_lines = []
found = False
for line in lines:
m = pattern.match(line)
if m:
prefix, quote_start, value, quote_end = m.groups()
if remove:
new_value = value.replace(remove, "").strip()
elif replace:
new_value = replace
elif append:
new_value = value + append
new_line = f"{prefix}{quote_start}{new_value}{quote_end}\n"
updated_lines.append(new_line)
found = True
else:
updated_lines.append(line)
if not found:
raise ValueError(f"{search} not found in {file_path}.")
with open(file_path, "w") as file:
file.writelines(updated_lines)
print_status(file_path)
def get_package_version(file_path):
with open(file_path, "r") as file:
for line in file:
if line.startswith("PACKAGEVERSION"):
_, version_str = line.split("=", 1)
return version_str.strip().strip("'\"")
raise ValueError("PACKAGEVERSION not found in the file.")
def main():
parser = argparse.ArgumentParser(description="Release helper script")
parser.add_argument(
"--dev", type=str, help="Switch to development version given as argument"
)
args = parser.parse_args()
version = get_package_version("Makefile")
if args.dev is not None:
print(f"Current version is: {version}")
print(f"Development version set to: {args.dev}")
if version == args.dev:
raise ValueError(
f"Development version should higher than current one ({version})"
)
update_package_version("Makefile", "PACKAGEVERSION", replace=args.dev)
update_package_version("c/Makefile", "VERSION", append="$(REVISION)")
update_package_version("r/DESCRIPTION", "Version", replace=version + ".900")
update_package_version(
"python/pyproject.toml", "version", replace=args.dev + ".dev0"
)
print("*** If happy with the changes, then do:")
print(f"git ci -a -m 'Start development of v{version}'")
else:
print(f"Current version is: {version}")
print("Preparing for release")
update_package_version("c/Makefile", "VERSION", remove="$(REVISION)")
update_package_version("r/DESCRIPTION", "Version", replace=version)
update_package_version("python/pyproject.toml", "version", replace=version)
replace_dev_with_date("python/doc/source/whatsnew/index.rst", version)
update_news_md("r/NEWS.md", version)
print("*** If happy with the changes, then do:")
print(f"git ci -a -m 'Prepare to release v{version}'")
if __name__ == "__main__":
main()