2018-03-26 01:04:20 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Helper script to bump the current version."""
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
|
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
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
if bump_type == 'patch':
|
|
|
|
patch += 1
|
|
|
|
elif bump_type == 'minor':
|
|
|
|
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 = {}
|
|
|
|
|
|
|
|
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')
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-03-30 07:23:02 +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
|
2018-03-30 07:23:02 +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:
|
|
|
|
to_change['release'] = _bump_release(version.release, 'patch')
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
elif bump_type == 'dev':
|
|
|
|
# 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:
|
|
|
|
to_change['dev'] = ('dev', version.dev + 1)
|
2018-03-26 01:04:20 +00:00
|
|
|
else:
|
2018-03-30 07:23:02 +00:00
|
|
|
to_change['pre'] = ('dev', 0)
|
|
|
|
to_change['release'] = _bump_release(version.release, 'minor')
|
2018-03-26 01:04:20 +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:
|
|
|
|
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')
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
else:
|
2018-03-30 07:23:02 +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:
|
|
|
|
assert False, 'Unsupported type: {}'.format(bump_type)
|
2018-03-26 01:04:20 +00:00
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
temp = Version('0')
|
|
|
|
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."""
|
|
|
|
with open('homeassistant/const.py') as fil:
|
|
|
|
content = fil.read()
|
|
|
|
|
2018-03-30 07:23:02 +00:00
|
|
|
major, minor, patch = str(version).split('.', 2)
|
|
|
|
|
2018-03-26 01:04:20 +00:00
|
|
|
content = re.sub('MAJOR_VERSION = .*\n',
|
|
|
|
'MAJOR_VERSION = {}\n'.format(major),
|
|
|
|
content)
|
|
|
|
content = re.sub('MINOR_VERSION = .*\n',
|
|
|
|
'MINOR_VERSION = {}\n'.format(minor),
|
|
|
|
content)
|
|
|
|
content = re.sub('PATCH_VERSION = .*\n',
|
|
|
|
"PATCH_VERSION = '{}'\n".format(patch),
|
|
|
|
content)
|
|
|
|
|
|
|
|
with open('homeassistant/const.py', 'wt') as fil:
|
|
|
|
content = fil.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.",
|
2018-03-30 07:23:02 +00:00
|
|
|
choices=['beta', 'dev', 'patch', 'minor'],
|
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)
|
|
|
|
assert bumped > current, 'BUG! New version is not newer than old version'
|
|
|
|
write_version(bumped)
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_bump_version():
|
|
|
|
"""Make sure it all works."""
|
2018-03-30 07:23:02 +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')
|
2018-03-26 01:04:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|