-
Notifications
You must be signed in to change notification settings - Fork 107
/
release.py
94 lines (67 loc) · 2.35 KB
/
release.py
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
93
94
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(0 if i>type_index else (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.get('bpo')
gh = metadata.get('gh-issue')
issue = f'bpo-{bpo}' if bpo else f'gh-{gh}'
body = f"- {issue}: " + 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', '__init__.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/__init__.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()