-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmake_tag.py
executable file
·94 lines (78 loc) · 2.94 KB
/
make_tag.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
#!/usr/bin/env python3
import argparse
import fileinput
import os
import re
import subprocess
import sys
def check_version_format(version):
pattern = re.compile(r'^v?\d+\.\d+\.\d+$')
return pattern.match(version) is not None
def check_tag_exists(tag):
try:
subprocess.check_output(['git', 'rev-parse', tag], stderr=subprocess.STDOUT)
return True
except subprocess.CalledProcessError:
return False
def write_version(version):
print(f'--- Updating version in roc/version.go to {version}')
version_line = re.compile(r'\bbindingsVersion\s*=\s*".*"')
with fileinput.FileInput('roc/version.go', inplace=True) as f:
for line in f:
print(version_line.sub(f'bindingsVersion = "{version}"', line), end='')
def run_command(args):
print(f'--- Running command: {" ".join(args)}')
subprocess.check_call(args)
def check_version_to_commit():
cmd = ['git', 'diff', '--cached', 'roc/version.go']
return subprocess.check_output(cmd) != b''
def commit_change(version):
run_command(['git', 'add', 'roc/version.go'])
if not check_version_to_commit():
print('--- Version did not change, nothing to commit')
return
run_command(['git', 'commit', '-m', f'Release {version}'])
def show_status():
run_command(['git', 'status', '--porcelain'])
def create_tag(tag, force):
if force:
run_command(['git', 'tag', '-f', tag])
else:
run_command(['git', 'tag', tag])
def push_change(remote, tag, force):
run_command(['git', 'push', remote, 'HEAD'])
if force:
run_command(['git', 'push', '-f', remote, tag])
else:
run_command(['git', 'push', remote, tag])
def main():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
parser = argparse.ArgumentParser(description='Create and push tag for new release')
parser.add_argument('--push', metavar='REMOTE', required=False,
help='remote to push (e.g. "origin")')
parser.add_argument('-f', '--force', action='store_true', required=False,
help='force creation of tag, even when already exists')
parser.add_argument('version', metavar='VERSION',
help='version to release ("N.N.N" or "vN.N.N")')
args = parser.parse_args()
version = args.version
if not check_version_format(version):
print(f'Error: version "{version}" is not in correct format,'
' expected "N.N.N" or "vN.N.N"', file=sys.stderr)
sys.exit(1)
version = version.lstrip('v')
remote = args.push
force = args.force
tag = f'v{version}'
if not force and check_tag_exists(tag):
print(f'Error: tag "{tag}" already exists', file=sys.stderr)
sys.exit(1)
write_version(version)
commit_change(version)
show_status()
create_tag(tag, force)
if remote:
push_change(remote, tag, force)
print(f'--- Successfully released {tag}')
if __name__ == '__main__':
main()