forked from nuclio/nuclio-jupyter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cut_release.py
executable file
·79 lines (61 loc) · 2.34 KB
/
cut_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
#!/usr/bin/env python
# Copyright 2018 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Cut a release"""
import re
from argparse import ArgumentParser
from subprocess import run
from os import environ
from sys import executable
is_valid_version = re.compile(r'\d+\.\d+\.\d+$').match
init_file = 'nuclio/__init__.py'
def git_branch():
branch = environ.get('TRAVIS_BRANCH')
if branch:
return branch
cmd = ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
out = run(cmd, capture_output=True)
if out.returncode != 0:
return ''
return out.stdout.decode('utf-8').strip()
def change_version(version):
with open(init_file) as fp:
data = fp.read()
# __version__ = '0.3.0'
new_version = '__version__ = {!r}'.format(version)
data = re.sub(r'__version__\s*=.*', new_version, data)
with open(init_file, 'w') as out:
out.write(data)
def next_version():
cmd = [executable, 'setup.py', '--version']
version = run(cmd, check=True, capture_output=True).stdout.decode()
version = [int(v) for v in version.split('.')]
version[-1] += 1
return '.'.join(map(str, version))
if __name__ == '__main__':
parser = ArgumentParser(description=__doc__)
parser.add_argument('version', help='version (use + for next)')
args = parser.parse_args()
if git_branch() != 'master':
raise SystemExit('error: not on "master" branch')
version = next_version() if args.version == '+' else args.version
if not is_valid_version(version):
raise SystemExit('error: bad version (should be major.minor.patch)')
print(f'setting version to {version}')
change_version(version)
run(['git', 'add', init_file])
run(['git', 'commit', '-m', 'version {}'.format(version)])
run(['git', 'tag', 'v{}'.format(version)])
run(['git', 'push'])
run(['git', 'push', '--tags'])