forked from NordSecurity/libtelio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.py
executable file
·225 lines (176 loc) · 5.66 KB
/
release.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
import argparse
import os
import re
import subprocess
import sys
DRY_RUN = False
def execute_command(command):
print(f"$ {command}")
if not DRY_RUN:
return subprocess.run(command, shell=True, check=True)
def check_project_root_directory():
if not os.path.isfile("Cargo.toml"):
print("Not in project's root directory or Cargo.toml not found.")
sys.exit(1)
with open("Cargo.toml") as f:
content = f.read()
if not re.search(r"^\[package\]", content, re.MULTILINE) or not re.search(
r'name\s*=\s*"telio"', content
):
print('This does not appear to be a "libtelio" repo.')
sys.exit(1)
def get_default_branch():
default_branch = (
subprocess.run(
"git symbolic-ref refs/remotes/origin/HEAD",
capture_output=True,
text=True,
shell=True,
)
.stdout.strip()
.replace("refs/remotes/origin/", "")
)
if not default_branch:
print("Failed to retrieve default branch.")
sys.exit(1)
return default_branch
def check_git_tree(branch):
current_branch = subprocess.run(
"git rev-parse --abbrev-ref HEAD",
capture_output=True,
text=True,
shell=True,
).stdout.strip()
if current_branch != branch:
print("Git tree is not on the default branch.")
sys.exit(1)
git_status = subprocess.run(
"git status --short",
capture_output=True,
text=True,
shell=True,
).stdout.strip()
if git_status:
print("Git tree is dirty.")
sys.exit(1)
def check_existing_tag(tag):
existing_tags = (
subprocess.run(
"git tag --list",
capture_output=True,
text=True,
shell=True,
)
.stdout.strip()
.split("\n")
)
if tag in existing_tags:
print(f"Tag '{tag}' already exists in the git tree.")
sys.exit(1)
def check_cargo_tools(install_missing_tools):
cargo_output = subprocess.run(
"cargo install --list",
capture_output=True,
text=True,
shell=True,
).stdout
if ("cargo-edit" not in cargo_output) or ("cargo-set-version" not in cargo_output):
if install_missing_tools:
if not DRY_RUN:
cargo_install_output = subprocess.run(
"cargo install cargo-edit",
capture_output=True,
text=True,
shell=True,
).stdout
if cargo_install_output.returncode != 0:
print("Failed to install 'cargo-edit'.")
sys.exit(1)
else:
print("$ cargo install cargo-edit")
else:
print(
"Required tool 'cargo-edit' not found. Use --install-missing-tools to install."
)
sys.exit(1)
def validate_tag_format(tag):
if not re.match(r"^v[0-9]+\.[0-9]+\.[0-9]+$", tag):
print(
"Invalid tag format. Expected format: 'vMAJOR.MINOR.BUGFIX', e.g. 'v4.12.3'."
)
sys.exit(1)
def update_changelog(tag):
changelog_file = "./changelog.md"
if not os.path.isfile(changelog_file):
print("Changelog file not found.")
sys.exit(1)
if not DRY_RUN:
with open(changelog_file) as f:
content = f.read()
content = re.sub(r"### UNRELEASED", tag, content, flags=re.IGNORECASE)
with open(changelog_file, "w") as f:
f.write(content)
else:
print(
'$ substitute "### UNRELEASED" --> "### {}" in ./changelog.md'.format(tag)
)
def update_cargo_toml(tag):
execute_command(f"cargo set-version {tag.replace('v', '')} -p telio")
def commit_and_push(tag, push, remote, branch):
execute_command(f"git add .")
execute_command(f"git commit --message 'Release {tag}'")
execute_command(f"git tag {tag}")
if push:
execute_command(f"git push {remote} {branch}")
execute_command(f"git push {remote} --tags")
def main():
parser = argparse.ArgumentParser(description="Libtelio release helper script")
parser.add_argument(
"--dry-run",
action="store_true",
help="Run in dry-run mode, only prints commands",
)
parser.add_argument(
"--install-missing-tools",
action="store_true",
help="Install missing tools (e.g., 'cargo-edit')",
)
parser.add_argument("--tag", help="Version to release (mandatory)")
parser.add_argument(
"--push", action="store_true", help="Push changes to the remote repository"
)
parser.add_argument(
"--changelog", action="store_true", help="Make changes to './changelog.md'"
)
parser.add_argument(
"--remote",
default="origin",
help="Remote name for git push (default: 'origin')",
)
parser.add_argument(
"--branch",
help="Remote name for git push (default repo branch will be used, if not supplied)",
)
args = parser.parse_args()
if not args.tag:
parser.error("The --tag argument is required.")
global DRY_RUN
if args.dry_run:
DRY_RUN = True
check_project_root_directory()
branch = None
if not args.branch:
branch = get_default_branch()
else:
branch = args.branch
check_git_tree(branch)
check_existing_tag(args.tag)
check_cargo_tools(args.install_missing_tools)
validate_tag_format(args.tag)
if args.changelog:
update_changelog(args.tag)
update_cargo_toml(args.tag)
commit_and_push(args.tag, args.push, args.remote, branch)
if __name__ == "__main__":
main()