|
| 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