summaryrefslogtreecommitdiff
path: root/release.py
blob: 2556d50940be6932244ccdd0c08aff0708f41c2d (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
82
83
84
85
86
87
88
89
90
91
92
import re
from glob import glob
from os.path import join
from subprocess import call

import blurb as blurb_module
from argparse import ArgumentParser
from mock import version_info

VERSION_TYPES = ['major', 'minor', 'bugfix']


def incremented_version(version_info, type_):
    type_index = VERSION_TYPES.index(type_)
    version_info = tuple(e+(1 if i==type_index else 0)
                         for i, e in enumerate(version_info))
    return '.'.join(str(p) for p in version_info)


def text_from_news():
    # hack:
    blurb_module.sections.append('NEWS.d')

    blurbs = blurb_module.Blurbs()
    for path in glob(join('NEWS.d', '*')):
        blurbs.load_next(path)

    text = []
    for metadata, body in blurbs:
        bpo = metadata['bpo']
        body = f"- Issue #{bpo}: " + body
        text.append(blurb_module.textwrap_body(body, subsequent_indent='  '))

    return '\n'.join(text)


def news_to_changelog(version):
    with open('CHANGELOG.rst') as source:
        current_changelog = source.read()

    text = [version]
    text.append('-'*len(version))
    text.append('')
    text.append(text_from_news())
    text.append(current_changelog)

    new_changelog = '\n'.join(text)
    with open('CHANGELOG.rst', 'w') as target:
        target.write(new_changelog)


def update_version(new_version):
    path = join('mock', 'mock.py')
    with open(path) as source:
        text = source.read()

    text = re.sub("(__version__ = ')[^']+(')",
                  r"\g<1>"+new_version+r"\2",
                  text)

    with open(path, 'w') as target:
        target.write(text)


def git(command):
    return call('git '+command, shell=True)


def git_commit(new_version):
    git('rm NEWS.d/*')
    git('add CHANGELOG.rst')
    git('add mock/mock.py')
    git(f'commit -m "Preparing for {new_version} release."')


def parse_args():
    parser = ArgumentParser()
    parser.add_argument('type', choices=VERSION_TYPES)
    return parser.parse_args()


def main():
    args = parse_args()
    new_version = incremented_version(version_info, args.type)
    news_to_changelog(new_version)
    update_version(new_version)
    git_commit(new_version)
    print(f'{new_version} ready to push, please check the HEAD commit first!')


if __name__ == '__main__':
    main()