2011-02-17 21:16:26 +01:00
|
|
|
#!/usr/bin/env python2
|
2011-01-26 17:26:51 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-11-11 01:03:39 +01:00
|
|
|
#
|
|
|
|
# build.py - part of the FDroid server tools
|
2011-08-26 00:50:15 +02:00
|
|
|
# Copyright (C) 2010-11, Ciaran Gultnieks, ciaran@ciarang.com
|
2010-11-11 01:03:39 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import zipfile
|
2011-01-03 00:26:12 +01:00
|
|
|
import tarfile
|
2010-11-11 01:03:39 +01:00
|
|
|
import md5
|
2011-01-08 23:31:55 +01:00
|
|
|
import shlex
|
2010-11-11 01:03:39 +01:00
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2011-02-17 21:16:26 +01:00
|
|
|
import common
|
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
#Read configuration...
|
|
|
|
execfile('config.py')
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
# Parse command line...
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-v", "--verbose", action="store_true", default=False,
|
|
|
|
help="Spew out even more information than normal")
|
2010-11-13 15:11:48 +01:00
|
|
|
parser.add_option("-p", "--package", default=None,
|
|
|
|
help="Build only the specified package")
|
2010-11-11 01:03:39 +01:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
# Get all apps...
|
2011-04-26 01:27:12 +02:00
|
|
|
apps = common.read_metadata(options.verbose)
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2011-08-26 00:50:15 +02:00
|
|
|
output_dir = "repo"
|
|
|
|
tmp_dir = "tmp"
|
|
|
|
if not os.path.isdir('tmp'):
|
|
|
|
os.makedirs('tmp')
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2011-06-16 23:13:22 +02:00
|
|
|
if not os.path.isdir('build'):
|
|
|
|
os.makedirs('build')
|
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
for app in apps:
|
|
|
|
|
2011-06-16 23:08:07 +02:00
|
|
|
if app['disabled']:
|
|
|
|
print "Skipping %s: disabled" % app['id']
|
|
|
|
elif not app['builds']:
|
|
|
|
print "Skipping %s: no builds specified" % app['id']
|
|
|
|
|
2010-11-13 15:11:48 +01:00
|
|
|
if (app['disabled'] is None and app['repo'] != ''
|
|
|
|
and app['repotype'] != '' and (options.package is None or
|
2011-01-05 00:58:44 +01:00
|
|
|
options.package == app['id']) and len(app['builds']) > 0):
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2011-01-05 00:58:44 +01:00
|
|
|
print "Processing " + app['id']
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2011-06-16 23:13:22 +02:00
|
|
|
build_dir = 'build/' + app['id']
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2011-08-07 17:14:54 +02:00
|
|
|
# Set up vcs interface and make sure we have the latest code...
|
|
|
|
vcs = common.getvcs(app['repotype'], app['repo'], build_dir)
|
|
|
|
|
|
|
|
refreshed_source = False
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
for thisbuild in app['builds']:
|
|
|
|
|
2011-08-26 00:50:15 +02:00
|
|
|
dest = os.path.join(output_dir, app['id'] + '_' +
|
2011-01-05 00:58:44 +01:00
|
|
|
thisbuild['vercode'] + '.apk')
|
2011-08-26 00:50:15 +02:00
|
|
|
dest_unsigned = os.path.join(tmp_dir, app['id'] + '_' +
|
|
|
|
thisbuild['vercode'] + '_unsigned.apk')
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2011-01-05 00:58:44 +01:00
|
|
|
if os.path.exists(dest):
|
2011-03-02 22:47:48 +01:00
|
|
|
print "..version " + thisbuild['version'] + " already exists"
|
|
|
|
elif thisbuild['commit'].startswith('!'):
|
|
|
|
print ("..skipping version " + thisbuild['version'] + " - " +
|
|
|
|
thisbuild['commit'][1:])
|
2010-11-12 19:22:06 +01:00
|
|
|
else:
|
2011-03-02 22:47:48 +01:00
|
|
|
print "..building version " + thisbuild['version']
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-08-07 17:14:54 +02:00
|
|
|
if not refreshed_source:
|
|
|
|
vcs.refreshlocal()
|
|
|
|
refreshed_source = True
|
2011-01-03 11:55:18 +01:00
|
|
|
|
2011-01-09 22:54:55 +01:00
|
|
|
# Optionally, the actual app source can be in a subdirectory...
|
|
|
|
if thisbuild.has_key('subdir'):
|
2011-08-07 17:14:54 +02:00
|
|
|
root_dir = os.path.join(build_dir, thisbuild['subdir'])
|
2011-01-09 22:54:55 +01:00
|
|
|
else:
|
|
|
|
root_dir = build_dir
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-08-07 17:14:54 +02:00
|
|
|
# Get a working copy of the right revision...
|
|
|
|
vcs.reset(thisbuild['commit'])
|
2011-01-09 22:54:55 +01:00
|
|
|
|
2011-01-16 00:24:41 +01:00
|
|
|
# Initialise submodules if requred...
|
2011-03-03 23:41:45 +01:00
|
|
|
if thisbuild.get('submodules', 'no') == 'yes':
|
2011-08-07 17:14:54 +02:00
|
|
|
vcs.initsubmodules()
|
2011-01-16 00:24:41 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Generate (or update) the ant build file, build.xml...
|
2011-04-27 11:03:43 +02:00
|
|
|
if (thisbuild.get('update', 'yes') == 'yes' and
|
|
|
|
not thisbuild.has_key('maven')):
|
2011-04-06 20:55:27 +02:00
|
|
|
parms = [os.path.join(sdk_path, 'tools', 'android'),
|
|
|
|
'update', 'project', '-p', '.']
|
2011-01-10 08:59:41 +01:00
|
|
|
parms.append('--subprojects')
|
|
|
|
if thisbuild.has_key('target'):
|
|
|
|
parms.append('-t')
|
|
|
|
parms.append(thisbuild['target'])
|
|
|
|
if subprocess.call(parms, cwd=root_dir) != 0:
|
|
|
|
print "Failed to update project"
|
|
|
|
sys.exit(1)
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# If the app has ant set up to sign the release, we need to switch
|
|
|
|
# that off, because we want the unsigned apk...
|
2011-02-27 00:40:20 +01:00
|
|
|
for propfile in ('build.properties', 'default.properties'):
|
|
|
|
if os.path.exists(os.path.join(root_dir, propfile)):
|
|
|
|
if subprocess.call(['sed','-i','s/^key.store/#/',
|
|
|
|
propfile], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend %s" % propfile
|
|
|
|
sys.exit(1)
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Update the local.properties file...
|
|
|
|
locprops = os.path.join(root_dir, 'local.properties')
|
|
|
|
if os.path.exists(locprops):
|
|
|
|
f = open(locprops, 'r')
|
|
|
|
props = f.read()
|
|
|
|
f.close()
|
|
|
|
# Fix old-fashioned 'sdk-location' by copying
|
|
|
|
# from sdk.dir, if necessary...
|
2011-03-03 23:41:45 +01:00
|
|
|
if thisbuild.get('oldsdkloc', 'no') == "yes":
|
2011-01-10 08:59:41 +01:00
|
|
|
sdkloc = re.match(r".*^sdk.dir=(\S+)$.*", props,
|
|
|
|
re.S|re.M).group(1)
|
|
|
|
props += "\nsdk-location=" + sdkloc + "\n"
|
|
|
|
# Add ndk location...
|
|
|
|
props+= "\nndk.dir=" + ndk_path + "\n"
|
2011-01-26 17:02:30 +01:00
|
|
|
# Add java.encoding if necessary...
|
|
|
|
if thisbuild.has_key('encoding'):
|
|
|
|
props += "\njava.encoding=" + thisbuild['encoding'] + "\n"
|
2011-01-10 08:59:41 +01:00
|
|
|
f = open(locprops, 'w')
|
|
|
|
f.write(props)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Insert version code and number into the manifest if necessary...
|
|
|
|
if thisbuild.has_key('insertversion'):
|
|
|
|
if subprocess.call(['sed','-i','s/' + thisbuild['insertversion'] +
|
|
|
|
'/' + thisbuild['version'] +'/g',
|
|
|
|
'AndroidManifest.xml'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend manifest"
|
|
|
|
sys.exit(1)
|
|
|
|
if thisbuild.has_key('insertvercode'):
|
|
|
|
if subprocess.call(['sed','-i','s/' + thisbuild['insertvercode'] +
|
|
|
|
'/' + thisbuild['vercode'] +'/g',
|
|
|
|
'AndroidManifest.xml'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend manifest"
|
|
|
|
sys.exit(1)
|
2011-01-08 23:31:55 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Delete unwanted file...
|
|
|
|
if thisbuild.has_key('rm'):
|
|
|
|
os.remove(os.path.join(build_dir, thisbuild['rm']))
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-03-03 21:25:18 +01:00
|
|
|
# Fix apostrophes translation files if necessary...
|
|
|
|
if thisbuild.get('fixapos', 'no') == 'yes':
|
|
|
|
for root, dirs, files in os.walk(os.path.join(root_dir,'res')):
|
|
|
|
for filename in files:
|
|
|
|
if filename.endswith('.xml'):
|
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
r"\([^\\]\)'@\1\\'" +
|
|
|
|
'@g',
|
|
|
|
os.path.join(root, filename)]) != 0:
|
|
|
|
print "Failed to amend " + filename
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-02-01 15:54:34 +01:00
|
|
|
# Fix translation files if necessary...
|
2011-03-03 21:25:18 +01:00
|
|
|
if thisbuild.get('fixtrans', 'no') == 'yes':
|
2011-02-01 15:54:34 +01:00
|
|
|
for root, dirs, files in os.walk(os.path.join(root_dir,'res')):
|
|
|
|
for filename in files:
|
|
|
|
if filename.endswith('.xml'):
|
|
|
|
f = open(os.path.join(root, filename))
|
|
|
|
changed = False
|
|
|
|
outlines = []
|
|
|
|
for line in f:
|
|
|
|
num = 1
|
|
|
|
index = 0
|
|
|
|
oldline = line
|
|
|
|
while True:
|
|
|
|
index = line.find("%", index)
|
|
|
|
if index == -1:
|
|
|
|
break
|
|
|
|
next = line[index+1:index+2]
|
|
|
|
if next == "s" or next == "d":
|
|
|
|
line = (line[:index+1] +
|
|
|
|
str(num) + "$" +
|
|
|
|
line[index+1:])
|
|
|
|
num += 1
|
|
|
|
index += 3
|
|
|
|
else:
|
|
|
|
index += 1
|
|
|
|
# We only want to insert the positional arguments
|
|
|
|
# when there is more than one argument...
|
|
|
|
if oldline != line:
|
|
|
|
if num > 2:
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
line = oldline
|
|
|
|
outlines.append(line)
|
|
|
|
f.close()
|
|
|
|
if changed:
|
|
|
|
f = open(os.path.join(root, filename), 'w')
|
|
|
|
f.writelines(outlines)
|
|
|
|
f.close()
|
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Run a pre-build command if one is required...
|
|
|
|
if thisbuild.has_key('prebuild'):
|
2011-01-26 17:02:30 +01:00
|
|
|
if subprocess.call(thisbuild['prebuild'],
|
|
|
|
cwd=root_dir, shell=True) != 0:
|
2011-01-10 08:59:41 +01:00
|
|
|
print "Error running pre-build command"
|
|
|
|
sys.exit(1)
|
2011-01-08 23:31:55 +01:00
|
|
|
|
2011-06-16 22:57:49 +02:00
|
|
|
# Apply patches if any
|
|
|
|
if 'patch' in thisbuild:
|
|
|
|
for patch in thisbuild['patch'].split(';'):
|
|
|
|
print "Applying " + patch
|
|
|
|
patch_path = os.path.join('metadata', app['id'], patch)
|
|
|
|
if subprocess.call(['patch', '-p1',
|
2011-09-13 13:10:07 +02:00
|
|
|
'-i', os.path.abspath(patch_path)], cwd=build_dir) != 0:
|
2011-06-16 22:57:49 +02:00
|
|
|
print "Failed to apply patch %s" % patch_path
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Special case init functions for funambol...
|
2011-03-03 23:41:45 +01:00
|
|
|
if thisbuild.get('initfun', 'no') == "yes":
|
2011-01-10 08:59:41 +01:00
|
|
|
|
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
'<taskdef resource="net/sf/antcontrib/antcontrib.properties" />' +
|
|
|
|
'@' +
|
|
|
|
'<taskdef resource="net/sf/antcontrib/antcontrib.properties">' +
|
|
|
|
'<classpath>' +
|
|
|
|
'<pathelement location="/usr/share/java/ant-contrib.jar"/>' +
|
|
|
|
'</classpath>' +
|
|
|
|
'</taskdef>' +
|
|
|
|
'@g',
|
|
|
|
'build.xml'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend build.xml"
|
|
|
|
sys.exit(1)
|
2011-01-09 16:52:00 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
'\${user.home}/funambol/build/android/build.properties' +
|
|
|
|
'@' +
|
|
|
|
'build.properties' +
|
|
|
|
'@g',
|
|
|
|
'build.xml'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend build.xml"
|
|
|
|
sys.exit(1)
|
2011-01-09 16:52:00 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
buildxml = os.path.join(root_dir, 'build.xml')
|
|
|
|
f = open(buildxml, 'r')
|
|
|
|
xml = f.read()
|
|
|
|
f.close()
|
|
|
|
xmlout = ""
|
|
|
|
mode = 0
|
|
|
|
for line in xml.splitlines():
|
|
|
|
if mode == 0:
|
|
|
|
if line.find("jarsigner") != -1:
|
|
|
|
mode = 1
|
2011-01-09 16:52:00 +01:00
|
|
|
else:
|
2011-01-10 08:59:41 +01:00
|
|
|
xmlout += line + "\n"
|
|
|
|
else:
|
|
|
|
if line.find("/exec") != -1:
|
|
|
|
mode += 1
|
|
|
|
if mode == 3:
|
|
|
|
mode =0
|
|
|
|
f = open(buildxml, 'w')
|
|
|
|
f.write(xmlout)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
'platforms/android-2.0' +
|
|
|
|
'@' +
|
|
|
|
'platforms/android-8' +
|
|
|
|
'@g',
|
|
|
|
'build.xml'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend build.xml"
|
2011-01-05 00:58:44 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
shutil.copyfile(
|
|
|
|
os.path.join(root_dir, "build.properties.example"),
|
|
|
|
os.path.join(root_dir, "build.properties"))
|
|
|
|
|
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
'javacchome=.*'+
|
|
|
|
'@' +
|
|
|
|
'javacchome=' + javacc_path +
|
|
|
|
'@g',
|
|
|
|
'build.properties'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend build.properties"
|
|
|
|
sys.exit(1)
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
'sdk-folder=.*'+
|
|
|
|
'@' +
|
|
|
|
'sdk-folder=' + sdk_path +
|
|
|
|
'@g',
|
|
|
|
'build.properties'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend build.properties"
|
|
|
|
sys.exit(1)
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
if subprocess.call(['sed','-i','s@' +
|
|
|
|
'android.sdk.version.*'+
|
|
|
|
'@' +
|
|
|
|
'android.sdk.version=2.0' +
|
|
|
|
'@g',
|
|
|
|
'build.properties'], cwd=root_dir) !=0:
|
|
|
|
print "Failed to amend build.properties"
|
2011-01-05 00:58:44 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Build the source tarball right before we build the release...
|
|
|
|
tarname = app['id'] + '_' + thisbuild['vercode'] + '_src'
|
2011-08-26 00:50:15 +02:00
|
|
|
tarball = tarfile.open(os.path.join(output_dir,
|
2011-01-10 08:59:41 +01:00
|
|
|
tarname + '.tar.gz'), "w:gz")
|
|
|
|
tarball.add(build_dir, tarname)
|
|
|
|
tarball.close()
|
|
|
|
|
2011-01-10 09:31:35 +01:00
|
|
|
# Build native stuff if required...
|
2011-03-03 23:41:45 +01:00
|
|
|
if thisbuild.get('buildjni', 'no') == 'yes':
|
2011-01-10 09:31:35 +01:00
|
|
|
ndkbuild = os.path.join(ndk_path, "ndk-build")
|
|
|
|
p = subprocess.Popen([ndkbuild], cwd=root_dir,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
if p.returncode != 0:
|
|
|
|
print output
|
2011-06-16 23:08:07 +02:00
|
|
|
print "NDK build failed for %s:%s" % (app['id'], thisbuild['version'])
|
2011-01-10 09:31:35 +01:00
|
|
|
sys.exit(1)
|
2011-01-27 20:12:37 +01:00
|
|
|
elif options.verbose:
|
|
|
|
print output
|
2011-01-10 09:31:35 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Build the release...
|
2011-04-27 11:03:43 +02:00
|
|
|
if thisbuild.has_key('maven'):
|
|
|
|
p = subprocess.Popen(['mvn', 'clean', 'install',
|
|
|
|
'-Dandroid.sdk.path=' + sdk_path],
|
|
|
|
cwd=root_dir, stdout=subprocess.PIPE)
|
2011-01-10 08:59:41 +01:00
|
|
|
else:
|
2011-04-27 11:03:43 +02:00
|
|
|
if thisbuild.has_key('antcommand'):
|
|
|
|
antcommand = thisbuild['antcommand']
|
|
|
|
else:
|
|
|
|
antcommand = 'release'
|
|
|
|
p = subprocess.Popen(['ant', antcommand], cwd=root_dir,
|
|
|
|
stdout=subprocess.PIPE)
|
2011-01-10 08:59:41 +01:00
|
|
|
output = p.communicate()[0]
|
|
|
|
if p.returncode != 0:
|
2011-01-05 00:58:44 +01:00
|
|
|
print output
|
2011-06-16 23:08:07 +02:00
|
|
|
print "Build failed for %s:%s" % (app['id'], thisbuild['version'])
|
2011-01-10 08:59:41 +01:00
|
|
|
sys.exit(1)
|
2011-01-27 20:12:37 +01:00
|
|
|
elif options.verbose:
|
|
|
|
print output
|
2011-01-10 09:31:35 +01:00
|
|
|
print "Build successful"
|
2011-01-05 00:58:44 +01:00
|
|
|
|
2011-01-10 08:59:41 +01:00
|
|
|
# Find the apk name in the output...
|
|
|
|
if thisbuild.has_key('bindir'):
|
|
|
|
bindir = os.path.join(build_dir, thisbuild['bindir'])
|
|
|
|
else:
|
|
|
|
bindir = os.path.join(root_dir, 'bin')
|
2011-03-03 23:41:45 +01:00
|
|
|
if thisbuild.get('initfun', 'no') == "yes":
|
2011-01-10 08:59:41 +01:00
|
|
|
# Special case (again!) for funambol...
|
|
|
|
src = ("funambol-android-sync-client-" +
|
|
|
|
thisbuild['version'] + "-unsigned.apk")
|
2011-06-21 00:14:38 +02:00
|
|
|
src = os.path.join(bindir, src)
|
2011-04-27 11:03:43 +02:00
|
|
|
elif thisbuild.has_key('maven'):
|
|
|
|
src = re.match(r".*^\[INFO\] Installing /.*/([^/]*)\.apk",
|
|
|
|
output, re.S|re.M).group(1)
|
|
|
|
src = os.path.join(bindir, src) + '.apk'
|
|
|
|
#[INFO] Installing /home/ciaran/fdroidserver/tmp/mainline/application/target/callerid-1.0-SNAPSHOT.apk
|
2011-01-10 08:59:41 +01:00
|
|
|
else:
|
|
|
|
src = re.match(r".*^.*Creating (\S+) for release.*$.*", output,
|
|
|
|
re.S|re.M).group(1)
|
2011-04-27 11:03:43 +02:00
|
|
|
src = os.path.join(bindir, src)
|
2011-01-10 08:59:41 +01:00
|
|
|
|
|
|
|
# By way of a sanity check, make sure the version and version
|
|
|
|
# code in our new apk match what we expect...
|
2011-06-21 00:14:38 +02:00
|
|
|
print "Checking " + src
|
2011-04-06 20:55:27 +02:00
|
|
|
p = subprocess.Popen([os.path.join(sdk_path, 'platform-tools',
|
|
|
|
'aapt'),
|
|
|
|
'dump', 'badging', src],
|
|
|
|
stdout=subprocess.PIPE)
|
2011-01-10 08:59:41 +01:00
|
|
|
output = p.communicate()[0]
|
2011-03-03 23:41:45 +01:00
|
|
|
if thisbuild.get('novcheck', 'no') == "yes":
|
2011-01-31 23:57:56 +01:00
|
|
|
vercode = thisbuild['vercode']
|
|
|
|
version = thisbuild['version']
|
|
|
|
else:
|
|
|
|
vercode = None
|
|
|
|
version = None
|
|
|
|
for line in output.splitlines():
|
|
|
|
if line.startswith("package:"):
|
|
|
|
pat = re.compile(".*versionCode='([0-9]*)'.*")
|
|
|
|
vercode = re.match(pat, line).group(1)
|
|
|
|
pat = re.compile(".*versionName='([^']*)'.*")
|
|
|
|
version = re.match(pat, line).group(1)
|
2011-02-01 15:54:34 +01:00
|
|
|
if version == None or vercode == None:
|
2011-01-31 23:57:56 +01:00
|
|
|
print "Could not find version information in build in output"
|
|
|
|
sys.exit(1)
|
2011-01-10 08:59:41 +01:00
|
|
|
|
|
|
|
# Some apps (e.g. Timeriffic) have had the bonkers idea of
|
|
|
|
# including the entire changelog in the version number. Remove
|
|
|
|
# it so we can compare. (TODO: might be better to remove it
|
|
|
|
# before we compile, in fact)
|
|
|
|
index = version.find(" //")
|
|
|
|
if index != -1:
|
|
|
|
version = version[:index]
|
|
|
|
|
|
|
|
if (version != thisbuild['version'] or
|
|
|
|
vercode != thisbuild['vercode']):
|
|
|
|
print "Unexpected version/version code in output"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-08-26 00:50:15 +02:00
|
|
|
# Copy the unsigned apk to our temp directory for further
|
2011-01-10 08:59:41 +01:00
|
|
|
# processing...
|
|
|
|
shutil.copyfile(src, dest_unsigned)
|
|
|
|
|
|
|
|
# Figure out the key alias name we'll use. Only the first 8
|
|
|
|
# characters are significant, so we'll use the first 8 from
|
|
|
|
# the MD5 of the app's ID and hope there are no collisions.
|
|
|
|
# If a collision does occur later, we're going to have to
|
|
|
|
# come up with a new alogrithm, AND rename all existing keys
|
|
|
|
# in the keystore!
|
|
|
|
if keyaliases.has_key(app['id']):
|
|
|
|
# For this particular app, the key alias is overridden...
|
|
|
|
keyalias = keyaliases[app['id']]
|
|
|
|
else:
|
|
|
|
m = md5.new()
|
|
|
|
m.update(app['id'])
|
|
|
|
keyalias = m.hexdigest()[:8]
|
|
|
|
print "Key alias: " + keyalias
|
|
|
|
|
|
|
|
# See if we already have a key for this application, and
|
|
|
|
# if not generate one...
|
|
|
|
p = subprocess.Popen(['keytool', '-list',
|
|
|
|
'-alias', keyalias, '-keystore', keystore,
|
|
|
|
'-storepass', keystorepass], stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
if p.returncode !=0:
|
|
|
|
print "Key does not exist - generating..."
|
|
|
|
p = subprocess.Popen(['keytool', '-genkey',
|
|
|
|
'-keystore', keystore, '-alias', keyalias,
|
|
|
|
'-keyalg', 'RSA', '-keysize', '2048',
|
|
|
|
'-validity', '10000',
|
|
|
|
'-storepass', keystorepass, '-keypass', keypass,
|
|
|
|
'-dname', keydname], stdout=subprocess.PIPE)
|
2011-01-05 00:58:44 +01:00
|
|
|
output = p.communicate()[0]
|
|
|
|
print output
|
|
|
|
if p.returncode != 0:
|
2011-01-10 08:59:41 +01:00
|
|
|
print "Failed to generate key"
|
2011-01-05 00:58:44 +01:00
|
|
|
sys.exit(1)
|
2011-01-10 08:59:41 +01:00
|
|
|
|
|
|
|
# Sign the application...
|
|
|
|
p = subprocess.Popen(['jarsigner', '-keystore', keystore,
|
|
|
|
'-storepass', keystorepass, '-keypass', keypass,
|
|
|
|
dest_unsigned, keyalias], stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
print output
|
|
|
|
if p.returncode != 0:
|
|
|
|
print "Failed to sign application"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Zipalign it...
|
2011-04-06 20:55:27 +02:00
|
|
|
p = subprocess.Popen([os.path.join(sdk_path,'tools','zipalign'),
|
|
|
|
'-v', '4', dest_unsigned, dest],
|
|
|
|
stdout=subprocess.PIPE)
|
2011-01-10 08:59:41 +01:00
|
|
|
output = p.communicate()[0]
|
|
|
|
print output
|
|
|
|
if p.returncode != 0:
|
|
|
|
print "Failed to align application"
|
|
|
|
sys.exit(1)
|
|
|
|
os.remove(dest_unsigned)
|
2011-01-04 22:44:14 +01:00
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
print "Finished."
|
|
|
|
|