-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathbump.py
More file actions
executable file
·81 lines (64 loc) · 3.29 KB
/
Copy pathbump.py
File metadata and controls
executable file
·81 lines (64 loc) · 3.29 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
#!/usr/bin/env python3
# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2024 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #
import sys
import os
import re
import subprocess
# =====
def _run(cmd: list[str]) -> str:
return str(subprocess.run(cmd, stdout=subprocess.PIPE, check=True).stdout.decode())
def _get_version() -> tuple[int, int]:
ver = _run(["/usr/bin/git", "describe", "--tags", "--abbrev=0"]).strip()
assert (m := re.match(r"v(\d+)\.(\d+)", ver)), ver
return (int(m.group(1)), int(m.group(2)))
def _make_version(major: int, minor: int) -> str:
return f"v{major}.{minor}"
def _patch_file(path: str, replace: str, old: tuple[int, int], new: tuple[int, int]) -> None:
with open(path, "r+") as f:
old_text = f.read()
new_text = old_text.replace(
replace.format(major=old[0], minor=old[1]),
replace.format(major=new[0], minor=new[1]),
)
assert old_text != new_text, path
f.seek(0, os.SEEK_SET)
f.write(new_text)
def _bump(do_major: bool, items: list[tuple[str, str]]) -> None:
_run(["/usr/bin/git", "diff-index", "--quiet", "HEAD", "--"])
old = _get_version()
new = ((old[0] + 1, old[1]) if do_major else (old[0], old[1] + 1))
for (path, replace) in items:
_patch_file(path, replace, old, new)
_run(["/usr/bin/git", "add", path])
ver_new = _make_version(*new)
_run(["/usr/bin/git", "commit", "-m", f"Bump version: {_make_version(*old)} -> {ver_new}"])
_run(["/usr/bin/git", "tag", ver_new])
# =====
if __name__ == "__main__":
_bump(
do_major=(len(sys.argv) == 2 and sys.argv[1] == "major"),
items=[
("kvmd/__init__.py", "__version__ = \"{major}.{minor}\""),
("setup.py", "version=\"{major}.{minor}\""),
("PKGBUILD", "pkgver={major}.{minor}"),
],
)