Skip to content

Commit b8cef9d

Browse files
authored
Update version handling and add helper script (#806)
* Update version handling and add helper script * Remove typing hints * Address review, add tag argument
1 parent 692a235 commit b8cef9d

File tree

4 files changed

+119
-7
lines changed

4 files changed

+119
-7
lines changed

plexapi/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from uuid import getnode
77

88
from plexapi.config import PlexConfig, reset_base_headers
9+
import plexapi.const as const
910
from plexapi.utils import SecretsFilter
1011

1112
# Load User Defined Config
@@ -15,7 +16,7 @@
1516

1617
# PlexAPI Settings
1718
PROJECT = 'PlexAPI'
18-
VERSION = '4.7.0'
19+
VERSION = __version__ = const.__version__
1920
TIMEOUT = CONFIG.get('plexapi.timeout', 30, int)
2021
X_PLEX_CONTAINER_SIZE = CONFIG.get('plexapi.container_size', 100, int)
2122
X_PLEX_ENABLE_FAST_CONNECT = CONFIG.get('plexapi.enable_fast_connect', False, bool)

plexapi/const.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
"""Constants used by plexapi."""
3+
4+
# Library version
5+
MAJOR_VERSION = 4
6+
MINOR_VERSION = 7
7+
PATCH_VERSION = 0
8+
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
9+
__version__ = f"{__short_version__}.{PATCH_VERSION}"

setup.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
except ImportError:
1010
from distutils.core import setup
1111

12-
# Get the current version
13-
with open('plexapi/__init__.py') as handle:
14-
for line in handle.readlines():
15-
if line.startswith('VERSION'):
16-
version = re.findall("'([0-9\.]+?)'", line)[0]
12+
from plexapi import const
1713

1814
# Get README.rst contents
1915
readme = open('README.rst', 'r').read()
@@ -28,7 +24,7 @@
2824

2925
setup(
3026
name='PlexAPI',
31-
version=version,
27+
version=const.__version__,
3228
description='Python bindings for the Plex API.',
3329
author='Michael Shepanski',
3430
author_email='[email protected]',

tools/version_bump.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""Helper script to bump the current version."""
4+
import argparse
5+
import re
6+
import subprocess
7+
8+
from packaging.version import Version
9+
10+
from plexapi import const
11+
12+
SUPPORTED_BUMP_TYPES = ["patch", "minor", "major"]
13+
14+
15+
def _bump_release(release, bump_type):
16+
"""Return a bumped release tuple consisting of 3 numbers."""
17+
major, minor, patch = release
18+
19+
if bump_type == "patch":
20+
patch += 1
21+
elif bump_type == "minor":
22+
minor += 1
23+
patch = 0
24+
elif bump_type == "major":
25+
major += 1
26+
minor = 0
27+
patch = 0
28+
29+
return major, minor, patch
30+
31+
32+
def bump_version(version, bump_type):
33+
"""Return a new version given a current version and action."""
34+
new_release = _bump_release(version.release, bump_type)
35+
temp = Version("0")
36+
temp._version = version._version._replace(release=new_release)
37+
return Version(str(temp))
38+
39+
40+
def write_version(version):
41+
"""Update plexapi constant file with new version."""
42+
with open("plexapi/const.py") as f:
43+
content = f.read()
44+
45+
version_names = ["MAJOR", "MINOR", "PATCH"]
46+
version_values = str(version).split(".", 2)
47+
48+
for n, v in zip(version_names, version_values):
49+
version_line = f"{n}_VERSION = "
50+
content = re.sub(f"{version_line}.*\n", f"{version_line}{v}\n", content)
51+
52+
with open("plexapi/const.py", "wt") as f:
53+
content = f.write(content)
54+
55+
56+
def main():
57+
"""Execute script."""
58+
parser = argparse.ArgumentParser(description="Bump version of plexapi")
59+
parser.add_argument(
60+
"bump_type",
61+
help="The type of version bump to perform",
62+
choices=SUPPORTED_BUMP_TYPES,
63+
)
64+
parser.add_argument(
65+
"--commit", action="store_true", help="Create a version bump commit"
66+
)
67+
parser.add_argument(
68+
"--tag", action="store_true", help="Tag the commit with the release version"
69+
)
70+
arguments = parser.parse_args()
71+
72+
if arguments.tag and not arguments.commit:
73+
parser.error("--tag requires use of --commit")
74+
75+
if arguments.commit and subprocess.run(["git", "diff", "--quiet"]).returncode == 1:
76+
print("Cannot use --commit because git is dirty")
77+
return
78+
79+
current = Version(const.__version__)
80+
bumped = bump_version(current, arguments.bump_type)
81+
assert bumped > current, "Bumped version is not newer than old version"
82+
83+
write_version(bumped)
84+
85+
if not arguments.commit:
86+
return
87+
88+
subprocess.run(["git", "commit", "-nam", f"Release {bumped}"])
89+
90+
if arguments.tag:
91+
subprocess.run(["git", "tag", str(bumped), "-m", f"Release {bumped}"])
92+
93+
def test_bump_version():
94+
"""Make sure it all works."""
95+
import pytest
96+
97+
assert bump_version(Version("4.7.0"), "patch") == Version("4.7.1")
98+
assert bump_version(Version("4.7.0"), "minor") == Version("4.8.0")
99+
assert bump_version(Version("4.7.3"), "minor") == Version("4.8.0")
100+
assert bump_version(Version("4.7.0"), "major") == Version("5.0.0")
101+
assert bump_version(Version("4.7.3"), "major") == Version("5.0.0")
102+
assert bump_version(Version("5.0.0"), "major") == Version("6.0.0")
103+
104+
105+
if __name__ == "__main__":
106+
main()

0 commit comments

Comments
 (0)