forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_bump.py
executable file
·215 lines (165 loc) · 6.88 KB
/
version_bump.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
#!/usr/bin/env python3
"""Helper script to bump the current version."""
import argparse
import re
import subprocess
from packaging.version import Version
from homeassistant import const
from homeassistant.util import dt as dt_util
def _bump_release(release, bump_type):
"""Bump a release tuple consisting of 3 numbers."""
major, minor, patch = release
if bump_type == "patch":
patch += 1
elif bump_type == "minor":
minor += 1
patch = 0
return major, minor, patch
def bump_version(version, bump_type):
"""Return a new version given a current version and action."""
to_change = {}
if bump_type == "minor":
# Convert 0.67.3 to 0.68.0
# Convert 0.67.3.b5 to 0.68.0
# Convert 0.67.3.dev0 to 0.68.0
# Convert 0.67.0.b5 to 0.67.0
# Convert 0.67.0.dev0 to 0.67.0
to_change["dev"] = None
to_change["pre"] = None
if not version.is_prerelease or version.release[2] != 0:
to_change["release"] = _bump_release(version.release, "minor")
elif bump_type == "patch":
# Convert 0.67.3 to 0.67.4
# Convert 0.67.3.b5 to 0.67.3
# Convert 0.67.3.dev0 to 0.67.3
to_change["dev"] = None
to_change["pre"] = None
if not version.is_prerelease:
to_change["release"] = _bump_release(version.release, "patch")
elif bump_type == "dev":
# Convert 0.67.3 to 0.67.4.dev0
# Convert 0.67.3.b5 to 0.67.4.dev0
# Convert 0.67.3.dev0 to 0.67.3.dev1
if version.is_devrelease:
to_change["dev"] = ("dev", version.dev + 1)
else:
to_change["pre"] = ("dev", 0)
to_change["release"] = _bump_release(version.release, "minor")
elif bump_type == "beta":
# Convert 0.67.5 to 0.67.6b0
# Convert 0.67.0.dev0 to 0.67.0b0
# Convert 0.67.5.b4 to 0.67.5b5
if version.is_devrelease:
to_change["dev"] = None
to_change["pre"] = ("b", 0)
elif version.is_prerelease:
if version.pre[0] == "a":
to_change["pre"] = ("b", 0)
if version.pre[0] == "b":
to_change["pre"] = ("b", version.pre[1] + 1)
else:
to_change["pre"] = ("b", 0)
to_change["release"] = _bump_release(version.release, "patch")
else:
to_change["release"] = _bump_release(version.release, "patch")
to_change["pre"] = ("b", 0)
elif bump_type == "nightly":
# Convert 0.70.0d0 to 0.70.0d20190424, fails when run on non dev release
if not version.is_devrelease:
raise ValueError("Can only be run on dev release")
to_change["dev"] = ("dev", dt_util.utcnow().strftime("%Y%m%d"))
else:
assert False, f"Unsupported type: {bump_type}"
temp = Version("0")
temp._version = version._version._replace(**to_change)
return Version(str(temp))
def write_version(version):
"""Update Home Assistant constant file with new version."""
with open("homeassistant/const.py") as fil:
content = fil.read()
major, minor, patch = str(version).split(".", 2)
content = re.sub(
"MAJOR_VERSION: Final = .*\n", f"MAJOR_VERSION: Final = {major}\n", content
)
content = re.sub(
"MINOR_VERSION: Final = .*\n", f"MINOR_VERSION: Final = {minor}\n", content
)
content = re.sub(
"PATCH_VERSION: Final = .*\n", f'PATCH_VERSION: Final = "{patch}"\n', content
)
with open("homeassistant/const.py", "w") as fil:
fil.write(content)
def write_version_metadata(version: Version) -> None:
"""Update pyproject.toml file with new version."""
with open("pyproject.toml", encoding="utf8") as fp:
content = fp.read()
content = re.sub(r"(version\W+=\W).+\n", f'\\g<1>"{version}"\n', content, count=1)
with open("pyproject.toml", "w", encoding="utf8") as fp:
fp.write(content)
def write_ci_workflow(version: Version) -> None:
"""Update ci workflow with new version."""
with open(".github/workflows/ci.yaml") as fp:
content = fp.read()
short_version = ".".join(str(version).split(".", maxsplit=2)[:2])
content = re.sub(
r"(\n\W+HA_SHORT_VERSION: )\"\d{4}\.\d{1,2}\"\n",
f'\\g<1>"{short_version}"\n',
content,
count=1,
)
with open(".github/workflows/ci.yaml", "w") as fp:
fp.write(content)
def main():
"""Execute script."""
parser = argparse.ArgumentParser(description="Bump version of Home Assistant")
parser.add_argument(
"type",
help="The type of the bump the version to.",
choices=["beta", "dev", "patch", "minor", "nightly"],
)
parser.add_argument(
"--commit", action="store_true", help="Create a version bump commit."
)
arguments = parser.parse_args()
if (
arguments.commit
and subprocess.run(["git", "diff", "--quiet"], check=False).returncode == 1
):
print("Cannot use --commit because git is dirty.")
return
current = Version(const.__version__)
bumped = bump_version(current, arguments.type)
assert bumped > current, "BUG! New version is not newer than old version"
write_version(bumped)
write_version_metadata(bumped)
write_ci_workflow(bumped)
print(bumped)
if not arguments.commit:
return
subprocess.run(["git", "commit", "-nam", f"Bump version to {bumped}"], check=True)
def test_bump_version():
"""Make sure it all works."""
import pytest
assert bump_version(Version("0.56.0"), "beta") == Version("0.56.1b0")
assert bump_version(Version("0.56.0b3"), "beta") == Version("0.56.0b4")
assert bump_version(Version("0.56.0.dev0"), "beta") == Version("0.56.0b0")
assert bump_version(Version("0.56.3"), "dev") == Version("0.57.0.dev0")
assert bump_version(Version("0.56.0b3"), "dev") == Version("0.57.0.dev0")
assert bump_version(Version("0.56.0.dev0"), "dev") == Version("0.56.0.dev1")
assert bump_version(Version("0.56.3"), "patch") == Version("0.56.4")
assert bump_version(Version("0.56.3.b3"), "patch") == Version("0.56.3")
assert bump_version(Version("0.56.0.dev0"), "patch") == Version("0.56.0")
assert bump_version(Version("0.56.0"), "minor") == Version("0.57.0")
assert bump_version(Version("0.56.3"), "minor") == Version("0.57.0")
assert bump_version(Version("0.56.0.b3"), "minor") == Version("0.56.0")
assert bump_version(Version("0.56.3.b3"), "minor") == Version("0.57.0")
assert bump_version(Version("0.56.0.dev0"), "minor") == Version("0.56.0")
assert bump_version(Version("0.56.2.dev0"), "minor") == Version("0.57.0")
today = dt_util.utcnow().strftime("%Y%m%d")
assert bump_version(Version("0.56.0.dev0"), "nightly") == Version(
f"0.56.0.dev{today}"
)
with pytest.raises(ValueError):
assert bump_version(Version("0.56.0"), "nightly")
if __name__ == "__main__":
main()