-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbump_version.py
More file actions
113 lines (96 loc) · 3.71 KB
/
bump_version.py
File metadata and controls
113 lines (96 loc) · 3.71 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
# ruff: noqa: T201
import os
import re
from pathlib import Path
from git import GitCommandError, Repo
from semver import Version
MAIN_BRANCH = "main"
VERSION_OCCURANCES = [
("pyproject.toml", r'(version = ")(\S+)(")', 1),
("main.py", r'(VERSION = ")(\S+)(")', 1),
("compose.yaml", r"(image: ghcr.io/bernikr/ownhaystack:)(\S+)()", 1),
]
UPDATE_UV = True
versions = set()
has_wanings = False
for filename, regex, occurrences in VERSION_OCCURANCES:
with Path(__file__).parent.joinpath(filename).open("r") as f:
res = re.findall(regex, f.read())
if len(res) != occurrences:
print(f"WARNING: version occurance mismatch in {filename}")
has_wanings = True
versions.update(r[1] for r in res)
if len(versions) != 1:
print(f"WARNING: multiple versions found: {versions}")
has_wanings = True
version = max(Version.parse(v) for v in versions)
if version.prerelease:
versions = {
"r": version.finalize_version(),
"b": version.bump_prerelease(),
}
else:
versions = {
"M": version.bump_major(),
"m": version.bump_minor(),
"p": version.bump_patch(),
"bM": version.bump_major().bump_prerelease("beta"),
"bm": version.bump_minor().bump_prerelease("beta"),
"bp": version.bump_patch().bump_prerelease("beta"),
}
print(f"Current version: {version}")
print("Possible next versions:")
for i, (key, value) in enumerate(versions.items(), start=1):
print(f" {i} or {key}:{' ' * (4 - len(key))}{value}")
next_version = version.bump_prerelease() if version.prerelease else version.bump_patch()
print(f"Default version: {next_version} [Enter to confirm] or enter a new version")
user_input = input()
if user_input:
if user_input in versions:
next_version = versions[user_input]
elif user_input.isnumeric():
next_version = list(versions.values())[int(user_input) - 1]
else:
next_version = Version.parse(user_input)
print(f"Entered version: {next_version}")
if not next_version > version:
print("WARNING: new version should be greater than current version")
has_wanings = True
repo = Repo(Path(__file__).parent)
if repo.is_dirty():
print("WARNING: repo is dirty, please commit or stage changes before continuing")
input("Press enter to continue")
for filename, regex, _ in VERSION_OCCURANCES:
with Path(__file__).parent.joinpath(filename).open("r+") as f:
res = re.sub(regex, f"\\g<1>{next_version}\\g<3>", f.read())
f.seek(0)
f.write(res)
f.truncate()
if UPDATE_UV:
os.system("uv lock") # noqa: S605, S607
if has_wanings:
print("WARNING: there were warnings, please check the output before contiuning")
input("Press enter to continue")
res = input("Do you want to commit the changes? [y/N] ")
if res.lower() in {"y", "yes"}:
repo.git.add(*{filename for filename, _, _ in VERSION_OCCURANCES})
if UPDATE_UV:
repo.git.add("uv.lock")
repo.git.commit("-m", f"Bump version to {next_version}")
repo.create_tag(f"v{next_version}", message=f"Bump version to {next_version}")
print("changes committed and created tag")
if not next_version.prerelease:
cur_branch = repo.active_branch.name
repo.git.checkout(MAIN_BRANCH)
try:
repo.git.merge(cur_branch, ff_only=True)
except GitCommandError:
print(f"WARNING: merge {cur_branch} into {MAIN_BRANCH} failed, please merge manually")
finally:
repo.git.checkout(cur_branch)
res = input("Do you want to push the changes? [y/N] ")
if res.lower() in {"y", "yes"}:
repo.git.push(follow_tags=True)
if not next_version.prerelease:
repo.git.push("origin", MAIN_BRANCH)
print("changes pushed")