summaryrefslogtreecommitdiff
path: root/tools/finalize.py
blob: 98b06c070c22e4385af36026e4a435c9c89c6fd4 (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
72
73
74
75
76
77
78
79
80
81
"""
Finalize the repo for a release. Invokes towncrier and bumpversion.
"""

__requires__ = ['bump2version', 'towncrier']


import subprocess
import pathlib
import re
import sys


def release_kind():
    """
    Determine which release to make based on the files in the
    changelog.
    """
    # use min here as 'major' < 'minor' < 'patch'
    return min(
        'major' if 'breaking' in file.name else
        'minor' if 'change' in file.name else
        'patch'
        for file in pathlib.Path('changelog.d').iterdir()
    )


bump_version_command = [
    sys.executable,
    '-m', 'bumpversion',
    release_kind(),
]


def get_version():
    cmd = bump_version_command + ['--dry-run', '--verbose']
    out = subprocess.check_output(cmd, text=True)
    return re.search('^new_version=(.*)', out, re.MULTILINE).group(1)


def update_changelog():
    cmd = [
        sys.executable, '-m',
        'towncrier',
        '--version', get_version(),
        '--yes',
    ]
    subprocess.check_call(cmd)


def bump_version():
    cmd = bump_version_command + ['--allow-dirty']
    subprocess.check_call(cmd)


def ensure_config():
    """
    Double-check that Git has an e-mail configured.
    """
    subprocess.check_output(['git', 'config', 'user.email'])


def check_changes():
    """
    Verify that all of the files in changelog.d have the appropriate
    names.
    """
    allowed = 'deprecation', 'breaking', 'change', 'doc', 'misc'
    assert all(
        any(key in file.name for key in allowed)
        for file in pathlib.Path('changelog.d').iterdir()
        if file.name != '.gitignore'
    )


if __name__ == '__main__':
    print("Cutting release at", get_version())
    ensure_config()
    check_changes()
    update_changelog()
    bump_version()