-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathgithub_increment_version.py
351 lines (284 loc) · 13.9 KB
/
github_increment_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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""
This script automatically increments the IPv8 setup.py version and creates a changelog message for it on GitHub.
You are tasked with opening a PR from your fork.
IMPORTANT: THIS SCRIPT ASSUMES THAT YOUR LOCAL WORKSPACE IS CLEAN. DO NOT RUN THIS SCRIPT WITH UNCOMMITTED CHANGES!
Other caveats:
- Claims the local branchname ``__automated_version_update``, which may not be cleaned on error.
- Claims the remote branchname ``automated_version_update`` on your GitHub fork.
- Adds the local remote ``__Tribler``, which may not be cleaned on error!
- Adds the local remote ``__<username>``, which may not be cleaned on error!
The local branch and local remotes should not exist before running this script.
"""
from __future__ import annotations
import ast
import datetime
import subprocess
import sys
from typing import cast
from packaging.version import Version
# ruff: noqa: S602,S603,S607,T201
def parse_setup() -> tuple[str, ast.Expr]:
"""
Search for the ``setup(...)`` expression in ``setup.py`` and return its AST node.
We later update the ``version=...`` parameter.
"""
print("[1/8] | Parsing setup.py file.")
with open('setup.py') as f:
file_contents = f.read()
treeobj = ast.parse(file_contents, 'setup.py')
setup_expression = None
for element in treeobj.body:
if isinstance(element, ast.Expr) and cast(ast.Name, cast(ast.Call, element.value).func).id == "setup":
setup_expression = element
if not setup_expression:
print("No setup() found in setup.py!")
sys.exit(1)
return file_contents, setup_expression
def parse_doc_conf() -> tuple[str, tuple[ast.Constant, ast.Constant, ast.Constant]]:
"""
Search for ``copyright = ...``, ``version = ...``, and ``release = ...`` in ``doc/conf.py``
and return their AST nodes.
"""
print("[1/8] | Parsing doc/conf.py file.")
with open('doc/conf.py') as f:
file_contents = f.read()
treeobj = ast.parse(file_contents, 'doc/conf.py')
copyright_element = None
version_element = None
release_element = None
for element in treeobj.body:
if isinstance(element, ast.Assign) and isinstance(element.value, ast.Constant):
if cast(ast.Name, element.targets[0]).id == "copyright":
copyright_element = element.value
elif cast(ast.Name, element.targets[0]).id == "version":
version_element = element.value
elif cast(ast.Name, element.targets[0]).id == "release":
release_element = element.value
if not copyright_element:
print("No 'copyright' assignment found in doc/conf.py!")
sys.exit(1)
if not version_element:
print("No 'version' assignment found in doc/conf.py!")
sys.exit(1)
if not release_element:
print("No 'release' assignment found in doc/conf.py!")
sys.exit(1)
return file_contents, (copyright_element, version_element, release_element)
def parse_rest_manager() -> tuple[str, ast.Constant]:
"""
Search for ``aiohttp_apispec = AiohttpApiSpec(...)`` in ``ipv8/REST/rest_manager.py``
and return the ``version=...`` parameter AST node.
"""
print("[1/8] | Parsing ipv8/REST/rest_manager.py file.")
with open('ipv8/REST/rest_manager.py') as f:
file_contents = f.read()
treeobj = ast.parse(file_contents, 'ipv8/REST/rest_manager.py')
version_element = None
for element in treeobj.body:
if isinstance(element, ast.ClassDef) and element.name == "RESTManager":
for inner_definition in element.body:
if isinstance(inner_definition, ast.AsyncFunctionDef) and inner_definition.name == "start":
for f_statement in inner_definition.body:
if (isinstance(f_statement, ast.Assign)
and f_statement.targets
and isinstance(f_statement.targets[0], ast.Name)
and cast(ast.Name, f_statement.targets[0]).id == "aiohttp_apispec"
and isinstance(f_statement.value, ast.Call)
and isinstance(f_statement.value.func, ast.Name)
and f_statement.value.func.id == "AiohttpApiSpec"):
for setup_arg in f_statement.value.keywords:
if setup_arg.arg == "version":
version_element = setup_arg.value
if not version_element:
print("No 'version' assignment found in ipv8/REST/rest_manager.py!")
sys.exit(1)
return file_contents, cast(ast.Constant, version_element)
def modify_setup(file_contents: str, setup_expression: ast.Expr) -> tuple[str, str, str, str, str]:
"""
Rewrite the ``setup.py`` contents by modifying the ``setup(...)`` AST node.
"""
print("[2/8] | Modifying setup.py file.")
new_filecontents = ""
old_version = ""
old_version_tag = ""
new_version = ""
new_version_tag = ""
for keyword in cast(ast.Call, setup_expression.value).keywords:
if keyword.arg == "version":
lineno = keyword.value.lineno
coloffset = keyword.value.col_offset
old_version = cast(ast.Name, keyword.value).s
version = Version(old_version)
new_vstring = [version.major, version.minor, version.micro]
old_version_tag = '.'.join(str(s) for s in new_vstring[:2])
new_vstring[1] += 1
new_version = '.'.join(str(s) for s in new_vstring)
new_version_tag = '.'.join(str(s) for s in new_vstring[:2])
new_split_filecontents = file_contents.splitlines(True)
source_line = new_split_filecontents[lineno - 1]
new_split_filecontents[lineno - 1] = (source_line[:coloffset + 1]
+ new_version
+ source_line[len(old_version) + coloffset + 1:])
new_filecontents = "".join(new_split_filecontents)
break
return old_version, old_version_tag, new_version, new_version_tag, new_filecontents
def modify_docs(file_contents: str, ast_elements: tuple[ast.Constant, ast.Constant, ast.Constant],
new_version: str, new_version_tag: str) -> str:
"""
Rewrite the ``conf.py`` contents by modifying the ``copyright``, ``version``, and ``release`` values.
"""
print("[2/8] | Modifying doc/conf.py file.")
to_insert = [f"2017-{datetime.datetime.now().year}, Tribler", # noqa: DTZ005
new_version_tag,
new_version]
new_split_filecontents = file_contents.splitlines(True)
for i, element in enumerate(ast_elements):
lineno = element.lineno
coloffset = element.col_offset
old_version = element.s
source_line = new_split_filecontents[lineno - 1]
new_split_filecontents[lineno - 1] = (source_line[:coloffset + 1]
+ to_insert[i]
+ source_line[len(old_version) + coloffset + 1:])
return "".join(new_split_filecontents)
def modify_rest_manager(file_contents: str, element: ast.Constant, new_version_tag: str) -> str:
"""
Rewrite the ``rest_manager.py`` contents by modifying the ``version=`` constructor parameter.
"""
print("[2/8] | Modifying ipv8/REST/rest_manager.py file.")
new_split_filecontents = file_contents.splitlines(True)
lineno = element.lineno
coloffset = element.col_offset
old_version = element.s
source_line = new_split_filecontents[lineno - 1]
new_split_filecontents[lineno - 1] = (source_line[:coloffset + 1]
+ f"v{new_version_tag}"
+ source_line[len(old_version) + coloffset + 1:])
return "".join(new_split_filecontents)
print("[1/8] Parsing source files.")
old_setup_file, setup_ast = parse_setup()
old_docs_file, docs_ast_elements = parse_doc_conf()
old_rest_manager_file, rest_manager_ast = parse_rest_manager()
print("[2/8] Modifying source files.")
old_version, old_version_tag, new_version, new_version_tag, new_setup_file = modify_setup(old_setup_file, setup_ast)
new_docs_file = modify_docs(old_docs_file, docs_ast_elements, new_version, new_version_tag)
new_rest_manager_file = modify_rest_manager(old_rest_manager_file, rest_manager_ast, new_version_tag)
# LOGIN
print("[3/8] Requesting GitHub username.")
username = input('Username: ')
# GET REPOSITORY REFERENCES
print(f"[4/8] Retrieving Tribler:py-ipv8 and {username}:py-ipv8.")
# branchname or "HEAD"
original_branch = subprocess.check_output("git rev-parse --abbrev-ref HEAD", encoding="utf-8", shell=True).strip()
if original_branch == "HEAD":
# HEAD, origin/main, origin/HEAD
detached_details = subprocess.check_output("git show -s --pretty=%D HEAD", encoding="utf-8", shell=True)
original_branch = detached_details.split(", ")[1].strip()
print(subprocess.check_output(f"git remote add __{username} [email protected]:{username}/py-ipv8.git", encoding="utf-8", shell=True))
print(subprocess.check_output("git remote add __Tribler [email protected]:Tribler/py-ipv8.git", encoding="utf-8", shell=True))
print(subprocess.check_output("git fetch __Tribler master", encoding="utf-8", shell=True))
print(subprocess.check_output("git checkout -b __automated_version_update __Tribler/master", encoding="utf-8", shell=True))
# GET CHANGES
print("[5/8] Calculating changes since last release.")
known_tags = sorted(Version(t) for t in subprocess.check_output("git tag -l", encoding="utf-8", shell=True).split())
last_release_commit, = subprocess.check_output(f"git rev-list -n 1 {known_tags[-1]}", encoding="utf-8", shell=True).split()
git_log = subprocess.check_output(f'git log {last_release_commit}..HEAD --pretty=format:"%H"',
encoding="utf-8", shell=True).split("\n")
git_log = [
(
subprocess.check_output(f"git log --format=%B -n 1 {sha_entry}", encoding="utf-8", shell=True).split("\n")[0],
subprocess.check_output(f"git log --format=%b -n 1 {sha_entry}", encoding="utf-8", shell=True).split("\n")[0]
)
for sha_entry in git_log
]
commits_since_last = len(git_log) + 2
total_commits_str = subprocess.check_output("git rev-list --count HEAD", encoding="utf-8", shell=True)
total_commits = int(total_commits_str) + 2
# PERFORM FILE REWRITES
print("[6/8] Rewriting source files.")
with open("setup.py", "w") as f:
f.write(new_setup_file)
with open("doc/conf.py", "w") as f:
f.write(new_docs_file)
with open("ipv8/REST/rest_manager.py", "w") as f:
f.write(new_rest_manager_file)
# CREATE FEATURE BRANCH
print("[7/8] Pushing changes to branch on fork.")
print(subprocess.check_output("git add setup.py", encoding="utf-8", shell=True))
print(subprocess.check_output("git add doc/conf.py", encoding="utf-8", shell=True))
print(subprocess.check_output("git add ipv8/REST/rest_manager.py", encoding="utf-8", shell=True))
print(subprocess.check_output('git commit -m "Automated version increment"', encoding="utf-8", shell=True))
print(subprocess.check_output(f"git push -f -u __{username} __automated_version_update:automated_version_update",
encoding="utf-8", shell=True))
# > Cleanup
print(subprocess.check_output(f"git checkout {original_branch}", encoding="utf-8", shell=True))
print(subprocess.check_output("git branch -D __automated_version_update", encoding="utf-8", shell=True))
print(subprocess.check_output("git remote remove __Tribler", encoding="utf-8", shell=True))
print(subprocess.check_output(f"git remote remove __{username}", encoding="utf-8", shell=True))
# CREATE PULL REQUEST
print("[8/8] Formatting Pull Request message")
print("vv OUTPUT vv")
def commit_messages_to_names(commit_msg_list: list[str]) -> list[str]:
"""
Humans are not computers. Correct some common mistakes.
"""
out = []
misspellings = {
"add ": "Added ",
"Add ": "Added ",
"ADD ": "Added ",
"added ": "Added ",
"ADDED ": "Added ",
"fix ": "Fixed ",
"Fix ": "Fixed ",
"FIX ": "Fixed ",
"fixed ": "Fixed ",
"FIXED ": "Fixed ",
"update ": "Updated ",
"Update ": "Updated ",
"UPDATE ": "Updated ",
"updated ": "Updated ",
"UPDATED ": "Updated ",
"remove ": "Removed ",
"Remove ": "Removed ",
"REMOVE ": "Removed ",
"removed ": "Removed ",
"REMOVED ": "Removed "
}
residual_prefixes = {
"READY: ": "",
"ready: ": "",
"Ready: ": "",
"READY ": "",
"ready ": "",
"Ready ": "",
"WIP: ": "",
"wip: ": "",
"Wip: ": "",
"WIP ": "",
"wip ": "",
"Wip ": "",
"[READY] ": "",
"[WIP] ": ""
}
for commit_msg in commit_msg_list:
corrected = commit_msg
# First, strip residual prefixes, e.g. "READY: Add some feature" -> "Add some feature".
for mistake, correction in residual_prefixes.items():
if commit_msg.startswith(mistake):
corrected = correction + commit_msg[len(mistake):]
# Second, modify misspellings, e.g. "Add some feature" -> "Added some feature".
# We do this after the first step to correct compound errors (both leaving the prefix AND not adhering to
# the naming standard).
for mistake, correction in misspellings.items():
if corrected.startswith(mistake):
corrected = correction + corrected[len(mistake):]
out.append(corrected)
return sorted(out)
print("Title: Automated Version Update")
print(f"Tag version: {new_version_tag}")
print(f"Release title: IPv8 v{new_version_tag}.{total_commits} release")
print(f"Body:\n"
f"Includes the first {total_commits} commits (+{commits_since_last} since v{old_version_tag}) "
"for IPv8, containing:\n\n - "
+ ("\n - ".join(commit_messages_to_names([c[1] for c in git_log if c[0].startswith('Merge')]))))