/
make_release.py
350 lines (292 loc) · 9.78 KB
/
make_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
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
# Copyright (c) 2019-2022 Artem Bulgakov
#
# This file is distributed under the terms of the same license,
# as the Kivy framework.
"""
Script to make release
======================
Run this script before release (before deploying).
What this script does:
* Undo all local changes in repository
* Update version in `__init__.py`, `README.md`
* Format files
* Rename file "unreleased.rst" to version, add to index.rst
* Commit "Version ..."
* Create tag
* Add `unreleased.rst` to Changelog, add to `index.rst`
* Commit
* Git push
"""
import os
import re
import subprocess
import sys
from kivymd.tools.argument_parser import ArgumentParserWithHelp
from kivymd.tools.release.git_commands import (
command,
get_previous_version,
git_clean,
git_commit,
git_push,
git_tag,
)
from kivymd.tools.release.update_icons import update_icons
def run_pre_commit():
"""Run pre-commit."""
try:
command(["pre-commit", "run", "--all-files"])
except subprocess.CalledProcessError:
pass
git_commit("Run pre-commit", allow_error=True)
def replace_in_file(pattern, repl, file):
"""Replace one `pattern` match to `repl` in file `file`."""
file_content = open(file, "rt", encoding="utf-8").read()
new_file_content = re.sub(pattern, repl, file_content, 1, re.M)
open(file, "wt", encoding="utf-8").write(new_file_content)
return not file_content == new_file_content
def update_version_py(version, is_release, test: bool = False):
"""Change version in `kivymd/_version.py`."""
init_file = os.path.abspath("kivymd/_version.py")
init_version_regex = r"(?<=^__version__ = ['\"])[^'\"]+(?=['\"]$)"
success = replace_in_file(init_version_regex, version, init_file)
if test and not success:
print("Couldn't update _version.py file.", file=sys.stderr)
init_version_regex = r"(?<=^release = )(True|False)(?=$)"
success = replace_in_file(init_version_regex, str(is_release), init_file)
if test and not success:
print("Couldn't update _version.py file.", file=sys.stderr)
def update_readme(previous_version, version, test: bool = False):
"""Change version in `README.md`."""
readme_file = os.path.abspath("README.md")
readme_version_regex = rf"(?<=\[){previous_version}[ \-*\w^\]\n]*(?=\])"
success = replace_in_file(readme_version_regex, version, readme_file)
if test and not success:
print("Couldn't update README.md file.", file=sys.stderr)
readme_install_version_regex = (
rf"(?<=pip install kivymd==){previous_version}(?=\n```)"
)
success = replace_in_file(
readme_install_version_regex, version, readme_file
)
if test and not success:
print("Couldn't update README.md file.", file=sys.stderr)
readme_buildozer_version_regex = rf"(?<=, kivymd==){previous_version}(?=, )"
success = replace_in_file(
readme_buildozer_version_regex, version, readme_file
)
if test and not success:
print("Couldn't update README.md file.", file=sys.stderr)
def move_changelog(
index_file,
unreleased_file,
previous_version,
version_file,
version,
test: bool = False,
):
"""Edit unreleased.rst and rename to <version>.rst."""
# Read unreleased changelog
changelog = open(unreleased_file, "rt", encoding="utf-8").read()
# Edit changelog
changelog = re.sub(
r"Unreleased\n----------",
f"{version}\n{'-' * (1 + len(version))}",
changelog,
1,
re.M,
)
changelog = re.sub(
r"(?<=See on GitHub: `)branch master",
f"tag {version}",
changelog,
1,
re.M,
)
changelog = re.sub(r"(?<=/tree/)master", f"{version}", changelog, 1, re.M)
changelog = re.sub(
rf"(?<=compare {previous_version}/)master",
f"{version}",
changelog,
1,
re.M,
)
changelog = re.sub(
rf"(?<=compare/{previous_version}...)master",
f"{version}",
changelog,
1,
re.M,
)
changelog = re.sub(
r"(?<=pip install )https[\S]*/master.zip(?=\n)",
f"kivymd=={version}",
changelog,
1,
re.M,
)
# Write changelog
open(version_file, "wt", encoding="utf-8").write(changelog)
# Remove unreleased changelog
os.remove(unreleased_file)
# Update index file
success = replace_in_file(
"/changelog/unreleased.rst", f"/changelog/{version}.rst", index_file
)
if test and not success:
print("Couldn't update changelog file.", file=sys.stderr)
def create_unreleased_changelog(
index_file,
unreleased_file,
version,
ask: bool = True,
test: bool = False,
):
"""Create unreleased.rst by template."""
# Check if unreleased file exists
if os.path.exists(unreleased_file):
if ask and input(
f'Do you want to rewrite "{unreleased_file}"? (y)'
) not in (
"",
"y",
"yes",
):
exit(0)
# Generate unreleased changelog
changelog = f"""Unreleased
----------
See on GitHub: `branch master <https://github.com/kivymd/KivyMD/tree/master>`_ | `compare {version}/master <https://github.com/kivymd/KivyMD/compare/{version}...master>`_
.. code-block:: bash
pip install https://github.com/kivymd/KivyMD/archive/master.zip
* Bug fixes and other minor improvements.
"""
# Create unreleased file
open(unreleased_file, "wt", encoding="utf-8").write(changelog)
# Update index file
success = replace_in_file(
r"(?<=Changelog\n=========\n\n)",
".. include:: /changelog/unreleased.rst\n",
index_file,
)
if test and not success:
print("Couldn't update changelog index file.", file=sys.stderr)
def main():
parser = create_argument_parser()
args = parser.parse_args()
release = args.command == "release"
version = args.version or "0.0.0"
next_version = args.next_version or (
(version[:-1] + str(int(version[-1]) + 1) + ".dev0")
if "rc" not in version
else version
)
prepare = args.command == "prepare"
test = args.command == "test"
ask = args.yes is not True
push = args.push is True
if release and version == "0.0.0":
parser.error("Please specify new version.")
version_re = r"[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}(rc[\d]{1,3})?"
if not re.match(version_re, version):
parser.error(f'Version "{version}" doesn\'t match template.')
next_version_re = (
r"[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}(\.dev[\d]{1,3}|rc[\d]{1,3})?"
)
if not re.match(next_version_re, next_version):
parser.error(f'Next version "{next_version}" doesn\'t match template.')
if test and push:
parser.error("Don't use --push with test.")
repository_root = os.path.normpath(
os.path.dirname(
os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
)
)
# Change directory to repository root
os.chdir(repository_root)
previous_version = get_previous_version()
# Print info
print(f"Previous version: {previous_version}")
print(f"New version: {version}")
print(f"Next version: {next_version}")
update_icons(make_commit=True)
git_clean(ask=ask)
run_pre_commit()
if prepare:
git_push([], ask=ask, push=push)
return
update_version_py(version, is_release=True, test=test)
update_readme(previous_version, version, test=test)
changelog_index_file = os.path.join(
repository_root, "docs", "sources", "changelog", "index.rst"
)
changelog_unreleased_file = os.path.join(
repository_root, "docs", "sources", "changelog", "unreleased.rst"
)
changelog_version_file = os.path.join(
repository_root, "docs", "sources", "changelog", f"{version}.rst"
)
move_changelog(
changelog_index_file,
changelog_unreleased_file,
previous_version,
changelog_version_file,
version,
test=test,
)
git_commit(f"KivyMD {version}")
git_tag(version)
branches_to_push = []
# Move branch stable to stable-x.x.x
# command(["git", "branch", "-m", "stable", f"stable-{old_version}"])
# branches_to_push.append(f"stable-{old_version}")
# Create branch stable
# command(["git", "branch", "stable"])
# command(["git", "push", "--force", "origin", "master:stable"])
# branches_to_push.append("stable")
create_unreleased_changelog(
changelog_index_file,
changelog_unreleased_file,
version,
test=test,
)
update_version_py(next_version, is_release=False, test=test)
git_commit(f"KivyMD {next_version}")
git_push(branches_to_push, ask=ask, push=push)
def create_argument_parser():
parser = ArgumentParserWithHelp(
prog="make_release.py",
allow_abbrev=False,
# usage="%(prog)s command [options] extensions [--exclude extensions]",
)
parser.add_argument(
"--yes",
action="store_true",
help="remove and modify files without asking.",
)
parser.add_argument(
"--push",
action="store_true",
help="push changes to remote repository. Use only with release and prepare.",
)
parser.add_argument(
"command",
choices=["release", "prepare", "test"],
help="release will update icons, modify files and make tag.\n"
"prepare will update icons and format files.\n"
"test will check if script can modify each file correctly.",
)
parser.add_argument(
"version",
type=str,
nargs="?",
help="new version in format n.n.n (1.111.11).",
)
parser.add_argument(
"next_version",
type=str,
nargs="?",
help="development version in format n.n.n.devn (1.111.11.dev0).",
)
return parser
if __name__ == "__main__":
main()