forked from pplante/droidtowers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-release.py
executable file
·150 lines (105 loc) · 5.53 KB
/
build-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
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
#! /usr/bin/env python
import re
import sys
import semver
import requests
import json
import tempfile
from datetime import datetime
from getpass import getpass
from pbs import git, ant, scp, pwd, cd, mkdir
TOWER_CONSTS_JAVA = './main/source/com/happydroids/HappyDroidConsts.java'
GOOGLE_MANIFEST_PATH = './android-google/AndroidManifest.xml'
AMAZON_MANIFEST_PATH = './android-amazon/AndroidManifest.xml'
debug_flag_re = re.compile(r'(boolean DEBUG = (?:true|false);)')
server_url_re = re.compile(r'(String HAPPYDROIDS_SERVER = "(?:.+?)";)')
server_https_re = re.compile(r'(String HAPPYDROIDS_URI = "(?:.+?)" \+ HAPPYDROIDS_SERVER;)')
version_re = re.compile(r'(String VERSION = "(?:.+?)";)')
version_code_re = re.compile(r'(int VERSION_CODE = \d+;)')
git_sha_re = re.compile(r'(String GIT_SHA = "(?:.+?)";)')
android_manifest_re = re.compile(r'package="com.happydroids.droidtowers"\s+android:versionCode="\d+"\s+android:versionName=".+?">')
def retrieve_git_revision():
git_status = git.status('--porcelain').strip()
if len(git_status) > 0:
print("Git status reports there are changes to be committed.")
if raw_input("Continue? [y/n]") == 'n':
sys.exit(1)
return git('rev-parse', 'HEAD').strip()
def retreive_build_number():
return open('build.ver').read()
def git_check_for_tag_collision(new_build_number):
existing_tags = git.tag().split('\n')
for tag in existing_tags:
if tag == new_build_number:
raise Exception('Build number has already been used as a tag in git, aborting.')
def prompt_for_new_build_number(previous_build_number):
while True:
try:
input_ver = raw_input('Please provide build number: ')
if semver.from_string(input_ver) <= previous_build_number:
raise semver.SemanticVersionError('\tVersion cannot be less than: %s' % previous_build_number)
return input_ver
except semver.SemanticVersionError, e:
print e
if __name__ == '__main__':
try:
root_dir = pwd().strip()
revision = retrieve_git_revision()
previous_build_number = semver.from_string(retreive_build_number())
print 'Previous build: ', previous_build_number
new_build_number = '%d.%d.%d' % (
previous_build_number.major, previous_build_number.minor, previous_build_number.patchlevel + 1)
if raw_input('Use "%s" as next build number? (yes) ' % (new_build_number,)) != 'yes':
new_build_number = prompt_for_new_build_number(previous_build_number)
git_check_for_tag_collision(new_build_number)
build_version_code = new_build_number.replace('.', '')
tower_consts = open(TOWER_CONSTS_JAVA).read()
tower_consts = debug_flag_re.sub('boolean DEBUG = false;', tower_consts)
tower_consts = server_https_re.sub('String HAPPYDROIDS_URI = "https://" + HAPPYDROIDS_SERVER;', tower_consts)
tower_consts = server_url_re.sub('String HAPPYDROIDS_SERVER = "www.happydroids.com";', tower_consts)
tower_consts = version_re.sub('String VERSION = "%s";' % (new_build_number,), tower_consts)
tower_consts = version_code_re.sub('int VERSION_CODE = %s;' % (build_version_code,), tower_consts)
tower_consts = git_sha_re.sub('String GIT_SHA = "%s";' % (revision,), tower_consts)
google_manifest = open(GOOGLE_MANIFEST_PATH).read().strip()
google_manifest = android_manifest_re.sub('package="com.happydroids.droidtowers" android:versionCode="%s" android:versionName="%s">' %(build_version_code, new_build_number), google_manifest)
amazon_manifest = open(AMAZON_MANIFEST_PATH).read().strip()
amazon_manifest = android_manifest_re.sub('package="com.happydroids.droidtowers" android:versionCode="%s" android:versionName="%s">' %(build_version_code, new_build_number), amazon_manifest)
print tower_consts
if raw_input('Is this correct? [y/n]') == 'n':
print "\n\nABORTED!\n\n"
sys.exit(1)
print google_manifest
if raw_input('Is this correct? [y/n]') == 'n':
print "\n\nABORTED!\n\n"
sys.exit(1)
print amazon_manifest
if raw_input('Is this correct? [y/n]') == 'n':
print "\n\nABORTED!\n\n"
sys.exit(1)
with open(TOWER_CONSTS_JAVA, 'w+') as fp:
fp.write(tower_consts)
with open(GOOGLE_MANIFEST_PATH, 'w+') as fp:
fp.write(google_manifest)
with open(AMAZON_MANIFEST_PATH, 'w+') as fp:
fp.write(amazon_manifest)
with open('build.ver', 'w') as fp:
fp.write(new_build_number)
with open('release.properties', 'w') as fp:
fp.write('project.version=%s' % (new_build_number))
ant('release', _fg=True)
cd('%s/android-google' % (root_dir,))
ant('release', _fg=True)
cd('%s/android-amazon' % (root_dir,))
ant('release', _fg=True)
cd(root_dir)
git.commit(a=True, m='Artifacts from release-%s' % (new_build_number,))
git.tag('release-%s' % (new_build_number,))
tower_consts = open(TOWER_CONSTS_JAVA).read()
tower_consts = debug_flag_re.sub('boolean DEBUG = true;', tower_consts)
tower_consts = server_url_re.sub('String HAPPYDROIDS_SERVER = "local.happydroids.com";', tower_consts)
tower_consts = server_https_re.sub('String HAPPYDROIDS_URI = "http://" + HAPPYDROIDS_SERVER;', tower_consts)
with open(TOWER_CONSTS_JAVA, 'w') as fp:
fp.write(tower_consts)
except Exception, e:
print e
sys.exit(1)