2018-03-26 01:04:20 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Helper script to bump the current version."""
|
|
|
|
import argparse
|
|
|
|
import re
|
2018-06-21 21:27:01 +00:00
|
|
|
import subprocess
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
from packaging.version import Version
|
|
|
|
|
2018-03-26 01:04:20 +00:00
|
|
|
from homeassistant import const
|
|
|
|
|
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
def _bump_release(release, bump_type):
|
|
|
|
"""Bump a release tuple consisting of 3 numbers."""
|
|
|
|
major, minor, patch = release
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if bump_type == "patch":
|
2018-03-30 07:23:02 +00:00
|
|
|
patch += 1
|
2019-07-31 19:25:30 +00:00
|
|
|
elif bump_type == "minor":
|
2018-03-30 07:23:02 +00:00
|
|
|
minor += 1
|
|
|
|
patch = 0
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
return major, minor, patch
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
def bump_version(version, bump_type):
|
2018-03-26 01:04:20 +00:00
|
|
|
"""Return a new version given a current version and action."""
|
2018-03-30 07:23:02 +00:00
|
|
|
to_change = {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if bump_type == "minor":
|
2018-03-30 07:23:02 +00:00
|
|
|
# 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
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["dev"] = None
|
|
|
|
to_change["pre"] = None
|
2018-03-30 07:23:02 +00:00
|
|
|
|
|
|
|
if not version.is_prerelease or version.release[2] != 0:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["release"] = _bump_release(version.release, "minor")
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
elif bump_type == "patch":
|
2018-03-26 01:04:20 +00:00
|
|
|
# Convert 0.67.3 to 0.67.4
|
2018-03-26 21:55:09 +00:00
|
|
|
# Convert 0.67.3.b5 to 0.67.3
|
2018-03-26 01:04:20 +00:00
|
|
|
# Convert 0.67.3.dev0 to 0.67.3
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["dev"] = None
|
|
|
|
to_change["pre"] = None
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
if not version.is_prerelease:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["release"] = _bump_release(version.release, "patch")
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
elif bump_type == "dev":
|
2018-03-26 01:04:20 +00:00
|
|
|
# Convert 0.67.3 to 0.67.4.dev0
|
2018-03-26 21:55:09 +00:00
|
|
|
# Convert 0.67.3.b5 to 0.67.4.dev0
|
2018-03-26 01:04:20 +00:00
|
|
|
# Convert 0.67.3.dev0 to 0.67.3.dev1
|
2018-03-30 07:23:02 +00:00
|
|
|
if version.is_devrelease:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["dev"] = ("dev", version.dev + 1)
|
2018-03-26 01:04:20 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["pre"] = ("dev", 0)
|
|
|
|
to_change["release"] = _bump_release(version.release, "minor")
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
elif bump_type == "beta":
|
2018-03-30 07:23:02 +00:00
|
|
|
# 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:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["dev"] = None
|
|
|
|
to_change["pre"] = ("b", 0)
|
2018-03-30 07:23:02 +00:00
|
|
|
|
|
|
|
elif version.is_prerelease:
|
2019-07-31 19:25:30 +00:00
|
|
|
if version.pre[0] == "a":
|
|
|
|
to_change["pre"] = ("b", 0)
|
|
|
|
if version.pre[0] == "b":
|
|
|
|
to_change["pre"] = ("b", version.pre[1] + 1)
|
2018-03-30 07:23:02 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["pre"] = ("b", 0)
|
|
|
|
to_change["release"] = _bump_release(version.release, "patch")
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
to_change["release"] = _bump_release(version.release, "patch")
|
|
|
|
to_change["pre"] = ("b", 0)
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
else:
|
2019-08-23 16:53:33 +00:00
|
|
|
assert False, f"Unsupported type: {bump_type}"
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
temp = Version("0")
|
2018-03-30 07:23:02 +00:00
|
|
|
temp._version = version._version._replace(**to_change)
|
|
|
|
return Version(str(temp))
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
def write_version(version):
|
2018-03-26 01:04:20 +00:00
|
|
|
"""Update Home Assistant constant file with new version."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with open("homeassistant/const.py") as fil:
|
2018-03-26 01:04:20 +00:00
|
|
|
content = fil.read()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
major, minor, patch = str(version).split(".", 2)
|
2018-03-30 07:23:02 +00:00
|
|
|
|
2019-08-23 16:53:33 +00:00
|
|
|
content = re.sub("MAJOR_VERSION = .*\n", f"MAJOR_VERSION = {major}\n", content)
|
|
|
|
content = re.sub("MINOR_VERSION = .*\n", f"MINOR_VERSION = {minor}\n", content)
|
|
|
|
content = re.sub("PATCH_VERSION = .*\n", f"PATCH_VERSION = '{patch}'\n", content)
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with open("homeassistant/const.py", "wt") as fil:
|
2018-03-26 01:04:20 +00:00
|
|
|
content = fil.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Execute script."""
|
2019-07-31 19:25:30 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Bump version of Home Assistant")
|
2018-03-26 01:04:20 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"type",
|
2018-03-26 01:04:20 +00:00
|
|
|
help="The type of the bump the version to.",
|
2019-07-31 19:25:30 +00:00
|
|
|
choices=["beta", "dev", "patch", "minor"],
|
2018-03-26 01:04:20 +00:00
|
|
|
)
|
2018-06-21 21:27:01 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--commit", action="store_true", help="Create a version bump commit."
|
|
|
|
)
|
2018-03-26 01:04:20 +00:00
|
|
|
arguments = parser.parse_args()
|
2018-03-30 07:23:02 +00:00
|
|
|
current = Version(const.__version__)
|
|
|
|
bumped = bump_version(current, arguments.type)
|
2019-07-31 19:25:30 +00:00
|
|
|
assert bumped > current, "BUG! New version is not newer than old version"
|
2018-03-30 07:23:02 +00:00
|
|
|
write_version(bumped)
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-06-21 21:27:01 +00:00
|
|
|
if not arguments.commit:
|
|
|
|
return
|
|
|
|
|
2019-08-23 16:53:33 +00:00
|
|
|
subprocess.run(["git", "commit", "-am", f"Bumped version to {bumped}"])
|
2018-06-21 21:27:01 +00:00
|
|
|
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
def test_bump_version():
|
|
|
|
"""Make sure it all works."""
|
2019-07-31 19:25:30 +00:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-03-26 01:04:20 +00:00
|
|
|
main()
|