Restructured build.py a bit

This commit is contained in:
Ciaran Gultnieks 2012-02-26 14:09:25 +00:00
parent 2618f9609c
commit 4e5b4fa77c

124
build.py
View file

@ -33,8 +33,9 @@ from common import BuildException
from common import VCSException from common import VCSException
# Do a build on the build server.
def build_server(app, thisbuild, build_dir, output_dir): def build_server(app, thisbuild, build_dir, output_dir):
"""Do a build on the build server."""
import paramiko import paramiko
# Destroy the builder vm if it already exists... # Destroy the builder vm if it already exists...
@ -117,8 +118,8 @@ def build_server(app, thisbuild, build_dir, output_dir):
raise BuildException("Failed to destroy") raise BuildException("Failed to destroy")
# Do a build locally.
def build_local(app, thisbuild, build_dir, output_dir): def build_local(app, thisbuild, build_dir, output_dir):
"""Do a build locally."""
# Prepare the source code... # Prepare the source code...
root_dir = common.prepare_source(vcs, app, thisbuild, root_dir = common.prepare_source(vcs, app, thisbuild,
@ -164,11 +165,13 @@ def build_local(app, thisbuild, build_dir, output_dir):
'-Dandroid.sdk.path=' + sdk_path], '-Dandroid.sdk.path=' + sdk_path],
cwd=root_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cwd=root_dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else: else:
if thisbuild.has_key('antcommand'): if options.install:
antcommand = thisbuild['antcommand'] antcommands = ['debug',' install']
elif thisbuild.has_key('antcommand'):
antcommands = [thisbuild['antcommand']]
else: else:
antcommand = 'release' antcommands = ['release']
p = subprocess.Popen(['ant', antcommand], cwd=root_dir, p = subprocess.Popen(['ant'] + antcommands, cwd=root_dir,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = p.communicate() output, error = p.communicate()
if p.returncode != 0: if p.returncode != 0:
@ -247,10 +250,40 @@ def build_local(app, thisbuild, build_dir, output_dir):
os.path.join(output_dir, tarfilename)) os.path.join(output_dir, tarfilename))
#Read configuration... def trybuild(app, thisbuild, build_dir, output_dir, repo_dir, vcs, test):
execfile('config.py') """
Build a particular version of an application, if it needs building.
Returns True if the build was done, False if it wasn't necessary.
"""
dest = os.path.join(output_dir, app['id'] + '_' +
thisbuild['vercode'] + '.apk')
dest_repo = os.path.join(repo_dir, app['id'] + '_' +
thisbuild['vercode'] + '.apk')
if os.path.exists(dest) or (not test and os.path.exists(dest_repo)):
return False
if thisbuild['commit'].startswith('!'):
return False
if options.verbose:
mstart = '.. building version '
else:
mstart = 'Building version '
print mstart + thisbuild['version'] + ' of ' + app['id']
if options.server:
build_server(app, thisbuild, build_dir, output_dir)
else:
build_local(app, thisbuild, build_dir, output_dir)
return True
def parse_commandline():
"""Parse the command line. Returns options, args."""
# Parse command line...
parser = OptionParser() parser = OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal") help="Spew out even more information than normal")
@ -268,18 +301,33 @@ parser.add_option("--on-server", action="store_true", default=False,
help="Specify that we're running on the build server") help="Specify that we're running on the build server")
parser.add_option("-f", "--force", action="store_true", default=False, parser.add_option("-f", "--force", action="store_true", default=False,
help="Force build of disabled app. Only allowed in test mode.") help="Force build of disabled app. Only allowed in test mode.")
(options, args) = parser.parse_args() parser.add_option("--install", action="store_true", default=False,
help="Use 'ant debug install' to build and install a " +
"debug version on your device or emulator. " +
"Implies --force and --test")
options, args = parser.parse_args()
if options.force and not options.test: if options.force and not options.test:
print "Force is only allowed in test mode" print "Force is only allowed in test mode"
sys.exit(1) sys.exit(1)
# The --install option implies --test and --force...
if options.install:
options.force = True
options.test = True
return options, args
def main():
# Read configuration...
execfile('config.py')
options, args = parse_commandline()
# Get all apps... # Get all apps...
apps = common.read_metadata(options.verbose) apps = common.read_metadata(options.verbose)
failed_apps = {}
build_succeeded = []
log_dir = 'logs' log_dir = 'logs'
if not os.path.isdir(log_dir): if not os.path.isdir(log_dir):
print "Creating log directory" print "Creating log directory"
@ -306,25 +354,27 @@ if not os.path.isdir(build_dir):
os.makedirs(build_dir) os.makedirs(build_dir)
extlib_dir = os.path.join(build_dir, 'extlib') extlib_dir = os.path.join(build_dir, 'extlib')
# Filter apps and build versions according to command-line options... # Filter apps and build versions according to command-line options, etc...
if options.package: if options.package:
apps = [app for app in apps if app['id'] == options.package] apps = [app for app in apps if app['id'] == options.package]
if len(apps) == 0:
print "No such package"
sys.exit(1)
apps = [app for app in apps if not app['Disabled'] and app['builds'] and
len(app['Repo Type']) > 0 and len(app['builds']) > 0]
if len(apps) == 0:
print "Nothing to do - all apps are disabled or have no builds defined."
sys.exit(1)
if options.vercode: if options.vercode:
for app in apps: for app in apps:
app['builds'] = [b for b in app['builds'] app['builds'] = [b for b in app['builds']
if str(b['vercode']) == options.vercode] if str(b['vercode']) == options.vercode]
# Build applications... # Build applications...
failed_apps = {}
build_succeeded = []
for app in apps: for app in apps:
if app['Disabled'] and not options.force:
if options.verbose:
print "Skipping %s: disabled" % app['id']
elif (not app['builds']) or app['Repo Type'] =='' or len(app['builds']) == 0:
if options.verbose:
print "Skipping %s: no builds specified" % app['id']
else:
build_dir = 'build/' + app['id'] build_dir = 'build/' + app['id']
# Set up vcs interface and make sure we have the latest code... # Set up vcs interface and make sure we have the latest code...
@ -332,30 +382,8 @@ for app in apps:
for thisbuild in app['builds']: for thisbuild in app['builds']:
try: try:
dest = os.path.join(output_dir, app['id'] + '_' + if trybuild(app, thisbuild, build_dir, output_dir, repo_dir,
thisbuild['vercode'] + '.apk') vcs, options.test):
dest_repo = os.path.join(repo_dir, app['id'] + '_' +
thisbuild['vercode'] + '.apk')
if os.path.exists(dest) or (not options.test and os.path.exists(dest_repo)):
if options.verbose:
print "..version " + thisbuild['version'] + " already exists"
elif thisbuild['commit'].startswith('!'):
if options.verbose:
print ("..skipping version " + thisbuild['version'] + " - " +
thisbuild['commit'][1:])
else:
if options.verbose:
mstart = '.. building version '
else:
mstart = 'Building version '
print mstart + thisbuild['version'] + ' of ' + app['id']
if options.server:
build_server(app, thisbuild, build_dir, output_dir)
else:
build_local(app, thisbuild, build_dir, output_dir)
build_succeeded.append(app) build_succeeded.append(app)
except BuildException as be: except BuildException as be:
if options.stop: if options.stop:
@ -388,3 +416,7 @@ if len(build_succeeded) > 0:
if len(failed_apps) > 0: if len(failed_apps) > 0:
print str(len(failed_apps)) + ' builds failed' print str(len(failed_apps)) + ' builds failed'
if __name__ == "__main__":
main()