2 # -*- coding: utf-8 -*-
4 # build.py - part of the FDroid server tools
5 # Copyright (C) 2010-2014, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013-2014 Daniel Martà <mvdan@mvdan.cc>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30 from ConfigParser import ConfigParser
31 from optparse import OptionParser, OptionError
36 from common import BuildException, VCSException, FDroidPopen, SilentPopen
43 def get_builder_vm_id():
44 vd = os.path.join('builder', '.vagrant')
46 # Vagrant 1.2 (and maybe 1.1?) it's a directory tree...
47 with open(os.path.join(vd, 'machines', 'default',
48 'virtualbox', 'id')) as vf:
52 # Vagrant 1.0 - it's a json file...
53 with open(os.path.join('builder', '.vagrant')) as vf:
55 return v['active']['default']
58 def got_valid_builder_vm():
59 """Returns True if we have a valid-looking builder vm
61 if not os.path.exists(os.path.join('builder', 'Vagrantfile')):
63 vd = os.path.join('builder', '.vagrant')
64 if not os.path.exists(vd):
66 if not os.path.isdir(vd):
67 # Vagrant 1.0 - if the directory is there, it's valid...
69 # Vagrant 1.2 - the directory can exist, but the id can be missing...
70 if not os.path.exists(os.path.join(vd, 'machines', 'default',
76 def vagrant(params, cwd=None, printout=False):
77 """Run a vagrant command.
79 :param: list of parameters to pass to vagrant
80 :cwd: directory to run in, or None for current directory
81 :returns: (ret, out) where ret is the return code, and out
82 is the stdout (and stderr) from vagrant
84 p = FDroidPopen(['vagrant'] + params, cwd=cwd)
85 return (p.returncode, p.stdout)
88 def get_vagrant_sshinfo():
89 """Get ssh connection info for a vagrant VM
91 :returns: A dictionary containing 'hostname', 'port', 'user'
94 if subprocess.call('vagrant ssh-config >sshconfig',
95 cwd='builder', shell=True) != 0:
96 raise BuildException("Error getting ssh config")
97 vagranthost = 'default' # Host in ssh config file
98 sshconfig = paramiko.SSHConfig()
99 sshf = open('builder/sshconfig', 'r')
100 sshconfig.parse(sshf)
102 sshconfig = sshconfig.lookup(vagranthost)
103 idfile = sshconfig['identityfile']
104 if isinstance(idfile, list):
106 elif idfile.startswith('"') and idfile.endswith('"'):
107 idfile = idfile[1:-1]
108 return {'hostname': sshconfig['hostname'],
109 'port': int(sshconfig['port']),
110 'user': sshconfig['user'],
114 def get_clean_vm(reset=False):
115 """Get a clean VM ready to do a buildserver build.
117 This might involve creating and starting a new virtual machine from
118 scratch, or it might be as simple (unless overridden by the reset
119 parameter) as re-using a snapshot created previously.
121 A BuildException will be raised if anything goes wrong.
123 :reset: True to force creating from scratch.
124 :returns: A dictionary containing 'hostname', 'port', 'user'
127 # Reset existing builder machine to a clean state if possible.
130 logging.info("Checking for valid existing build server")
132 if got_valid_builder_vm():
133 logging.info("...VM is present")
134 p = FDroidPopen(['VBoxManage', 'snapshot',
135 get_builder_vm_id(), 'list',
136 '--details'], cwd='builder')
137 if 'fdroidclean' in p.stdout:
138 logging.info("...snapshot exists - resetting build server to "
140 retcode, output = vagrant(['status'], cwd='builder')
142 if 'running' in output:
143 logging.info("...suspending")
144 vagrant(['suspend'], cwd='builder')
145 logging.info("...waiting a sec...")
147 p = FDroidPopen(['VBoxManage', 'snapshot', get_builder_vm_id(),
148 'restore', 'fdroidclean'],
151 if p.returncode == 0:
152 logging.info("...reset to snapshot - server is valid")
153 retcode, output = vagrant(['up'], cwd='builder')
155 raise BuildException("Failed to start build server")
156 logging.info("...waiting a sec...")
158 sshinfo = get_vagrant_sshinfo()
161 logging.info("...failed to reset to snapshot")
163 logging.info("...snapshot doesn't exist - "
164 "VBoxManage snapshot list:\n" + p.stdout)
166 # If we can't use the existing machine for any reason, make a
167 # new one from scratch.
169 if os.path.exists('builder'):
170 logging.info("Removing broken/incomplete/unwanted build server")
171 vagrant(['destroy', '-f'], cwd='builder')
172 shutil.rmtree('builder')
175 p = subprocess.Popen('vagrant --version', shell=True,
176 stdout=subprocess.PIPE)
177 vver = p.communicate()[0]
178 if vver.startswith('Vagrant version 1.2'):
179 with open('builder/Vagrantfile', 'w') as vf:
180 vf.write('Vagrant.configure("2") do |config|\n')
181 vf.write('config.vm.box = "buildserver"\n')
184 with open('builder/Vagrantfile', 'w') as vf:
185 vf.write('Vagrant::Config.run do |config|\n')
186 vf.write('config.vm.box = "buildserver"\n')
189 logging.info("Starting new build server")
190 retcode, _ = vagrant(['up'], cwd='builder')
192 raise BuildException("Failed to start build server")
194 # Open SSH connection to make sure it's working and ready...
195 logging.info("Connecting to virtual machine...")
196 sshinfo = get_vagrant_sshinfo()
197 sshs = paramiko.SSHClient()
198 sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
199 sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
200 port=sshinfo['port'], timeout=300,
202 key_filename=sshinfo['idfile'])
205 logging.info("Saving clean state of new build server")
206 retcode, _ = vagrant(['suspend'], cwd='builder')
208 raise BuildException("Failed to suspend build server")
209 logging.info("...waiting a sec...")
211 p = FDroidPopen(['VBoxManage', 'snapshot', get_builder_vm_id(),
212 'take', 'fdroidclean'],
214 if p.returncode != 0:
215 raise BuildException("Failed to take snapshot")
216 logging.info("...waiting a sec...")
218 logging.info("Restarting new build server")
219 retcode, _ = vagrant(['up'], cwd='builder')
221 raise BuildException("Failed to start build server")
222 logging.info("...waiting a sec...")
224 # Make sure it worked...
225 p = FDroidPopen(['VBoxManage', 'snapshot', get_builder_vm_id(),
226 'list', '--details'],
228 if 'fdroidclean' not in p.stdout:
229 raise BuildException("Failed to take snapshot.")
235 """Release the VM previously started with get_clean_vm().
237 This should always be called.
239 logging.info("Suspending build server")
240 subprocess.call(['vagrant', 'suspend'], cwd='builder')
243 # Note that 'force' here also implies test mode.
244 def build_server(app, thisbuild, vcs, build_dir, output_dir, force):
245 """Do a build on the build server."""
248 raise BuildException("Paramiko is required to use the buildserver")
250 logging.getLogger("paramiko").setLevel(logging.DEBUG)
252 logging.getLogger("paramiko").setLevel(logging.WARN)
254 sshinfo = get_clean_vm()
258 # Open SSH connection...
259 logging.info("Connecting to virtual machine...")
260 sshs = paramiko.SSHClient()
261 sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
262 sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
263 port=sshinfo['port'], timeout=300,
264 look_for_keys=False, key_filename=sshinfo['idfile'])
266 homedir = '/home/' + sshinfo['user']
268 # Get an SFTP connection...
269 ftp = sshs.open_sftp()
270 ftp.get_channel().settimeout(15)
272 # Put all the necessary files in place...
275 # Helper to copy the contents of a directory to the server...
277 root = os.path.dirname(path)
278 main = os.path.basename(path)
280 for r, d, f in os.walk(path):
281 rr = os.path.relpath(r, root)
286 lfile = os.path.join(root, rr, ff)
287 if not os.path.islink(lfile):
289 ftp.chmod(ff, os.stat(lfile).st_mode)
290 for i in range(len(rr.split('/'))):
294 logging.info("Preparing server for build...")
295 serverpath = os.path.abspath(os.path.dirname(__file__))
296 ftp.put(os.path.join(serverpath, 'build.py'), 'build.py')
297 ftp.put(os.path.join(serverpath, 'common.py'), 'common.py')
298 ftp.put(os.path.join(serverpath, 'metadata.py'), 'metadata.py')
299 ftp.put(os.path.join(serverpath, '..', 'buildserver',
300 'config.buildserver.py'), 'config.py')
301 ftp.chmod('config.py', 0o600)
303 # Copy over the ID (head commit hash) of the fdroidserver in use...
304 subprocess.call('git rev-parse HEAD >' +
305 os.path.join(os.getcwd(), 'tmp', 'fdroidserverid'),
306 shell=True, cwd=serverpath)
307 ftp.put('tmp/fdroidserverid', 'fdroidserverid')
309 # Copy the metadata - just the file for this app...
310 ftp.mkdir('metadata')
312 ftp.chdir('metadata')
313 ftp.put(os.path.join('metadata', app['id'] + '.txt'),
315 # And patches if there are any...
316 if os.path.exists(os.path.join('metadata', app['id'])):
317 send_dir(os.path.join('metadata', app['id']))
320 # Create the build directory...
325 # Copy any extlibs that are required...
326 if 'extlibs' in thisbuild:
327 ftp.chdir(homedir + '/build/extlib')
328 for lib in thisbuild['extlibs']:
330 libsrc = os.path.join('build/extlib', lib)
331 if not os.path.exists(libsrc):
332 raise BuildException("Missing extlib {0}".format(libsrc))
335 if d not in ftp.listdir():
338 ftp.put(libsrc, lp[-1])
341 # Copy any srclibs that are required...
343 if 'srclibs' in thisbuild:
344 for lib in thisbuild['srclibs']:
346 common.getsrclib(lib, 'build/srclib', srclibpaths,
347 basepath=True, prepare=False))
349 # If one was used for the main source, add that too.
350 basesrclib = vcs.getsrclib()
352 srclibpaths.append(basesrclib)
353 for name, number, lib in srclibpaths:
354 logging.info("Sending srclib '%s'" % lib)
355 ftp.chdir(homedir + '/build/srclib')
356 if not os.path.exists(lib):
357 raise BuildException("Missing srclib directory '" + lib + "'")
358 fv = '.fdroidvcs-' + name
359 ftp.put(os.path.join('build/srclib', fv), fv)
361 # Copy the metadata file too...
362 ftp.chdir(homedir + '/srclibs')
363 ftp.put(os.path.join('srclibs', name + '.txt'),
365 # Copy the main app source code
366 # (no need if it's a srclib)
367 if (not basesrclib) and os.path.exists(build_dir):
368 ftp.chdir(homedir + '/build')
369 fv = '.fdroidvcs-' + app['id']
370 ftp.put(os.path.join('build', fv), fv)
373 # Execute the build script...
374 logging.info("Starting build...")
375 chan = sshs.get_transport().open_session()
377 cmdline = 'python build.py --on-server'
379 cmdline += ' --force --test'
381 cmdline += ' --verbose'
382 cmdline += " %s:%s" % (app['id'], thisbuild['vercode'])
383 chan.exec_command('bash -c ". ~/.bsenv && ' + cmdline + '"')
385 while not chan.exit_status_ready():
386 while chan.recv_ready():
387 output += chan.recv(1024)
389 logging.info("...getting exit status")
390 returncode = chan.recv_exit_status()
392 get = chan.recv(1024)
397 raise BuildException(
398 "Build.py failed on server for {0}:{1}".format(
399 app['id'], thisbuild['version']), output)
401 # Retrieve the built files...
402 logging.info("Retrieving build output...")
404 ftp.chdir(homedir + '/tmp')
406 ftp.chdir(homedir + '/unsigned')
407 apkfile = common.getapkname(app, thisbuild)
408 tarball = common.getsrcname(app, thisbuild)
410 ftp.get(apkfile, os.path.join(output_dir, apkfile))
411 if not options.notarball:
412 ftp.get(tarball, os.path.join(output_dir, tarball))
414 raise BuildException(
415 "Build failed for %s:%s - missing output files".format(
416 app['id'], thisbuild['version']), output)
421 # Suspend the build server.
425 def adapt_gradle(build_dir):
426 for root, dirs, files in os.walk(build_dir):
427 if 'build.gradle' in files:
428 path = os.path.join(root, 'build.gradle')
429 logging.info("Adapting build.gradle at %s" % path)
431 FDroidPopen(['sed', '-i',
432 r's@buildToolsVersion\([ =]*\)["\'][0-9\.]*["\']@buildToolsVersion\1"'
433 + config['build_tools'] + '"@g', path])
436 def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver):
437 """Do a build locally."""
439 if thisbuild.get('buildjni') not in (None, ['no']):
440 if not config['ndk_path']:
441 logging.critical("$ANDROID_NDK is not set!")
443 elif not os.path.isdir(config['sdk_path']):
444 logging.critical("$ANDROID_NDK points to a non-existing directory!")
447 # Prepare the source code...
448 root_dir, srclibpaths = common.prepare_source(vcs, app, thisbuild,
449 build_dir, srclib_dir, extlib_dir, onserver)
451 # We need to clean via the build tool in case the binary dirs are
452 # different from the default ones
454 if thisbuild['type'] == 'maven':
455 logging.info("Cleaning Maven project...")
456 cmd = [config['mvn3'], 'clean', '-Dandroid.sdk.path=' + config['sdk_path']]
458 if '@' in thisbuild['maven']:
459 maven_dir = os.path.join(root_dir, thisbuild['maven'].split('@', 1)[1])
460 maven_dir = os.path.normpath(maven_dir)
464 p = FDroidPopen(cmd, cwd=maven_dir)
466 elif thisbuild['type'] == 'gradle':
468 logging.info("Cleaning Gradle project...")
469 cmd = [config['gradle'], 'clean']
471 if '@' in thisbuild['gradle']:
472 gradle_dir = os.path.join(root_dir, thisbuild['gradle'].split('@', 1)[1])
473 gradle_dir = os.path.normpath(gradle_dir)
475 gradle_dir = root_dir
477 adapt_gradle(build_dir)
478 for name, number, libpath in srclibpaths:
479 adapt_gradle(libpath)
481 p = FDroidPopen(cmd, cwd=gradle_dir)
483 elif thisbuild['type'] == 'kivy':
486 elif thisbuild['type'] == 'ant':
487 logging.info("Cleaning Ant project...")
488 p = FDroidPopen(['ant', 'clean'], cwd=root_dir)
490 if p is not None and p.returncode != 0:
491 raise BuildException("Error cleaning %s:%s" %
492 (app['id'], thisbuild['version']), p.stdout)
494 logging.info("Getting rid of Gradle wrapper binaries...")
495 for root, dirs, files in os.walk(build_dir):
496 # Don't remove possibly necessary 'gradle' dirs if 'gradlew' is not there
497 if 'gradlew' in files:
498 os.remove(os.path.join(root, 'gradlew'))
499 if 'gradlew.bat' in files:
500 os.remove(os.path.join(root, 'gradlew.bat'))
502 shutil.rmtree(os.path.join(root, 'gradle'))
504 if not options.skipscan:
505 # Scan before building...
506 logging.info("Scanning source for common problems...")
507 count = common.scan_source(build_dir, root_dir, thisbuild)
510 logging.warn('Scanner found %d problems:' % count)
512 raise BuildException("Can't build due to %d errors while scanning" % count)
514 if not options.notarball:
515 # Build the source tarball right before we build the release...
516 logging.info("Creating source tarball...")
517 tarname = common.getsrcname(app, thisbuild)
518 tarball = tarfile.open(os.path.join(tmp_dir, tarname), "w:gz")
521 return any(f.endswith(s) for s in ['.svn', '.git', '.hg', '.bzr'])
522 tarball.add(build_dir, tarname, exclude=tarexc)
526 manifest = os.path.join(root_dir, 'AndroidManifest.xml')
527 if os.path.exists(manifest):
528 homedir = os.path.expanduser('~')
529 with open(os.path.join(homedir, 'buildserverid'), 'r') as f:
530 buildserverid = f.read()
531 with open(os.path.join(homedir, 'fdroidserverid'), 'r') as f:
532 fdroidserverid = f.read()
533 with open(manifest, 'r') as f:
534 manifestcontent = f.read()
535 manifestcontent = manifestcontent.replace('</manifest>',
536 '<fdroid buildserverid="' + buildserverid + '"' +
537 ' fdroidserverid="' + fdroidserverid + '"' +
539 with open(manifest, 'w') as f:
540 f.write(manifestcontent)
542 # Run a build command if one is required...
543 if 'build' in thisbuild:
544 cmd = common.replace_config_vars(thisbuild['build'])
545 # Substitute source library paths into commands...
546 for name, number, libpath in srclibpaths:
547 libpath = os.path.relpath(libpath, root_dir)
548 cmd = cmd.replace('$$' + name + '$$', libpath)
549 logging.info("Running 'build' commands in %s" % root_dir)
551 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
553 if p.returncode != 0:
554 raise BuildException("Error running build command for %s:%s" %
555 (app['id'], thisbuild['version']), p.stdout)
557 # Build native stuff if required...
558 if thisbuild.get('buildjni') not in (None, ['no']):
559 logging.info("Building native libraries...")
560 jni_components = thisbuild.get('buildjni')
561 if jni_components == ['yes']:
562 jni_components = ['']
563 cmd = [os.path.join(config['ndk_path'], "ndk-build"), "-j1"]
564 for d in jni_components:
566 logging.info("Building native code in '%s'" % d)
568 logging.info("Building native code in the main project")
569 manifest = root_dir + '/' + d + '/AndroidManifest.xml'
570 if os.path.exists(manifest):
571 # Read and write the whole AM.xml to fix newlines and avoid
572 # the ndk r8c or later 'wordlist' errors. The outcome of this
573 # under gnu/linux is the same as when using tools like
574 # dos2unix, but the native python way is faster and will
575 # work in non-unix systems.
576 manifest_text = open(manifest, 'U').read()
577 open(manifest, 'w').write(manifest_text)
578 # In case the AM.xml read was big, free the memory
580 p = FDroidPopen(cmd, cwd=os.path.join(root_dir, d))
581 if p.returncode != 0:
582 raise BuildException("NDK build failed for %s:%s" % (app['id'], thisbuild['version']), p.stdout)
585 # Build the release...
586 if thisbuild['type'] == 'maven':
587 logging.info("Building Maven project...")
589 if '@' in thisbuild['maven']:
590 maven_dir = os.path.join(root_dir, thisbuild['maven'].split('@', 1)[1])
594 mvncmd = [config['mvn3'], '-Dandroid.sdk.path=' + config['sdk_path'],
595 '-Dmaven.jar.sign.skip=true', '-Dmaven.test.skip=true',
596 '-Dandroid.sign.debug=false', '-Dandroid.release=true',
598 if 'target' in thisbuild:
599 target = thisbuild["target"].split('-')[1]
600 FDroidPopen(['sed', '-i',
601 's@<platform>[0-9]*</platform>@<platform>'+target+'</platform>@g',
602 'pom.xml'], cwd=root_dir)
603 if '@' in thisbuild['maven']:
604 FDroidPopen(['sed', '-i',
605 's@<platform>[0-9]*</platform>@<platform>'+target+'</platform>@g',
606 'pom.xml'], cwd=maven_dir)
608 if 'mvnflags' in thisbuild:
609 mvncmd += thisbuild['mvnflags']
611 p = FDroidPopen(mvncmd, cwd=maven_dir)
613 bindir = os.path.join(root_dir, 'target')
615 elif thisbuild['type'] == 'kivy':
616 logging.info("Building Kivy project...")
618 spec = os.path.join(root_dir, 'buildozer.spec')
619 if not os.path.exists(spec):
620 raise BuildException("Expected to find buildozer-compatible spec at {0}"
623 defaults = {'orientation': 'landscape', 'icon': '',
624 'permissions': '', 'android.api': "18"}
625 bconfig = ConfigParser(defaults, allow_no_value=True)
628 distdir = 'python-for-android/dist/fdroid'
629 if os.path.exists(distdir):
630 shutil.rmtree(distdir)
632 modules = bconfig.get('app', 'requirements').split(',')
634 cmd = 'ANDROIDSDK=' + config['sdk_path']
635 cmd += ' ANDROIDNDK=' + config['ndk_path']
636 cmd += ' ANDROIDNDKVER=r9'
637 cmd += ' ANDROIDAPI=' + str(bconfig.get('app', 'android.api'))
638 cmd += ' VIRTUALENV=virtualenv'
639 cmd += ' ./distribute.sh'
640 cmd += ' -m ' + "'" + ' '.join(modules) + "'"
642 p = FDroidPopen(cmd, cwd='python-for-android', shell=True)
643 if p.returncode != 0:
644 raise BuildException("Distribute build failed")
646 cid = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
648 raise BuildException("Package ID mismatch between metadata and spec")
650 orientation = bconfig.get('app', 'orientation', 'landscape')
651 if orientation == 'all':
652 orientation = 'sensor'
656 '--name', bconfig.get('app', 'title'),
657 '--package', app['id'],
658 '--version', bconfig.get('app', 'version'),
659 '--orientation', orientation
662 perms = bconfig.get('app', 'permissions')
663 for perm in perms.split(','):
664 cmd.extend(['--permission', perm])
666 if config.get('app', 'fullscreen') == 0:
667 cmd.append('--window')
669 icon = bconfig.get('app', 'icon.filename')
671 cmd.extend(['--icon', os.path.join(root_dir, icon)])
673 cmd.append('release')
674 p = FDroidPopen(cmd, cwd=distdir)
676 elif thisbuild['type'] == 'gradle':
677 logging.info("Building Gradle project...")
678 if '@' in thisbuild['gradle']:
679 flavours = thisbuild['gradle'].split('@')[0].split(',')
680 gradle_dir = thisbuild['gradle'].split('@')[1]
681 gradle_dir = os.path.join(root_dir, gradle_dir)
683 flavours = thisbuild['gradle'].split(',')
684 gradle_dir = root_dir
686 if len(flavours) == 1 and flavours[0] in ['main', 'yes', '']:
689 commands = [config['gradle']]
690 if 'preassemble' in thisbuild:
691 commands += thisbuild['preassemble'].split()
693 flavours_cmd = ''.join(flavours)
695 flavours_cmd = flavours_cmd[0].upper() + flavours_cmd[1:]
697 commands += ['assemble'+flavours_cmd+'Release']
699 p = FDroidPopen(commands, cwd=gradle_dir)
701 elif thisbuild['type'] == 'ant':
702 logging.info("Building Ant project...")
704 if 'antcommand' in thisbuild:
705 cmd += [thisbuild['antcommand']]
708 p = FDroidPopen(cmd, cwd=root_dir)
710 bindir = os.path.join(root_dir, 'bin')
712 if p is not None and p.returncode != 0:
713 raise BuildException("Build failed for %s:%s" % (app['id'], thisbuild['version']), p.stdout)
714 logging.info("Successfully built version " + thisbuild['version'] + ' of ' + app['id'])
716 if thisbuild['type'] == 'maven':
717 stdout_apk = '\n'.join([
718 line for line in p.stdout.splitlines() if any(a in line for a in ('.apk', '.ap_'))])
719 m = re.match(r".*^\[INFO\] .*apkbuilder.*/([^/]*)\.apk",
720 stdout_apk, re.S | re.M)
722 m = re.match(r".*^\[INFO\] Creating additional unsigned apk file .*/([^/]+)\.apk[^l]",
723 stdout_apk, re.S | re.M)
725 m = re.match(r'.*^\[INFO\] [^$]*aapt \[package,[^$]*' + bindir + r'/([^/]+)\.ap[_k][,\]]',
726 stdout_apk, re.S | re.M)
728 raise BuildException('Failed to find output')
730 src = os.path.join(bindir, src) + '.apk'
731 elif thisbuild['type'] == 'kivy':
732 src = 'python-for-android/dist/default/bin/{0}-{1}-release.apk'.format(
733 bconfig.get('app', 'title'), bconfig.get('app', 'version'))
734 elif thisbuild['type'] == 'gradle':
737 if 'subdir' in thisbuild:
738 dd = os.path.join(dd, thisbuild['subdir'])
739 basename = os.path.basename(thisbuild['subdir'])
740 if '@' in thisbuild['gradle']:
741 dd = os.path.join(dd, thisbuild['gradle'].split('@')[1])
743 if len(flavours) == 1 and flavours[0] == '':
744 name = '-'.join([basename, 'release', 'unsigned'])
746 name = '-'.join([basename, '-'.join(flavours), 'release', 'unsigned'])
747 dd = os.path.normpath(dd)
748 src = os.path.join(dd, 'build', 'apk', name+'.apk')
749 elif thisbuild['type'] == 'ant':
750 stdout_apk = '\n'.join([
751 line for line in p.stdout.splitlines() if '.apk' in line])
752 src = re.match(r".*^.*Creating (.+) for release.*$.*", stdout_apk,
753 re.S | re.M).group(1)
754 src = os.path.join(bindir, src)
755 elif thisbuild['type'] == 'raw':
756 src = os.path.join(root_dir, thisbuild['output'])
757 src = os.path.normpath(src)
759 # Make sure it's not debuggable...
760 if common.isApkDebuggable(src, config):
761 raise BuildException("APK is debuggable")
763 # By way of a sanity check, make sure the version and version
764 # code in our new apk match what we expect...
765 logging.info("Checking " + src)
766 if not os.path.exists(src):
767 raise BuildException("Unsigned apk is not at expected location of " + src)
769 p = SilentPopen([os.path.join(config['sdk_path'],
770 'build-tools', config['build_tools'], 'aapt'),
771 'dump', 'badging', src])
777 for line in p.stdout.splitlines():
778 if line.startswith("package:"):
779 pat = re.compile(".*name='([a-zA-Z0-9._]*)'.*")
783 pat = re.compile(".*versionCode='([0-9]*)'.*")
787 pat = re.compile(".*versionName='([^']*)'.*")
791 elif line.startswith("native-code:"):
792 nativecode = line[12:]
794 if thisbuild.get('buildjni') is not None:
795 if nativecode is None or "'" not in nativecode:
796 raise BuildException("Native code should have been built but none was packaged")
797 if thisbuild['novcheck']:
798 vercode = thisbuild['vercode']
799 version = thisbuild['version']
800 if not version or not vercode:
801 raise BuildException("Could not find version information in build in output")
803 raise BuildException("Could not find package ID in output")
804 if foundid != app['id']:
805 raise BuildException("Wrong package ID - build " + foundid + " but expected " + app['id'])
807 # Some apps (e.g. Timeriffic) have had the bonkers idea of
808 # including the entire changelog in the version number. Remove
809 # it so we can compare. (TODO: might be better to remove it
810 # before we compile, in fact)
811 index = version.find(" //")
813 version = version[:index]
815 if (version != thisbuild['version'] or
816 vercode != thisbuild['vercode']):
817 raise BuildException(("Unexpected version/version code in output;"
818 " APK: '%s' / '%s', "
819 " Expected: '%s' / '%s'")
820 % (version, str(vercode), thisbuild['version'], str(thisbuild['vercode']))
823 # Copy the unsigned apk to our destination directory for further
824 # processing (by publish.py)...
825 dest = os.path.join(output_dir, common.getapkname(app, thisbuild))
826 shutil.copyfile(src, dest)
828 # Move the source tarball into the output directory...
829 if output_dir != tmp_dir and not options.notarball:
830 shutil.move(os.path.join(tmp_dir, tarname),
831 os.path.join(output_dir, tarname))
834 def trybuild(app, thisbuild, build_dir, output_dir, also_check_dir, srclib_dir, extlib_dir,
835 tmp_dir, repo_dir, vcs, test, server, force, onserver):
837 Build a particular version of an application, if it needs building.
839 :param output_dir: The directory where the build output will go. Usually
840 this is the 'unsigned' directory.
841 :param repo_dir: The repo directory - used for checking if the build is
843 :paaram also_check_dir: An additional location for checking if the build
844 is necessary (usually the archive repo)
845 :param test: True if building in test mode, in which case the build will
846 always happen, even if the output already exists. In test mode, the
847 output directory should be a temporary location, not any of the real
850 :returns: True if the build was done, False if it wasn't necessary.
853 dest_apk = common.getapkname(app, thisbuild)
855 dest = os.path.join(output_dir, dest_apk)
856 dest_repo = os.path.join(repo_dir, dest_apk)
859 if os.path.exists(dest) or os.path.exists(dest_repo):
863 dest_also = os.path.join(also_check_dir, dest_apk)
864 if os.path.exists(dest_also):
867 if 'disable' in thisbuild:
870 logging.info("Building version " + thisbuild['version'] + ' of ' + app['id'])
873 # When using server mode, still keep a local cache of the repo, by
874 # grabbing the source now.
875 vcs.gotorevision(thisbuild['commit'])
877 build_server(app, thisbuild, vcs, build_dir, output_dir, force)
879 build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver)
883 def parse_commandline():
884 """Parse the command line. Returns options, args."""
886 parser = OptionParser(usage="Usage: %prog [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
887 parser.add_option("-v", "--verbose", action="store_true", default=False,
888 help="Spew out even more information than normal")
889 parser.add_option("-q", "--quiet", action="store_true", default=False,
890 help="Restrict output to warnings and errors")
891 parser.add_option("-l", "--latest", action="store_true", default=False,
892 help="Build only the latest version of each package")
893 parser.add_option("-s", "--stop", action="store_true", default=False,
894 help="Make the build stop on exceptions")
895 parser.add_option("-t", "--test", action="store_true", default=False,
896 help="Test mode - put output in the tmp directory only, and always build, even if the output already exists.")
897 parser.add_option("--server", action="store_true", default=False,
898 help="Use build server")
899 parser.add_option("--resetserver", action="store_true", default=False,
900 help="Reset and create a brand new build server, even if the existing one appears to be ok.")
901 parser.add_option("--on-server", dest="onserver", action="store_true", default=False,
902 help="Specify that we're running on the build server")
903 parser.add_option("--skip-scan", dest="skipscan", action="store_true", default=False,
904 help="Skip scanning the source code for binaries and other problems")
905 parser.add_option("--no-tarball", dest="notarball", action="store_true", default=False,
906 help="Don't create a source tarball, useful when testing a build")
907 parser.add_option("-f", "--force", action="store_true", default=False,
908 help="Force build of disabled apps, and carries on regardless of scan problems. Only allowed in test mode.")
909 parser.add_option("-a", "--all", action="store_true", default=False,
910 help="Build all applications available")
911 parser.add_option("-w", "--wiki", default=False, action="store_true",
912 help="Update the wiki")
913 options, args = parser.parse_args()
915 # Force --stop with --on-server to get correct exit code
919 if options.force and not options.test:
920 raise OptionError("Force is only allowed in test mode", "force")
930 global options, config
932 options, args = parse_commandline()
933 if not args and not options.all:
934 raise OptionError("If you really want to build all the apps, use --all", "all")
936 config = common.read_config(options)
938 if config['build_server_always']:
939 options.server = True
940 if options.resetserver and not options.server:
941 raise OptionError("Using --resetserver without --server makes no sense", "resetserver")
944 if not os.path.isdir(log_dir):
945 logging.info("Creating log directory")
949 if not os.path.isdir(tmp_dir):
950 logging.info("Creating temporary directory")
956 output_dir = 'unsigned'
957 if not os.path.isdir(output_dir):
958 logging.info("Creating output directory")
959 os.makedirs(output_dir)
961 if config['archive_older'] != 0:
962 also_check_dir = 'archive'
964 also_check_dir = None
969 if not os.path.isdir(build_dir):
970 logging.info("Creating build directory")
971 os.makedirs(build_dir)
972 srclib_dir = os.path.join(build_dir, 'srclib')
973 extlib_dir = os.path.join(build_dir, 'extlib')
976 allapps = metadata.read_metadata(xref=not options.onserver)
978 apps = common.read_app_args(args, allapps, True)
979 apps = [app for app in apps if (options.force or not app['Disabled']) and
980 len(app['Repo Type']) > 0 and len(app['builds']) > 0]
983 raise Exception("No apps to process.")
987 for build in reversed(app['builds']):
988 if 'disable' in build:
990 app['builds'] = [build]
995 site = mwclient.Site((config['wiki_protocol'], config['wiki_server']),
996 path=config['wiki_path'])
997 site.login(config['wiki_user'], config['wiki_password'])
999 # Build applications...
1001 build_succeeded = []
1006 for thisbuild in app['builds']:
1010 # For the first build of a particular app, we need to set up
1011 # the source repo. We can reuse it on subsequent builds, if
1014 if app['Repo Type'] == 'srclib':
1015 build_dir = os.path.join('build', 'srclib', app['Repo'])
1017 build_dir = os.path.join('build', app['id'])
1019 # Set up vcs interface and make sure we have the latest code...
1020 logging.debug("Getting {0} vcs interface for {1}".format(
1021 app['Repo Type'], app['Repo']))
1022 vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
1026 logging.debug("Checking " + thisbuild['version'])
1027 if trybuild(app, thisbuild, build_dir, output_dir, also_check_dir,
1028 srclib_dir, extlib_dir, tmp_dir, repo_dir, vcs, options.test,
1029 options.server, options.force, options.onserver):
1030 build_succeeded.append(app)
1031 wikilog = "Build succeeded"
1032 except BuildException as be:
1033 logfile = open(os.path.join(log_dir, app['id'] + '.log'), 'a+')
1034 logfile.write(str(be))
1036 reason = str(be).split('\n', 1)[0] if options.verbose else str(be)
1037 print("Could not build app %s due to BuildException: %s" % (
1041 failed_apps[app['id']] = be
1042 wikilog = be.get_wikitext()
1043 except VCSException as vcse:
1044 print("VCS error while building app %s: %s" % (app['id'], vcse))
1047 failed_apps[app['id']] = vcse
1049 except Exception as e:
1050 print("Could not build app %s due to unknown error: %s" % (app['id'], traceback.format_exc()))
1053 failed_apps[app['id']] = e
1056 if options.wiki and wikilog:
1058 newpage = site.Pages[app['id'] + '/lastbuild']
1059 txt = "Build completed at " + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + "\n\n" + wikilog
1060 newpage.save(txt, summary='Build log')
1062 logging.info("Error while attempting to publish build log")
1064 for app in build_succeeded:
1065 logging.info("success: %s" % (app['id']))
1067 if not options.verbose:
1068 for fa in failed_apps:
1069 logging.info("Build for app %s failed:\n%s" % (fa, failed_apps[fa]))
1071 logging.info("Finished.")
1072 if len(build_succeeded) > 0:
1073 logging.info(str(len(build_succeeded)) + ' builds succeeded')
1074 if len(failed_apps) > 0:
1075 logging.info(str(len(failed_apps)) + ' builds failed')
1079 if __name__ == "__main__":