summaryrefslogtreecommitdiff
path: root/dev/bump.py
blob: 9bb2138f942f442cea813c73df5a994b2c433053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env python

"""
Cross-platform bump of version with special CHANGELOG modification.
INTENDED TO BE CALLED FROM PROJECT ROOT, NOT FROM dev/!
"""

import subprocess
import sys

try:
    bump_type = sys.argv[1]
except IndexError:
    sys.exit("Must pass 'bump_type' argument!")
else:
    if bump_type not in ("major", "minor", "patch"):
        sys.exit('bump_type must be one of "major", "minor", or "patch"!')


def git(cmd, *args):
    """Wrapper for calling git"""
    try:
        subprocess.run(["git", cmd, *args], check=True, text=True)
    except subprocess.CalledProcessError as e:
        print("Call to git failed!", file=sys.stderr)
        print("STDOUT:", e.stdout, file=sys.stderr)
        print("STDERR:", e.stderr, file=sys.stderr)
        sys.exit(e.returncode)


def bumpversion(severity, *args, catch=False):
    """Wrapper for calling bumpversion"""
    cmd = ["bump2version", *args, severity]
    try:
        if catch:
            return subprocess.run(
                cmd, check=True, capture_output=True, text=True
            ).stdout
        else:
            subprocess.run(cmd, check=True, text=True)
    except subprocess.CalledProcessError as e:
        print("Call to bump2version failed!", file=sys.stderr)
        print("STDOUT:", e.stdout, file=sys.stderr)
        print("STDERR:", e.stderr, file=sys.stderr)
        sys.exit(e.returncode)


# Do a dry run of the bump to find what the current version is and what it will become.
data = bumpversion(bump_type, "--dry-run", "--list", catch=True)
data = dict(x.split("=") for x in data.splitlines())

# Execute the bumpversion.
bumpversion(bump_type)

# Post-process the changelog with things that bumpversion is not good at updating.
with open("CHANGELOG.md") as fl:
    changelog = fl.read().replace(
        "<!---Comparison links-->",
        "<!---Comparison links-->\n[{new}]: {url}/{current}...{new}".format(
            new=data["new_version"],
            current=data["current_version"],
            url="https://github.com/SethMMorton/natsort/compare",
        ),
    )
with open("CHANGELOG.md", "w") as fl:
    fl.write(changelog)

# Finally, add the CHANGELOG.md changes to the previous commit.
git("add", "CHANGELOG.md")
git("commit", "--amend", "--no-edit")
git("tag", "--force", data["new_version"], "HEAD")