2012-01-02 12:49:20 +01:00
|
|
|
#!/usr/bin/env python
|
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
|
2012-01-03 22:39:30 +01:00
|
|
|
# Copyright (C) 2010-12, 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
|
|
|
from xml.dom.minidom import Document
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2011-02-17 21:16:26 +01:00
|
|
|
import common
|
2012-01-02 15:09:20 +01:00
|
|
|
from common import BuildException
|
|
|
|
from common import VCSException
|
2011-02-17 21:16:26 +01:00
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
#Read configuration...
|
|
|
|
execfile('config.py')
|
|
|
|
|
|
|
|
# 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")
|
2012-01-08 19:27:54 +01:00
|
|
|
parser.add_option("-s", "--stop", action="store_true", default=False,
|
|
|
|
help="Make the build stop on exceptions")
|
2012-01-27 23:10:08 +01:00
|
|
|
parser.add_option("-t", "--test", action="store_true", default=False,
|
|
|
|
help="Test mode - put output in the tmp directory only.")
|
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
|
|
|
|
2012-01-03 08:43:14 +01:00
|
|
|
failed_apps = {}
|
|
|
|
build_succeeded = []
|
|
|
|
|
2012-01-08 19:27:54 +01:00
|
|
|
log_dir = 'logs'
|
|
|
|
if not os.path.isdir(log_dir):
|
|
|
|
print "Creating log directory"
|
|
|
|
os.makedirs(log_dir)
|
|
|
|
|
2012-01-02 12:49:20 +01:00
|
|
|
tmp_dir = 'tmp'
|
|
|
|
if not os.path.isdir(tmp_dir):
|
|
|
|
print "Creating temporary directory"
|
|
|
|
os.makedirs(tmp_dir)
|
|
|
|
|
2012-01-27 23:10:08 +01:00
|
|
|
if options.test:
|
|
|
|
output_dir = tmp_dir
|
|
|
|
else:
|
2012-02-02 15:27:49 +01:00
|
|
|
output_dir = 'unsigned'
|
2012-01-27 23:10:08 +01:00
|
|
|
if not os.path.isdir(output_dir):
|
|
|
|
print "Creating output directory"
|
|
|
|
os.makedirs(output_dir)
|
|
|
|
|
2012-02-02 15:27:49 +01:00
|
|
|
repo_dir = 'repo'
|
|
|
|
|
2012-01-02 12:49:20 +01:00
|
|
|
build_dir = 'build'
|
|
|
|
if not os.path.isdir(build_dir):
|
|
|
|
print "Creating build directory"
|
|
|
|
os.makedirs(build_dir)
|
2012-01-27 23:10:08 +01:00
|
|
|
extlib_dir = os.path.join(build_dir, 'extlib')
|
|
|
|
|
|
|
|
# Build applications...
|
2010-11-11 01:03:39 +01:00
|
|
|
for app in apps:
|
|
|
|
|
2012-01-22 00:10:59 +01:00
|
|
|
if options.package and options.package != app['id']:
|
|
|
|
# Silent skip...
|
|
|
|
pass
|
|
|
|
elif app['Disabled']:
|
2012-01-29 08:50:38 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "Skipping %s: disabled" % app['id']
|
2012-01-22 00:10:59 +01:00
|
|
|
elif (not app['builds']) or app['Repo Type'] =='' or len(app['builds']) == 0:
|
2012-01-29 08:50:38 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "Skipping %s: no builds specified" % app['id']
|
2012-01-22 00:10:59 +01:00
|
|
|
else:
|
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...
|
2012-01-10 19:57:07 +01:00
|
|
|
vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
|
2011-08-07 17:14:54 +02:00
|
|
|
|
|
|
|
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']:
|
2012-01-02 15:09:20 +01:00
|
|
|
try:
|
|
|
|
dest = os.path.join(output_dir, app['id'] + '_' +
|
|
|
|
thisbuild['vercode'] + '.apk')
|
2012-02-02 15:27:49 +01:00
|
|
|
dest_repo = os.path.join(repo_dir, app['id'] + '_' +
|
|
|
|
thisbuild['vercode'] + '.apk')
|
2012-01-02 15:09:20 +01:00
|
|
|
|
2012-02-02 15:27:49 +01:00
|
|
|
if os.path.exists(dest) or os.path.exists(dest_repo):
|
2012-01-29 08:50:38 +01:00
|
|
|
if options.verbose:
|
|
|
|
print "..version " + thisbuild['version'] + " already exists"
|
2012-01-02 15:09:20 +01:00
|
|
|
elif thisbuild['commit'].startswith('!'):
|
2012-01-29 08:50:38 +01:00
|
|
|
if options.verbose:
|
|
|
|
print ("..skipping version " + thisbuild['version'] + " - " +
|
2012-01-02 15:09:20 +01:00
|
|
|
thisbuild['commit'][1:])
|
2011-01-09 22:54:55 +01:00
|
|
|
else:
|
2012-01-29 08:50:38 +01:00
|
|
|
if options.verbose:
|
|
|
|
mstart = '.. building version '
|
|
|
|
else:
|
|
|
|
mstart = 'Building version '
|
|
|
|
print mstart + thisbuild['version'] + ' of ' + app['id']
|
2012-01-02 15:09:20 +01:00
|
|
|
|
2012-01-03 22:39:30 +01:00
|
|
|
# Prepare the source code...
|
|
|
|
root_dir = common.prepare_source(vcs, app, thisbuild,
|
2012-01-27 23:10:08 +01:00
|
|
|
build_dir, extlib_dir, sdk_path, ndk_path,
|
|
|
|
javacc_path, not refreshed_source)
|
2012-01-03 22:39:30 +01:00
|
|
|
refreshed_source = True
|
2012-01-02 15:09:20 +01:00
|
|
|
|
2012-02-02 23:13:31 +01:00
|
|
|
# Scan before building...
|
|
|
|
buildprobs = common.scan_source(build_dir)
|
|
|
|
if len(buildprobs) > 0:
|
|
|
|
print 'Scanner found ' + str(len(buildprobs)) + ' problems:'
|
|
|
|
for problem in buildprobs:
|
|
|
|
print '...' + problem
|
|
|
|
raise BuildException("Can't build due to " +
|
|
|
|
str(len(buildprobs)) + " scanned problems")
|
|
|
|
|
2012-01-02 15:09:20 +01:00
|
|
|
# Build the source tarball right before we build the release...
|
|
|
|
tarname = app['id'] + '_' + thisbuild['vercode'] + '_src'
|
2012-01-27 23:10:08 +01:00
|
|
|
tarball = tarfile.open(os.path.join(tmp_dir,
|
2012-01-02 15:09:20 +01:00
|
|
|
tarname + '.tar.gz'), "w:gz")
|
2012-01-04 22:37:11 +01:00
|
|
|
def tarexc(f):
|
|
|
|
if f in ['.svn', '.git', '.hg', '.bzr']:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
tarball.add(build_dir, tarname, exclude=tarexc)
|
2012-01-02 15:09:20 +01:00
|
|
|
tarball.close()
|
|
|
|
|
|
|
|
# Build native stuff if required...
|
|
|
|
if thisbuild.get('buildjni', 'no') == 'yes':
|
|
|
|
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
|
|
|
|
raise BuildException("NDK build failed for %s:%s" % (app['id'], thisbuild['version']))
|
|
|
|
elif options.verbose:
|
|
|
|
print output
|
|
|
|
|
|
|
|
# Build the release...
|
|
|
|
if thisbuild.has_key('maven'):
|
|
|
|
p = subprocess.Popen(['mvn', 'clean', 'install',
|
|
|
|
'-Dandroid.sdk.path=' + sdk_path],
|
2012-01-08 19:27:54 +01:00
|
|
|
cwd=root_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2012-01-02 15:09:20 +01:00
|
|
|
else:
|
|
|
|
if thisbuild.has_key('antcommand'):
|
|
|
|
antcommand = thisbuild['antcommand']
|
2011-01-10 08:59:41 +01:00
|
|
|
else:
|
2012-01-02 15:09:20 +01:00
|
|
|
antcommand = 'release'
|
|
|
|
p = subprocess.Popen(['ant', antcommand], cwd=root_dir,
|
2012-01-08 19:27:54 +01:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
output, error = p.communicate()
|
2011-01-10 09:31:35 +01:00
|
|
|
if p.returncode != 0:
|
2012-01-08 19:27:54 +01:00
|
|
|
raise BuildException("Build failed for %s:%s" % (app['id'], thisbuild['version']), output.strip(), error.strip())
|
2011-01-27 20:12:37 +01:00
|
|
|
elif options.verbose:
|
|
|
|
print output
|
2012-01-02 15:09:20 +01:00
|
|
|
print "Build successful"
|
2011-01-10 09:31:35 +01:00
|
|
|
|
2012-01-02 15:09:20 +01:00
|
|
|
# Find the apk name in the output...
|
|
|
|
if thisbuild.has_key('bindir'):
|
|
|
|
bindir = os.path.join(build_dir, thisbuild['bindir'])
|
2011-04-27 11:03:43 +02:00
|
|
|
else:
|
2012-01-02 15:09:20 +01:00
|
|
|
bindir = os.path.join(root_dir, 'bin')
|
|
|
|
if thisbuild.get('initfun', 'no') == "yes":
|
|
|
|
# Special case (again!) for funambol...
|
|
|
|
src = ("funambol-android-sync-client-" +
|
|
|
|
thisbuild['version'] + "-unsigned.apk")
|
|
|
|
src = os.path.join(bindir, src)
|
|
|
|
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'
|
2011-04-27 11:03:43 +02:00
|
|
|
#[INFO] Installing /home/ciaran/fdroidserver/tmp/mainline/application/target/callerid-1.0-SNAPSHOT.apk
|
2012-01-02 15:09:20 +01:00
|
|
|
else:
|
|
|
|
src = re.match(r".*^.*Creating (\S+) for release.*$.*", output,
|
|
|
|
re.S|re.M).group(1)
|
|
|
|
src = os.path.join(bindir, src)
|
|
|
|
|
|
|
|
# By way of a sanity check, make sure the version and version
|
|
|
|
# code in our new apk match what we expect...
|
|
|
|
print "Checking " + src
|
|
|
|
p = subprocess.Popen([os.path.join(sdk_path, 'platform-tools',
|
|
|
|
'aapt'),
|
|
|
|
'dump', 'badging', src],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
output = p.communicate()[0]
|
|
|
|
if thisbuild.get('novcheck', 'no') == "yes":
|
|
|
|
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)
|
|
|
|
if version == None or vercode == None:
|
|
|
|
raise BuildException("Could not find version information in build in output")
|
|
|
|
|
|
|
|
# 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']):
|
|
|
|
raise BuildException(("Unexpected version/version code in output"
|
|
|
|
"APK: %s / %s"
|
|
|
|
"Expected: %s / %s")
|
2012-01-08 12:55:57 +01:00
|
|
|
% (version, str(vercode), thisbuild['version'], str(thisbuild['vercode']))
|
|
|
|
)
|
2012-01-02 15:09:20 +01:00
|
|
|
|
2012-02-02 15:27:49 +01:00
|
|
|
# Copy the unsigned apk to our destination directory for further
|
|
|
|
# processing (by publish.py)...
|
|
|
|
shutil.copyfile(src, dest)
|
2012-01-27 23:10:08 +01:00
|
|
|
|
|
|
|
# Move the source tarball into the output directory...
|
|
|
|
if output_dir != tmp_dir:
|
2012-02-02 15:27:49 +01:00
|
|
|
tarfilename = tarname + '.tar.gz'
|
|
|
|
shutil.move(os.path.join(tmp_dir, tarfilename),
|
|
|
|
os.path.join(output_dir, tarfilename))
|
2012-01-27 23:10:08 +01:00
|
|
|
|
2012-01-06 13:21:06 +01:00
|
|
|
build_succeeded.append(app)
|
2012-01-02 15:09:20 +01:00
|
|
|
except BuildException as be:
|
2012-01-08 19:27:54 +01:00
|
|
|
if options.stop:
|
|
|
|
raise
|
2012-01-02 15:09:20 +01:00
|
|
|
print "Could not build app %s due to BuildException: %s" % (app['id'], be)
|
2012-01-08 19:27:54 +01:00
|
|
|
logfile = open(os.path.join(log_dir, app['id'] + '.log'), 'a+')
|
|
|
|
logfile.write(str(be))
|
|
|
|
logfile.close
|
2012-01-03 08:43:14 +01:00
|
|
|
failed_apps[app['id']] = be
|
2012-01-02 15:09:20 +01:00
|
|
|
except VCSException as vcse:
|
2012-01-08 19:27:54 +01:00
|
|
|
if options.stop:
|
|
|
|
raise
|
2012-01-02 15:09:20 +01:00
|
|
|
print "VCS error while building app %s: %s" % (app['id'], vcse)
|
2012-01-03 08:43:14 +01:00
|
|
|
failed_apps[app['id']] = vcse
|
2012-01-02 15:09:20 +01:00
|
|
|
except Exception as e:
|
2012-01-08 19:27:54 +01:00
|
|
|
if options.stop:
|
|
|
|
raise
|
2012-01-02 15:09:20 +01:00
|
|
|
print "Could not build app %s due to unknown error: %s" % (app['id'], e)
|
2012-01-03 08:43:14 +01:00
|
|
|
failed_apps[app['id']] = e
|
|
|
|
|
|
|
|
for app in build_succeeded:
|
|
|
|
print "success: %s" % (app['id'])
|
2011-01-04 22:44:14 +01:00
|
|
|
|
2012-01-03 17:02:28 +01:00
|
|
|
for fa in failed_apps:
|
2012-01-08 19:27:54 +01:00
|
|
|
print "Build for app %s failed:\n%s" % (fa, failed_apps[fa])
|
2012-01-03 17:02:28 +01:00
|
|
|
|
2010-11-11 01:03:39 +01:00
|
|
|
print "Finished."
|
2012-01-08 19:27:54 +01:00
|
|
|
if len(build_succeeded) > 0:
|
2012-01-10 00:36:03 +01:00
|
|
|
print str(len(build_succeeded)) + ' builds succeeded'
|
2012-01-03 17:02:28 +01:00
|
|
|
if len(failed_apps) > 0:
|
2012-01-08 16:09:43 +01:00
|
|
|
print str(len(failed_apps)) + ' builds failed'
|
2010-11-11 01:03:39 +01:00
|
|
|
|