-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDetermineNextTag.py
62 lines (47 loc) · 1.85 KB
/
DetermineNextTag.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
# Script to determine highest tag number and increment version
from __future__ import print_function
from distutils.version import StrictVersion
import subprocess
import sys
def DetermineHighestTagVersion ():
# retrive all tags
cmd = 'git tag'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
tags = []
for line in p.stdout.readlines():
line = line.decode()
line = line.strip(' /\r\n') # remove '/' & newline suffix
tags.append(line)
# identify newest tag
newest_tag = tags[0]
for tag in tags:
try:
if StrictVersion(tag) > StrictVersion(newest_tag):
newest_tag = tag
except:
print('Ignoring unparsable tag '+tag, file=sys.stderr)
return newest_tag
def IncrementVersionNumber (version, incr_idx):
# tokenize version
tokens = version.split('.')
# append zero tokens to avoid out-of-bounds access
while incr_idx >= len(tokens):
tokens.append('0')
# increment token
tokens[incr_idx] = str(int(tokens[incr_idx])+1)
# set lower version indices to zero (eg. go from '0.9' to '1.0')
for i in range(incr_idx+1, len(tokens)):
tokens[i] = '0'
# joint tokens into new version string
return '.'.join(tokens)
if __name__ == "__main__":
incr_type = sys.argv[1] # version increment type (major, minor, patch)
# determine which version index to increment
if incr_type == 'major': incr_idx = 0
elif incr_type == 'minor': incr_idx = 1
elif incr_type == 'patch': incr_idx = 2
else: raise Exception('unknown incr_type')
prev_ver = DetermineHighestTagVersion()
new_ver = IncrementVersionNumber(prev_ver, incr_idx)
# output new version number to stdout, without newline suffix
sys.stdout.write(new_ver)