2010-11-11 01:03:39 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
#
|
|
|
|
# build.py - part of the FDroid server tools
|
|
|
|
# Copyright (C) 2010, Ciaran Gultnieks, ciaran@ciarang.com
|
|
|
|
#
|
|
|
|
# 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 glob
|
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import zipfile
|
|
|
|
import md5
|
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
#Read configuration...
|
|
|
|
execfile('config.py')
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
execfile('metadata.py')
|
|
|
|
|
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...
|
2010-11-11 23:34:39 +01:00
|
|
|
apps = read_metadata()
|
2010-11-11 01:03:39 +01:00
|
|
|
|
|
|
|
unsigned_dir = 'unsigned'
|
|
|
|
if os.path.exists(unsigned_dir):
|
|
|
|
shutil.rmtree(unsigned_dir)
|
|
|
|
os.mkdir(unsigned_dir)
|
|
|
|
|
|
|
|
for app in apps:
|
|
|
|
|
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
|
|
|
|
options.package == app['id'])):
|
2010-11-11 01:03:39 +01:00
|
|
|
|
|
|
|
print "About to build " + app['id']
|
|
|
|
|
|
|
|
build_dir = 'build_' + app['id']
|
|
|
|
|
|
|
|
# Remove the build directory if it already exists...
|
|
|
|
if os.path.exists(build_dir):
|
|
|
|
shutil.rmtree(build_dir)
|
|
|
|
|
|
|
|
# Get the source code...
|
|
|
|
if app['repotype'] == 'git':
|
2010-11-12 19:22:06 +01:00
|
|
|
if subprocess.call(['git', 'clone',app['repo'], build_dir]) != 0:
|
2010-11-11 01:03:39 +01:00
|
|
|
print "Git clone failed"
|
|
|
|
sys.exit(1)
|
2010-11-12 19:22:06 +01:00
|
|
|
elif app['repotype'] == 'svn':
|
|
|
|
if subprocess.call(['svn', 'checkout', app['repo'], build_dir]) != 0:
|
|
|
|
print "Svn checkout failed"
|
|
|
|
sys.exit(1)
|
2010-11-11 01:03:39 +01:00
|
|
|
else:
|
|
|
|
print "Invalid repo type " + app['repotype'] + " in " + app['id']
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
for thisbuild in app['builds']:
|
|
|
|
|
|
|
|
print "Building version " + thisbuild['version']
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2010-11-12 19:22:06 +01:00
|
|
|
# Optionally, the actual app source can be in a subdirectory...
|
|
|
|
if thisbuild.has_key('subdir'):
|
|
|
|
root_dir = os.path.join(build_dir, thisbuild['subdir'])
|
|
|
|
else:
|
|
|
|
root_dir = build_dir
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
if app['repotype'] == 'git':
|
2010-11-12 19:22:06 +01:00
|
|
|
if subprocess.call(['git', 'checkout', thisbuild['commit']],
|
2010-11-11 23:34:39 +01:00
|
|
|
cwd=build_dir) != 0:
|
|
|
|
print "Git checkout failed"
|
|
|
|
sys.exit(1)
|
2010-11-12 19:22:06 +01:00
|
|
|
elif app['repotype'] == 'svn':
|
|
|
|
if subprocess.call(['svn', 'update', '-r', thisbuild['commit']],
|
|
|
|
cwd=build_dir) != 0:
|
|
|
|
print "Svn update failed"
|
|
|
|
sys.exit(1)
|
2010-11-11 23:34:39 +01:00
|
|
|
else:
|
|
|
|
print "Invalid repo type " + app['repotype']
|
2010-11-11 01:03:39 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
# Generate (or update) the ant build file, build.xml...
|
2010-11-12 19:22:06 +01:00
|
|
|
parms = ['android','update','project','-p','.']
|
|
|
|
parms.append('--subprojects')
|
|
|
|
if thisbuild.has_key('target'):
|
|
|
|
parms.append('-t')
|
|
|
|
parms.append(thisbuild['target'])
|
|
|
|
if subprocess.call(parms, cwd=root_dir) != 0:
|
2010-11-11 23:34:39 +01:00
|
|
|
print "Failed to update project"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# If the app has ant set up to sign the release, we need to switch
|
|
|
|
# that off, because we want the unsigned apk...
|
2010-11-12 19:22:06 +01:00
|
|
|
if os.path.exists(os.path.join(root_dir, 'build.properties')):
|
2010-11-11 23:34:39 +01:00
|
|
|
if subprocess.call(['sed','-i','s/^key.store/#/',
|
2010-11-13 15:28:46 +01:00
|
|
|
'build.properties'], cwd=root_dir) !=0:
|
2010-11-11 23:34:39 +01:00
|
|
|
print "Failed to amend build.properties"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-11-13 15:28:46 +01:00
|
|
|
# Fix old-fashioned 'sdk-location' in local.properties by copying
|
|
|
|
# from sdk.dir, if necessary...
|
|
|
|
if (thisbuild.has_key('oldsdkloc') and
|
|
|
|
thisbuild['oldsdkloc'] == "yes"):
|
|
|
|
locprops = os.path.join(root_dir, 'local.properties')
|
|
|
|
f = open(locprops, 'r')
|
|
|
|
props = f.read()
|
|
|
|
f.close()
|
|
|
|
sdkloc = re.match(r".*^sdk.dir=(\S+)$.*", props,
|
|
|
|
re.S|re.M).group(1)
|
|
|
|
props += "\nsdk-location=" + sdkloc + "\n"
|
|
|
|
f = open(locprops, 'w')
|
|
|
|
f.write(props)
|
|
|
|
f.close()
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
# Build the release...
|
2010-11-12 19:22:06 +01:00
|
|
|
p = subprocess.Popen(['ant','release'], cwd=root_dir,
|
2010-11-11 23:34:39 +01:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
if p.returncode != 0:
|
2010-11-12 19:22:06 +01:00
|
|
|
print output
|
2010-11-11 23:34:39 +01:00
|
|
|
print "Build failed"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Find the apk name in the output...
|
|
|
|
src = re.match(r".*^.*Creating (\S+) for release.*$.*", output,
|
|
|
|
re.S|re.M).group(1)
|
2010-11-12 19:22:06 +01:00
|
|
|
src = os.path.join(os.path.join(root_dir, 'bin'), src)
|
2010-11-11 23:34:39 +01:00
|
|
|
|
|
|
|
# By way of a sanity check, make sure the version and version
|
|
|
|
# code in our new apk match what we expect...
|
|
|
|
p = subprocess.Popen([aapt_path,'dump','badging',
|
|
|
|
src], stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
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)
|
2010-11-13 15:28:46 +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]
|
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
if (version != thisbuild['version'] or
|
|
|
|
vercode != thisbuild['vercode']):
|
|
|
|
print "Unexpected version/version code in output"
|
|
|
|
sys.exit(1)
|
2010-11-11 01:03:39 +01:00
|
|
|
|
2010-11-11 23:34:39 +01:00
|
|
|
# Copy the unsigned apk to our 'unsigned' directory to be
|
|
|
|
# dealt with later...
|
|
|
|
dest = os.path.join(unsigned_dir, app['id'] + '_' +
|
|
|
|
thisbuild['vercode'] + '.apk')
|
|
|
|
shutil.copyfile(src, dest)
|
2010-11-11 01:03:39 +01:00
|
|
|
|
|
|
|
print "Finished."
|
|
|
|
|