-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathmake-release.py
executable file
·209 lines (159 loc) · 6.19 KB
/
make-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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
make-release
~~~~~~~~~~~~
Helper script that performs a release. Does pretty much everything
automatically for us.
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import sys
import os
import re
from datetime import datetime, date
from subprocess import Popen, PIPE
_date_clean_re = re.compile(r'(\d+)(st|nd|rd|th)')
def add_new_changelog_section(current_version, next_version):
version_string = 'Version {}'.format(current_version)
with open('CHANGES') as f:
all_lines = f.readlines()
stripped_lines = [l.strip() for l in all_lines]
try:
# get the index of the first occurrence of `version_string`
line_num = stripped_lines.index(version_string)
except:
fail('Could not find "{}" in {}.'.format(version_string, 'CHANGES'))
new_header = 'Version {}'.format(next_version)
horizontal_rule = '-' * len(new_header)
new_lines = [new_header + '\n', horizontal_rule + '\n', '\n',
'Not yet released.' + '\n', '\n']
# insert the new lines into the list of all lines read from CHANGES
all_lines[line_num:line_num] = new_lines
# write the changes back to...CHANGES
with open('CHANGES', 'w') as f:
f.writelines(all_lines)
def parse_changelog():
with open('CHANGES') as f:
lineiter = iter(f)
for line in lineiter:
match = re.search('^Version\s+(.*)', line.strip())
if match is None:
continue
# length = len(match.group(1))
version = match.group(1).strip()
if lineiter.next().count('-') != len(match.group(0)):
continue
while 1:
change_info = lineiter.next().strip()
if change_info:
break
match = re.search(r'Released on (\w+\s+\d+,\s+\d+)',
change_info)
if match is None:
continue
datestr = match.groups()[0]
return version, parse_date(datestr)
def bump_version(version):
try:
parts = map(int, version.split('.'))
except ValueError:
fail('Current version is not numeric')
if sys.argv[1] == 'major':
parts = [parts[0] + 1, 0, 0]
elif sys.argv[1] == 'minor':
parts = [parts[0], parts[1] + 1, 0]
else:
parts[-1] += 1
return '.'.join(map(str, parts))
def parse_date(string):
string = _date_clean_re.sub(r'\1', string)
return datetime.strptime(string, '%B %d, %Y')
def set_filename_version(filename, version_number, pattern):
changed = []
def inject_version(match):
before, old, after = match.groups()
changed.append(True)
return before + version_number + after
with open(filename) as f:
contents = re.sub(r"^(\s*%s\s*=\s*')(.+?)(')(?sm)" % pattern,
inject_version, f.read())
if not changed:
fail('Could not find %s in %s', pattern, filename)
with open(filename, 'w') as f:
f.write(contents)
def set_init_version(version):
info('Setting __init__.py version to %s', version)
set_filename_version('flask_restless/__init__.py', version, '__version__')
def set_setup_version(version):
info('Setting setup.py version to %s', version)
set_filename_version('setup.py', version, 'version')
def build():
Popen([sys.executable, 'setup.py', 'sdist', 'bdist_wheel']).wait()
def sign(version):
bdist_wheel = 'dist/Flask_Restless-{0}-py2.py3-none-any.whl'
bdist_wheel = bdist_wheel.format(version)
sdist = 'dist/Flask-Restless-{0}.tar.gz'.format(version)
Popen(['gpg', '--detach-sign', '-a', bdist_wheel]).wait()
Popen(['gpg', '--detach-sign', '-a', sdist]).wait()
def upload(version):
bdist_wheel = 'dist/Flask_Restless-{0}-py2.py3-none-any.whl'
bdist_wheel = bdist_wheel.format(version)
bdist_wheel_signature = '{0}.asc'.format(bdist_wheel)
sdist = 'dist/Flask-Restless-{0}.tar.gz'.format(version)
sdist_signature = '{0}.asc'.format(bdist_wheel)
files = [sdist, sdist_signature, bdist_wheel, bdist_wheel_signature]
Popen(['twine', 'upload'] + files).wait()
def fail(message, *args):
print >> sys.stderr, 'Error:', message % args
sys.exit(1)
def info(message, *args):
print >> sys.stderr, message % args
def get_git_tags():
process = Popen(['git', 'tag'], stdout=PIPE)
return set(process.communicate()[0].splitlines())
def git_is_clean():
return Popen(['git', 'diff', '--quiet']).wait() == 0
def make_git_commit(message, *args):
message = message % args
Popen(['git', 'commit', '-am', message]).wait()
def make_git_tag(tag):
info('Tagging "%s"', tag)
msg = '"Released version {}"'.format(tag)
Popen(['git', 'tag', '-s', '-m', msg, tag]).wait()
def main():
os.chdir(os.path.join(os.path.dirname(__file__), '..'))
rv = parse_changelog()
if rv is None:
fail('Could not parse changelog')
version, release_date = rv
dev_version = bump_version(version) + '-dev'
info('Releasing %s (release date %s)',
version, release_date.strftime('%d/%m/%Y'))
tags = get_git_tags()
if version in tags:
fail('Version "%s" is already tagged', version)
if release_date.date() != date.today():
fail('Release date is not today (%s != %s)')
if not git_is_clean():
fail('You have uncommitted changes in git')
set_init_version(version)
# set_setup_version(version)
make_git_commit('Bump version number to %s', version)
make_git_tag(version)
build()
sign(version)
upload(version)
set_init_version(dev_version)
# set_setup_version(dev_version)
add_new_changelog_section(version, dev_version)
make_git_commit('Set development version number to %s', dev_version)
print('*************************************')
print('* *')
print('* Now run *')
print('* *')
print('* git push --tags origin master *')
print('* *')
print('*************************************')
if __name__ == '__main__':
main()