-
Notifications
You must be signed in to change notification settings - Fork 0
/
release
executable file
·324 lines (252 loc) · 7.29 KB
/
release
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
# encoding: utf-8
"""
release.py
Created by Thomas Mangin on 2011-01-24.
Copyright (c) 2009-2017 Exa Networks. All rights reserved.
"""
import os
import sys
import fileinput
CHANGELOG = 'CHANGELOG'
class Path:
root = os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]))
changelog = os.path.join(root,CHANGELOG)
lib_exa = os.path.join(root, 'lib/exabgp')
version = os.path.join(root, 'lib/exabgp/version.py')
debian = os.path.join(root, 'debian/changelog')
egg = os.path.join(root, 'lib/exabgp.egg-info')
build_exabgp = os.path.join(root, 'build/lib/exabgp')
build_root = os.path.join(root, 'build')
@classmethod
def remove_egg(cls):
from shutil import rmtree
print('removing left-over egg')
if os.path.exists(cls.egg):
rmtree(cls.egg)
if os.path.exists(cls.build_exabgp):
rmtree(cls.build_root)
return 0
class Version:
template = """\
version = "%s"
# Do not change the first line as it is parsed by scripts
if __name__ == '__main__':
import sys
sys.stdout.write(version)
"""
@staticmethod
def current():
sys.path.append(Path.lib_exa)
from version import version as release
# transitional fix
if "-" in release:
release = release.split("-")[0]
return release
@staticmethod
def changelog():
with open(Path.changelog) as f:
f.readline()
for line in f:
if line.lower().startswith('version '):
return line.split()[1].rstrip().rstrip(':').strip()
return ''
@staticmethod
def set(tag,commit):
with open(Path.version, 'w') as f:
f.write(Version.template % (
tag
))
return Version.current() == tag
@staticmethod
def latest (tags):
valid = [
[int(_) for _ in tag.split('.')] for tag in tags
if Version.valid(tag) and tag.startswith('3.')
]
return '.'.join(str(_) for _ in sorted(valid)[-1])
@staticmethod
def valid (tag):
parts = tag.split('.')
return len(parts) == 3 \
and parts[0].isdigit() \
and parts[1].isdigit() \
and parts[2].isdigit()
@staticmethod
def candidates(tag):
latest = [int(_) for _ in tag.split('.')]
return [
'.'.join([str(_) for _ in (latest[0], latest[1], latest[2] + 1)]),
'.'.join([str(_) for _ in (latest[0], latest[1] + 1, 0)]),
'.'.join([str(_) for _ in (latest[0] + 1, 0, 0)]),
]
class Debian:
template = """\
exabgp (%s-0) unstable; urgency=low
* Latest ExaBGP release.
-- Vincent Bernat <bernat@debian.org> %s
"""
@staticmethod
def set(version):
from email.utils import formatdate
with open(Path.debian, 'w') as w:
w.write(Debian.template % (version, formatdate()))
print('updated debian/changelog')
class Command:
dryrun = 'dry-run' if os.environ.get('DRY', os.environ.get('DRYRUN', os.environ.get('DRY_RUN', False))) else ''
@staticmethod
def run(cmd):
print('>', cmd)
return Git.dryrun or os.system(cmd)
class Git (Command):
@staticmethod
def commit (comment):
return Git.run('git commit -a -m "%s"' % comment)
@staticmethod
def push(tag=False,repo=''):
command = 'git push'
if tag:
command += ' --tags'
if repo:
command += ' %s' % repo
return Git.run(command)
@staticmethod
def head_commit():
return os.popen('git rev-parse --short HEAD').read().strip()
@staticmethod
def tags():
return os.popen('git tag').read().split('-')[0].strip().split('\n')
@staticmethod
def tag(release):
return Git.run('git tag -a %s -m "release %s"' % (release, release))
@staticmethod
def pending():
# XXX: terrible please rewrite
commit = None
for line in os.popen('git status').read().split('\n'):
if 'modified:' in line:
if 'lib/exabgp/version.py' in line or 'debian/changelog' in line or 'README' in line:
if commit is not False:
commit = True
else:
return False
elif 'renamed:' in line:
return False
return commit
#
# Check that that there is no version inconsistancy before any pypi action
#
def release_github():
print()
print('updating Github')
current = Version.current()
print('current version is %s (from version.py)' % current)
release = Version.changelog()
print('release version is %s (from %s)' % (release, CHANGELOG))
if not Version.valid(release):
print('invalid new version in %s' % CHANGELOG)
return 1
tags = Git.tags()
if release in tags:
print('this version/tag was already released/used')
return 1
candidates = Version.candidates(Version.latest(tags))
if release not in candidates:
print('valid versions are:', ', '.join(candidates))
print('this release is not one of the candidates')
print('updating lib/exabgp/version.py')
Version.set(release, Git.head_commit())
# print('updating debian/changelog')
# Debian.set(release)
# print('updating ', end='')
# for readme in ('README.md', 'README.rst'):
# print(readme + ' ', end='')
# with fileinput.FileInput(readme, inplace=True) as replacer:
# for line in replacer:
# print(line.replace(current, release), end='')
# print()
print('checking if we need to commit a version.py change')
status = Git.pending()
if status is None:
print('all is already set for release')
elif status is False:
print('more than one file is modified and need updating, aborting')
return 1
else:
if Git.commit('updating version to %s' % release):
print('could not commit version change (%s)' % release)
return 1
print('version was updated')
print('tagging the new version')
if Git.tag(release):
print('could not tag version (%s)' % release)
return 1
print('pushing the new tag')
if Git.push(tag=True, repo='origin'):
print('could not push release tag to origin')
return 1
if Git.push(tag=False, repo='origin'):
print('could not push release version to origin')
return 1
if Git.push(tag=True, repo='upstream'):
print('could not push release tag to upstream')
return 1
if Git.push(tag=False, repo='upstream'):
print('could not push release version to upstream')
return 1
return 0
def release_pypi(test):
print()
print('updating PyPI')
Path.remove_egg()
if Command.run('python setup.py sdist bdist_wheel'):
print('could not generate egg')
return 1
# keyring used to save credential
# https://pypi.org/project/twine/
release = Version.latest(Git.tags())
server = ''
if test:
server = '--repository-url https://test.pypi.org/legacy/'
if Command.run('twine upload %s dist/exabgp-%s.tar.gz' % (server, release)):
print('could not upload with twine')
return 1
print('all done.')
return 0
def help():
print("""\
release help this help
release cleanup delete left-over file from release
release github tag a new version on github, and update pypi
release pypi create egg/wheel
release install local installation
""")
def main ():
if os.environ.get("SCRUTINIZER", "") == "true":
sys.exit(0)
if len(sys.argv) < 2:
help()
sys.exit(1)
if sys.argv[1] == 'cleanup':
sys.exit(Path.remove_egg())
if sys.argv[1] == 'github':
sys.exit(release_github())
if sys.argv[1] == 'pypi':
sys.exit(release_pypi('--test' in sys.argv or 'test' in sys.argv))
# "internal" commands
if sys.argv[1] == 'current':
sys.stdout.write("%s\n" % Version.current())
sys.exit(0)
if sys.argv[1] == 'release':
sys.stdout.write("%s\n" % Version.changelog())
sys.exit(0)
if '--help' in sys.argv or 'help' in sys.argv:
help()
sys.exit(1)
if sys.argv[1] == 'debian':
release = Version.changelog()
Debian.set(release)
sys.exit(0)
sys.exit(1)
if __name__ == '__main__':
main()