Update version handling and add helper script (#806)

* Update version handling and add helper script

* Remove typing hints

* Address review, add tag argument
This commit is contained in:
jjlawren 2021-09-12 19:59:23 -05:00 committed by GitHub
parent 692a23586c
commit b8cef9dc12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 119 additions and 7 deletions

View file

@ -6,6 +6,7 @@ from platform import uname
from uuid import getnode
from plexapi.config import PlexConfig, reset_base_headers
import plexapi.const as const
from plexapi.utils import SecretsFilter
# Load User Defined Config
@ -15,7 +16,7 @@ CONFIG = PlexConfig(CONFIG_PATH)
# PlexAPI Settings
PROJECT = 'PlexAPI'
VERSION = '4.7.0'
VERSION = __version__ = const.__version__
TIMEOUT = CONFIG.get('plexapi.timeout', 30, int)
X_PLEX_CONTAINER_SIZE = CONFIG.get('plexapi.container_size', 100, int)
X_PLEX_ENABLE_FAST_CONNECT = CONFIG.get('plexapi.enable_fast_connect', False, bool)

9
plexapi/const.py Normal file
View file

@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
"""Constants used by plexapi."""
# Library version
MAJOR_VERSION = 4
MINOR_VERSION = 7
PATCH_VERSION = 0
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__ = f"{__short_version__}.{PATCH_VERSION}"

View file

@ -9,11 +9,7 @@ try:
except ImportError:
from distutils.core import setup
# Get the current version
with open('plexapi/__init__.py') as handle:
for line in handle.readlines():
if line.startswith('VERSION'):
version = re.findall("'([0-9\.]+?)'", line)[0]
from plexapi import const
# Get README.rst contents
readme = open('README.rst', 'r').read()
@ -28,7 +24,7 @@ with open('requirements.txt') as handle:
setup(
name='PlexAPI',
version=version,
version=const.__version__,
description='Python bindings for the Plex API.',
author='Michael Shepanski',
author_email='michael.shepanski@gmail.com',

106
tools/version_bump.py Executable file
View file

@ -0,0 +1,106 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Helper script to bump the current version."""
import argparse
import re
import subprocess
from packaging.version import Version
from plexapi import const
SUPPORTED_BUMP_TYPES = ["patch", "minor", "major"]
def _bump_release(release, bump_type):
"""Return a bumped release tuple consisting of 3 numbers."""
major, minor, patch = release
if bump_type == "patch":
patch += 1
elif bump_type == "minor":
minor += 1
patch = 0
elif bump_type == "major":
major += 1
minor = 0
patch = 0
return major, minor, patch
def bump_version(version, bump_type):
"""Return a new version given a current version and action."""
new_release = _bump_release(version.release, bump_type)
temp = Version("0")
temp._version = version._version._replace(release=new_release)
return Version(str(temp))
def write_version(version):
"""Update plexapi constant file with new version."""
with open("plexapi/const.py") as f:
content = f.read()
version_names = ["MAJOR", "MINOR", "PATCH"]
version_values = str(version).split(".", 2)
for n, v in zip(version_names, version_values):
version_line = f"{n}_VERSION = "
content = re.sub(f"{version_line}.*\n", f"{version_line}{v}\n", content)
with open("plexapi/const.py", "wt") as f:
content = f.write(content)
def main():
"""Execute script."""
parser = argparse.ArgumentParser(description="Bump version of plexapi")
parser.add_argument(
"bump_type",
help="The type of version bump to perform",
choices=SUPPORTED_BUMP_TYPES,
)
parser.add_argument(
"--commit", action="store_true", help="Create a version bump commit"
)
parser.add_argument(
"--tag", action="store_true", help="Tag the commit with the release version"
)
arguments = parser.parse_args()
if arguments.tag and not arguments.commit:
parser.error("--tag requires use of --commit")
if arguments.commit and subprocess.run(["git", "diff", "--quiet"]).returncode == 1:
print("Cannot use --commit because git is dirty")
return
current = Version(const.__version__)
bumped = bump_version(current, arguments.bump_type)
assert bumped > current, "Bumped version is not newer than old version"
write_version(bumped)
if not arguments.commit:
return
subprocess.run(["git", "commit", "-nam", f"Release {bumped}"])
if arguments.tag:
subprocess.run(["git", "tag", str(bumped), "-m", f"Release {bumped}"])
def test_bump_version():
"""Make sure it all works."""
import pytest
assert bump_version(Version("4.7.0"), "patch") == Version("4.7.1")
assert bump_version(Version("4.7.0"), "minor") == Version("4.8.0")
assert bump_version(Version("4.7.3"), "minor") == Version("4.8.0")
assert bump_version(Version("4.7.0"), "major") == Version("5.0.0")
assert bump_version(Version("4.7.3"), "major") == Version("5.0.0")
assert bump_version(Version("5.0.0"), "major") == Version("6.0.0")
if __name__ == "__main__":
main()