-
Notifications
You must be signed in to change notification settings - Fork 8
/
update_version.py
96 lines (78 loc) · 3.11 KB
/
update_version.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
#! python
from subprocess import call
import subprocess
import fileinput
import re
import os
import sys
import getopt
if len(sys.argv) < 2:
print("usage: ./update_version.py --[major/minor/revision]")
print("e.g: ./update_version.py --revision")
sys.exit(1)
origPath = os.getcwd();
version_level = sys.argv[1];
valid_version_levels = ['--major', '--minor', '--revision']
if version_level not in valid_version_levels:
print('Version level "{}" not recognised, must be one of:'.format(version_level))
print(valid_version_levels)
sys.exit(1)
def safeProcess( cmd ):
"Run command, return boolean for success"
print(cmd);
try:
out = subprocess.check_output(cmd, shell=True)
print(out.decode("utf-8").replace("\\\n", "\n"))
return True;
except subprocess.CalledProcessError as e:
print("Status : FAIL", e.returncode, e.output)
return False;
def safeExit():
print ("Exiting...")
os.chdir(origPath)
sys.exit(1)
# safeProcess("git reset --hard head")
def updateModVersion(filename, version_level):
pattern = re.compile("(\s*modversion\s*=\s*)(\d*).(\d*).(\d*)");
new_version = None
for line in fileinput.input(filename, inplace=True):
if re.search(pattern, line):
major = int(pattern.search(line).groups()[1])
minor = int(pattern.search(line).groups()[2])
revision = int(pattern.search(line).groups()[3])
if version_level == "--major":
major += 1
minor = 0
revision = 0
elif version_level == "--minor":
minor += 1
revision = 0
elif version_level == "--revision":
revision += 1
new_version = "{}.{}.{}".format(major, minor, revision)
line = re.sub(pattern, "modversion={}".format(new_version), line);
print(line.rstrip('\n'))
if not new_version:
raise Exception("Could not find version in file {}".format(filename))
return new_version;
def updateVersionGradle(filename, new_version):
# This updates the version in build.gradle. This file is
# not within our repo, so beware!
#e.g. version = "1.0.3"
pattern = re.compile('version\s*=\s*"(\d*.\d*.\d*)"');
for line in fileinput.input(filename, inplace=True):
if re.search(pattern, line):
curr_version = pattern.search(line).groups()[0]
line = line.replace(curr_version, new_version)
print(line.rstrip('\n'))
return new_version
# Don't continue if working copy is dirty
if not safeProcess('git diff-index --quiet HEAD --'):
print( "Cannot continue, git working copy dirty")
safeExit()
# Make sure gradle knows about the version
gradle_file = 'gradle.properties'
new_version = updateModVersion(gradle_file, version_level)
# Commit changes
safeProcess("git add {}".format(gradle_file))
safeProcess('git commit -m "Update version number to ' + new_version + '"')