3 # build.py - part of the FDroid server tools
4 # Copyright (C) 2010-2014, Ciaran Gultnieks, ciaran@ciarang.com
5 # Copyright (C) 2013-2014 Daniel Martà <mvdan@mvdan.cc>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
33 from configparser import ConfigParser
34 from argparse import ArgumentParser
39 from . import metadata
42 from .common import FDroidPopen, SdkToolsPopen
43 from .exception import FDroidException, BuildException, VCSException
51 def get_vm_provider():
52 """Determine vm provider based on .vagrant directory content
54 if os.path.exists(os.path.join('builder', '.vagrant', 'machines',
55 'default', 'libvirt')):
60 def vm_get_builder_id(provider):
61 vd = os.path.join('builder', '.vagrant')
63 # Vagrant 1.2 (and maybe 1.1?) it's a directory tree...
64 with open(os.path.join(vd, 'machines', 'default',
65 provider, 'id')) as vf:
69 # Vagrant 1.0 - it's a json file...
70 with open(os.path.join('builder', '.vagrant')) as vf:
72 return v['active']['default']
75 def vm_get_builder_status():
76 """Get the current status of builder vm.
78 :returns: one of: 'running', 'paused', 'shutoff', 'not created'
79 If something is wrong with vagrant or the vm 'unknown' is returned.
81 (ret, out) = vagrant(['status'], cwd='builder')
83 allowed_providers = 'virtualbox|libvirt'
84 allowed_states = 'running|paused|shutoff|not created'
86 r = re.compile('^\s*(?P<vagrant_name>\w+)\s+' +
87 '(?P<vm_state>' + allowed_states + ')' +
88 '\s+\((?P<provider>' + allowed_providers + ')\)\s*$')
90 for line in out.split('\n'):
93 s = m.group('vm_state')
95 logging.debug('current builder vm status: ' + s)
98 logging.debug('current builder vm status: unknown')
102 def vm_is_builder_valid(provider):
103 """Returns True if we have a valid-looking builder vm
105 if not os.path.exists(os.path.join('builder', 'Vagrantfile')):
107 vd = os.path.join('builder', '.vagrant')
108 if not os.path.exists(vd):
110 if not os.path.isdir(vd):
111 # Vagrant 1.0 - if the directory is there, it's valid...
113 # Vagrant 1.2 - the directory can exist, but the id can be missing...
114 if not os.path.exists(os.path.join(vd, 'machines', 'default',
120 def vagrant(params, cwd=None, printout=False):
121 """Run a vagrant command.
123 :param: list of parameters to pass to vagrant
124 :cwd: directory to run in, or None for current directory
125 :printout: has not effect
126 :returns: (ret, out) where ret is the return code, and out
127 is the stdout (and stderr) from vagrant
129 p = FDroidPopen(['vagrant'] + params, cwd=cwd, output=printout, stderr_to_stdout=printout)
130 return (p.returncode, p.output)
133 def get_vagrant_sshinfo():
134 """Get ssh connection info for a vagrant VM
136 :returns: A dictionary containing 'hostname', 'port', 'user'
139 if subprocess.call('vagrant ssh-config >sshconfig',
140 cwd='builder', shell=True) != 0:
141 raise BuildException("Error getting ssh config")
142 vagranthost = 'default' # Host in ssh config file
143 sshconfig = paramiko.SSHConfig()
144 sshf = open(os.path.join('builder', 'sshconfig'), 'r')
145 sshconfig.parse(sshf)
147 sshconfig = sshconfig.lookup(vagranthost)
148 idfile = sshconfig['identityfile']
149 if isinstance(idfile, list):
151 elif idfile.startswith('"') and idfile.endswith('"'):
152 idfile = idfile[1:-1]
153 return {'hostname': sshconfig['hostname'],
154 'port': int(sshconfig['port']),
155 'user': sshconfig['user'],
159 def vm_shutdown_builder():
160 """Turn off builder vm.
163 if os.path.exists(os.path.join('builder', 'Vagrantfile')):
164 vagrant(['halt'], cwd='builder')
167 def vm_snapshot_list(provider):
168 output = options.verbose
169 if provider is 'virtualbox':
170 p = FDroidPopen(['VBoxManage', 'snapshot',
171 vm_get_builder_id(provider), 'list',
172 '--details'], cwd='builder',
173 output=output, stderr_to_stdout=output)
174 elif provider is 'libvirt':
175 p = FDroidPopen(['virsh', '-c', 'qemu:///system', 'snapshot-list',
176 vm_get_builder_id(provider)],
177 output=output, stderr_to_stdout=output)
181 def vm_snapshot_clean_available(provider):
182 return 'fdroidclean' in vm_snapshot_list(provider)
185 def vm_snapshot_restore(provider):
186 """Does a rollback of the build vm.
188 output = options.verbose
189 if provider is 'virtualbox':
190 p = FDroidPopen(['VBoxManage', 'snapshot',
191 vm_get_builder_id(provider), 'restore',
192 'fdroidclean'], cwd='builder',
193 output=output, stderr_to_stdout=output)
194 elif provider is 'libvirt':
195 p = FDroidPopen(['virsh', '-c', 'qemu:///system', 'snapshot-revert',
196 vm_get_builder_id(provider), 'fdroidclean'],
197 output=output, stderr_to_stdout=output)
198 return p.returncode == 0
201 def vm_snapshot_create(provider):
202 output = options.verbose
203 if provider is 'virtualbox':
204 p = FDroidPopen(['VBoxManage', 'snapshot',
205 vm_get_builder_id(provider),
206 'take', 'fdroidclean'], cwd='builder',
207 output=output, stderr_to_stdout=output)
208 elif provider is 'libvirt':
209 p = FDroidPopen(['virsh', '-c', 'qemu:///system', 'snapshot-create-as',
210 vm_get_builder_id(provider), 'fdroidclean'],
211 output=output, stderr_to_stdout=output)
212 return p.returncode != 0
215 def vm_test_ssh_into_builder():
216 logging.info("Connecting to virtual machine...")
217 sshinfo = get_vagrant_sshinfo()
218 sshs = paramiko.SSHClient()
219 sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
220 sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
221 port=sshinfo['port'], timeout=300,
223 key_filename=sshinfo['idfile'])
227 def vm_new_get_clean_builder(serverdir, reset=False):
228 if not os.path.isdir(serverdir):
229 logging.info("buildserver path does not exists, creating %s", serverdir)
230 os.makedirs(serverdir)
231 vagrantfile = os.path.join(serverdir, 'Vagrantfile')
232 if not os.path.isfile(vagrantfile):
233 with open(os.path.join('builder', 'Vagrantfile'), 'w') as f:
234 f.write(textwrap.dedent("""\
235 # generated file, do not change.
237 Vagrant.configure("2") do |config|
238 config.vm.box = "buildserver"
239 config.vm.synced_folder ".", "/vagrant", disabled: true
242 vm = vmtools.get_build_vm(serverdir)
244 logging.info('resetting buildserver by request')
245 elif not vm.vagrant_uuid_okay():
246 logging.info('resetting buildserver, bceause vagrant vm is not okay.')
248 elif not vm.snapshot_exists('fdroidclean'):
249 logging.info("resetting buildserver, because snapshot 'fdroidclean' is not present.")
258 logging.info('buildserver recreated: taking a clean snapshot')
259 vm.snapshot_create('fdroidclean')
261 logging.info('builserver ok: reverting to clean snapshot')
262 vm.snapshot_revert('fdroidclean')
265 return get_vagrant_sshinfo()
268 def vm_get_clean_builder(reset=False):
269 """Get a clean VM ready to do a buildserver build.
271 This might involve creating and starting a new virtual machine from
272 scratch, or it might be as simple (unless overridden by the reset
273 parameter) as re-using a snapshot created previously.
275 A BuildException will be raised if anything goes wrong.
277 :reset: True to force creating from scratch.
278 :returns: A dictionary containing 'hostname', 'port', 'user'
281 provider = get_vm_provider()
283 # Reset existing builder machine to a clean state if possible.
286 logging.info("Checking for valid existing build server")
287 if vm_is_builder_valid(provider):
288 logging.info("...VM is present (%s)" % provider)
289 if vm_snapshot_clean_available(provider):
290 logging.info("...snapshot exists - resetting build server to " +
292 status = vm_get_builder_status()
293 if status == 'running':
294 vm_test_ssh_into_builder()
295 logging.info("...suspending builder vm")
296 vagrant(['suspend'], cwd='builder')
297 logging.info("...waiting a sec...")
299 elif status == 'shutoff':
300 logging.info('...starting builder vm')
301 vagrant(['up'], cwd='builder')
302 logging.info('...waiting a sec...')
304 vm_test_ssh_into_builder()
305 logging.info('...suspending builder vm')
306 vagrant(['suspend'], cwd='builder')
307 logging.info("...waiting a sec...")
310 vm_get_builder_status()
312 if vm_snapshot_restore(provider):
314 vm_get_builder_status()
315 logging.info("...reset to snapshot - server is valid")
316 retcode, output = vagrant(['up'], cwd='builder')
318 raise BuildException("Failed to start build server")
319 logging.info("...waiting a sec...")
321 sshinfo = get_vagrant_sshinfo()
324 logging.info("...failed to reset to snapshot")
326 logging.info("...snapshot doesn't exist - "
327 "VBoxManage snapshot list:\n" +
328 vm_snapshot_list(provider))
330 logging.info('...VM not present')
332 # If we can't use the existing machine for any reason, make a
333 # new one from scratch.
335 if os.path.isdir('builder'):
336 vm = vmtools.get_build_vm('builder')
341 p = subprocess.Popen(['vagrant', '--version'],
342 universal_newlines=True,
343 stdout=subprocess.PIPE)
344 vver = p.communicate()[0].strip().split(' ')[1]
345 if vver.split('.')[0] != '1' or int(vver.split('.')[1]) < 4:
346 raise BuildException("Unsupported vagrant version {0}".format(vver))
348 with open(os.path.join('builder', 'Vagrantfile'), 'w') as vf:
349 vf.write('Vagrant.configure("2") do |config|\n')
350 vf.write(' config.vm.box = "buildserver"\n')
351 vf.write(' config.vm.synced_folder ".", "/vagrant", disabled: true\n')
354 logging.info("Starting new build server")
355 retcode, _ = vagrant(['up'], cwd='builder')
357 raise BuildException("Failed to start build server")
358 provider = get_vm_provider()
359 sshinfo = get_vagrant_sshinfo()
361 # Open SSH connection to make sure it's working and ready...
362 vm_test_ssh_into_builder()
364 logging.info("Saving clean state of new build server")
365 retcode, _ = vagrant(['suspend'], cwd='builder')
367 raise BuildException("Failed to suspend build server")
368 logging.info("...waiting a sec...")
370 if vm_snapshot_create(provider):
371 raise BuildException("Failed to take snapshot")
372 logging.info("...waiting a sec...")
374 logging.info("Restarting new build server")
375 retcode, _ = vagrant(['up'], cwd='builder')
377 raise BuildException("Failed to start build server")
378 logging.info("...waiting a sec...")
380 # Make sure it worked...
381 if not vm_snapshot_clean_available(provider):
382 raise BuildException("Failed to take snapshot.")
387 def vm_suspend_builder():
388 """Release the VM previously started with vm_get_clean_builder().
390 This should always be called after each individual app build attempt.
392 logging.info("Suspending build server")
393 subprocess.call(['vagrant', 'suspend'], cwd='builder')
396 # Note that 'force' here also implies test mode.
397 def build_server(app, build, vcs, build_dir, output_dir, log_dir, force):
398 """Do a build on the builder vm.
400 :param app: app metadata dict
402 :param vcs: version control system controller object
403 :param build_dir: local source-code checkout of app
404 :param output_dir: target folder for the build result
413 raise BuildException("Paramiko is required to use the buildserver")
415 logging.getLogger("paramiko").setLevel(logging.INFO)
417 logging.getLogger("paramiko").setLevel(logging.WARN)
419 # sshinfo = vm_get_clean_builder()
420 sshinfo = vm_new_get_clean_builder('builder')
423 if not buildserverid:
424 buildserverid = subprocess.check_output(['vagrant', 'ssh', '-c',
425 'cat /home/vagrant/buildserverid'],
426 cwd='builder').rstrip()
428 # Open SSH connection...
429 logging.info("Connecting to virtual machine...")
430 sshs = paramiko.SSHClient()
431 sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
432 sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
433 port=sshinfo['port'], timeout=300,
434 look_for_keys=False, key_filename=sshinfo['idfile'])
436 homedir = '/home/' + sshinfo['user']
438 # Get an SFTP connection...
439 ftp = sshs.open_sftp()
440 ftp.get_channel().settimeout(60)
442 # Put all the necessary files in place...
445 # Helper to copy the contents of a directory to the server...
447 root = os.path.dirname(path)
448 main = os.path.basename(path)
450 for r, d, f in os.walk(path):
451 rr = os.path.relpath(r, root)
456 lfile = os.path.join(root, rr, ff)
457 if not os.path.islink(lfile):
459 ftp.chmod(ff, os.stat(lfile).st_mode)
460 for i in range(len(rr.split('/'))):
464 logging.info("Preparing server for build...")
465 serverpath = os.path.abspath(os.path.dirname(__file__))
466 ftp.mkdir('fdroidserver')
467 ftp.chdir('fdroidserver')
468 ftp.put(os.path.join(serverpath, '..', 'fdroid'), 'fdroid')
469 ftp.chmod('fdroid', 0o755)
470 send_dir(os.path.join(serverpath))
473 ftp.put(os.path.join(serverpath, '..', 'buildserver',
474 'config.buildserver.py'), 'config.py')
475 ftp.chmod('config.py', 0o600)
477 # Copy over the ID (head commit hash) of the fdroidserver in use...
478 subprocess.call('git rev-parse HEAD >' +
479 os.path.join(os.getcwd(), 'tmp', 'fdroidserverid'),
480 shell=True, cwd=serverpath)
481 ftp.put('tmp/fdroidserverid', 'fdroidserverid')
483 # Copy the metadata - just the file for this app...
484 ftp.mkdir('metadata')
486 ftp.chdir('metadata')
487 ftp.put(os.path.join('metadata', app.id + '.txt'),
489 # And patches if there are any...
490 if os.path.exists(os.path.join('metadata', app.id)):
491 send_dir(os.path.join('metadata', app.id))
494 # Create the build directory...
499 # Copy any extlibs that are required...
501 ftp.chdir(homedir + '/build/extlib')
502 for lib in build.extlibs:
504 libsrc = os.path.join('build/extlib', lib)
505 if not os.path.exists(libsrc):
506 raise BuildException("Missing extlib {0}".format(libsrc))
509 if d not in ftp.listdir():
512 ftp.put(libsrc, lp[-1])
515 # Copy any srclibs that are required...
518 for lib in build.srclibs:
520 common.getsrclib(lib, 'build/srclib', basepath=True, prepare=False))
522 # If one was used for the main source, add that too.
523 basesrclib = vcs.getsrclib()
525 srclibpaths.append(basesrclib)
526 for name, number, lib in srclibpaths:
527 logging.info("Sending srclib '%s'" % lib)
528 ftp.chdir(homedir + '/build/srclib')
529 if not os.path.exists(lib):
530 raise BuildException("Missing srclib directory '" + lib + "'")
531 fv = '.fdroidvcs-' + name
532 ftp.put(os.path.join('build/srclib', fv), fv)
534 # Copy the metadata file too...
535 ftp.chdir(homedir + '/srclibs')
536 ftp.put(os.path.join('srclibs', name + '.txt'),
538 # Copy the main app source code
539 # (no need if it's a srclib)
540 if (not basesrclib) and os.path.exists(build_dir):
541 ftp.chdir(homedir + '/build')
542 fv = '.fdroidvcs-' + app.id
543 ftp.put(os.path.join('build', fv), fv)
546 # Execute the build script...
547 logging.info("Starting build...")
548 chan = sshs.get_transport().open_session()
550 cmdline = os.path.join(homedir, 'fdroidserver', 'fdroid')
551 cmdline += ' build --on-server'
553 cmdline += ' --force --test'
555 cmdline += ' --verbose'
557 cmdline += ' --skip-scan'
558 cmdline += " %s:%s" % (app.id, build.versionCode)
559 chan.exec_command('bash --login -c "' + cmdline + '"')
562 output += get_android_tools_version_log(build.ndk_path()).encode()
563 while not chan.exit_status_ready():
564 while chan.recv_ready():
565 output += chan.recv(1024)
567 logging.info("...getting exit status")
568 returncode = chan.recv_exit_status()
570 get = chan.recv(1024)
575 raise BuildException(
576 "Build.py failed on server for {0}:{1}".format(
577 app.id, build.versionName), str(output, 'utf-8'))
580 toolsversion_log = common.get_toolsversion_logname(app, build)
582 ftp.chdir(os.path.join(homedir, log_dir))
583 ftp.get(toolsversion_log, os.path.join(log_dir, toolsversion_log))
584 logging.debug('retrieved %s', toolsversion_log)
585 except Exception as e:
586 logging.warn('could not get %s from builder vm: %s' % (toolsversion_log, e))
588 # Retrieve the built files...
589 logging.info("Retrieving build output...")
591 ftp.chdir(homedir + '/tmp')
593 ftp.chdir(homedir + '/unsigned')
594 apkfile = common.get_release_filename(app, build)
595 tarball = common.getsrcname(app, build)
597 ftp.get(apkfile, os.path.join(output_dir, apkfile))
598 if not options.notarball:
599 ftp.get(tarball, os.path.join(output_dir, tarball))
601 raise BuildException(
602 "Build failed for %s:%s - missing output files".format(
603 app.id, build.versionName), output)
607 # Suspend the build server.
608 vm = vmtools.get_build_vm('builder')
612 def force_gradle_build_tools(build_dir, build_tools):
613 for root, dirs, files in os.walk(build_dir):
614 for filename in files:
615 if not filename.endswith('.gradle'):
617 path = os.path.join(root, filename)
618 if not os.path.isfile(path):
620 logging.debug("Forcing build-tools %s in %s" % (build_tools, path))
621 common.regsub_file(r"""(\s*)buildToolsVersion([\s=]+).*""",
622 r"""\1buildToolsVersion\2'%s'""" % build_tools,
626 def capitalize_intact(string):
627 """Like str.capitalize(), but leave the rest of the string intact without
628 switching it to lowercase."""
632 return string.upper()
633 return string[0].upper() + string[1:]
636 def has_native_code(apkobj):
637 """aapt checks if there are architecture folders under the lib/ folder
638 so we are simulating the same behaviour"""
639 arch_re = re.compile("^lib/(.*)/.*$")
640 arch = [file for file in apkobj.get_files() if arch_re.match(file)]
641 return False if not arch else True
644 def get_apk_metadata_aapt(apkfile):
645 """aapt function to extract versionCode, versionName, packageName and nativecode"""
651 p = SdkToolsPopen(['aapt', 'dump', 'badging', apkfile], output=False)
653 for line in p.output.splitlines():
654 if line.startswith("package:"):
655 pat = re.compile(".*name='([a-zA-Z0-9._]*)'.*")
659 pat = re.compile(".*versionCode='([0-9]*)'.*")
663 pat = re.compile(".*versionName='([^']*)'.*")
667 elif line.startswith("native-code:"):
668 nativecode = line[12:]
670 return vercode, version, foundid, nativecode
673 def get_apk_metadata_androguard(apkfile):
674 """androguard function to extract versionCode, versionName, packageName and nativecode"""
676 from androguard.core.bytecodes.apk import APK
677 apkobject = APK(apkfile)
679 raise BuildException("androguard library is not installed and aapt binary not found")
680 except FileNotFoundError:
681 raise BuildException("Could not open apk file for metadata analysis")
683 if not apkobject.is_valid_APK():
684 raise BuildException("Invalid APK provided")
686 foundid = apkobject.get_package()
687 vercode = apkobject.get_androidversion_code()
688 version = apkobject.get_androidversion_name()
689 nativecode = has_native_code(apkobject)
691 return vercode, version, foundid, nativecode
694 def get_metadata_from_apk(app, build, apkfile):
695 """get the required metadata from the built APK"""
697 if common.SdkToolsPopen(['aapt', 'version'], output=False):
698 vercode, version, foundid, nativecode = get_apk_metadata_aapt(apkfile)
700 vercode, version, foundid, nativecode = get_apk_metadata_androguard(apkfile)
702 # Ignore empty strings or any kind of space/newline chars that we don't
704 if nativecode is not None:
705 nativecode = nativecode.strip()
706 nativecode = None if not nativecode else nativecode
708 if build.buildjni and build.buildjni != ['no']:
709 if nativecode is None:
710 raise BuildException("Native code should have been built but none was packaged")
712 vercode = build.versionCode
713 version = build.versionName
714 if not version or not vercode:
715 raise BuildException("Could not find version information in build in output")
717 raise BuildException("Could not find package ID in output")
718 if foundid != app.id:
719 raise BuildException("Wrong package ID - build " + foundid + " but expected " + app.id)
721 return vercode, version
724 def build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver, refresh):
725 """Do a build locally."""
726 ndk_path = build.ndk_path()
727 if build.ndk or (build.buildjni and build.buildjni != ['no']):
729 logging.critical("Android NDK version '%s' could not be found!" % build.ndk or 'r12b')
730 logging.critical("Configured versions:")
731 for k, v in config['ndk_paths'].items():
732 if k.endswith("_orig"):
734 logging.critical(" %s: %s" % (k, v))
735 raise FDroidException()
736 elif not os.path.isdir(ndk_path):
737 logging.critical("Android NDK '%s' is not a directory!" % ndk_path)
738 raise FDroidException()
740 common.set_FDroidPopen_env(build)
742 # create ..._toolsversion.log when running in builder vm
744 log_path = os.path.join(log_dir,
745 common.get_toolsversion_logname(app, build))
746 with open(log_path, 'w') as f:
747 f.write(get_android_tools_version_log(build.ndk_path()))
749 # Prepare the source code...
750 root_dir, srclibpaths = common.prepare_source(vcs, app, build,
751 build_dir, srclib_dir,
752 extlib_dir, onserver, refresh)
754 # We need to clean via the build tool in case the binary dirs are
755 # different from the default ones
758 bmethod = build.build_method()
759 if bmethod == 'maven':
760 logging.info("Cleaning Maven project...")
761 cmd = [config['mvn3'], 'clean', '-Dandroid.sdk.path=' + config['sdk_path']]
763 if '@' in build.maven:
764 maven_dir = os.path.join(root_dir, build.maven.split('@', 1)[1])
765 maven_dir = os.path.normpath(maven_dir)
769 p = FDroidPopen(cmd, cwd=maven_dir)
771 elif bmethod == 'gradle':
773 logging.info("Cleaning Gradle project...")
775 if build.preassemble:
776 gradletasks += build.preassemble
778 flavours = build.gradle
779 if flavours == ['yes']:
782 flavours_cmd = ''.join([capitalize_intact(flav) for flav in flavours])
784 gradletasks += ['assemble' + flavours_cmd + 'Release']
786 if config['force_build_tools']:
787 force_gradle_build_tools(build_dir, config['build_tools'])
788 for name, number, libpath in srclibpaths:
789 force_gradle_build_tools(libpath, config['build_tools'])
791 cmd = [config['gradle']]
792 if build.gradleprops:
793 cmd += ['-P' + kv for kv in build.gradleprops]
797 p = FDroidPopen(cmd, cwd=root_dir)
799 elif bmethod == 'kivy':
802 elif bmethod == 'ant':
803 logging.info("Cleaning Ant project...")
804 p = FDroidPopen(['ant', 'clean'], cwd=root_dir)
806 if p is not None and p.returncode != 0:
807 raise BuildException("Error cleaning %s:%s" %
808 (app.id, build.versionName), p.output)
810 for root, dirs, files in os.walk(build_dir):
815 shutil.rmtree(os.path.join(root, d))
820 os.remove(os.path.join(root, f))
822 if 'build.gradle' in files:
823 # Even when running clean, gradle stores task/artifact caches in
824 # .gradle/ as binary files. To avoid overcomplicating the scanner,
825 # manually delete them, just like `gradle clean` should have removed
827 del_dirs(['build', '.gradle'])
828 del_files(['gradlew', 'gradlew.bat'])
830 if 'pom.xml' in files:
833 if any(f in files for f in ['ant.properties', 'project.properties', 'build.xml']):
834 del_dirs(['bin', 'gen'])
841 raise BuildException("Refusing to skip source scan since scandelete is present")
843 # Scan before building...
844 logging.info("Scanning source for common problems...")
845 count = scanner.scan_source(build_dir, root_dir, build)
848 logging.warn('Scanner found %d problems' % count)
850 raise BuildException("Can't build due to %d errors while scanning" % count)
852 if not options.notarball:
853 # Build the source tarball right before we build the release...
854 logging.info("Creating source tarball...")
855 tarname = common.getsrcname(app, build)
856 tarball = tarfile.open(os.path.join(tmp_dir, tarname), "w:gz")
859 return any(f.endswith(s) for s in ['.svn', '.git', '.hg', '.bzr'])
860 tarball.add(build_dir, tarname, exclude=tarexc)
863 # Run a build command if one is required...
865 logging.info("Running 'build' commands in %s" % root_dir)
866 cmd = common.replace_config_vars(build.build, build)
868 # Substitute source library paths into commands...
869 for name, number, libpath in srclibpaths:
870 libpath = os.path.relpath(libpath, root_dir)
871 cmd = cmd.replace('$$' + name + '$$', libpath)
873 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
875 if p.returncode != 0:
876 raise BuildException("Error running build command for %s:%s" %
877 (app.id, build.versionName), p.output)
879 # Build native stuff if required...
880 if build.buildjni and build.buildjni != ['no']:
881 logging.info("Building the native code")
882 jni_components = build.buildjni
884 if jni_components == ['yes']:
885 jni_components = ['']
886 cmd = [os.path.join(ndk_path, "ndk-build"), "-j1"]
887 for d in jni_components:
889 logging.info("Building native code in '%s'" % d)
891 logging.info("Building native code in the main project")
892 manifest = os.path.join(root_dir, d, 'AndroidManifest.xml')
893 if os.path.exists(manifest):
894 # Read and write the whole AM.xml to fix newlines and avoid
895 # the ndk r8c or later 'wordlist' errors. The outcome of this
896 # under gnu/linux is the same as when using tools like
897 # dos2unix, but the native python way is faster and will
898 # work in non-unix systems.
899 manifest_text = open(manifest, 'U').read()
900 open(manifest, 'w').write(manifest_text)
901 # In case the AM.xml read was big, free the memory
903 p = FDroidPopen(cmd, cwd=os.path.join(root_dir, d))
904 if p.returncode != 0:
905 raise BuildException("NDK build failed for %s:%s" % (app.id, build.versionName), p.output)
908 # Build the release...
909 if bmethod == 'maven':
910 logging.info("Building Maven project...")
912 if '@' in build.maven:
913 maven_dir = os.path.join(root_dir, build.maven.split('@', 1)[1])
917 mvncmd = [config['mvn3'], '-Dandroid.sdk.path=' + config['sdk_path'],
918 '-Dmaven.jar.sign.skip=true', '-Dmaven.test.skip=true',
919 '-Dandroid.sign.debug=false', '-Dandroid.release=true',
922 target = build.target.split('-')[1]
923 common.regsub_file(r'<platform>[0-9]*</platform>',
924 r'<platform>%s</platform>' % target,
925 os.path.join(root_dir, 'pom.xml'))
926 if '@' in build.maven:
927 common.regsub_file(r'<platform>[0-9]*</platform>',
928 r'<platform>%s</platform>' % target,
929 os.path.join(maven_dir, 'pom.xml'))
931 p = FDroidPopen(mvncmd, cwd=maven_dir)
933 bindir = os.path.join(root_dir, 'target')
935 elif bmethod == 'kivy':
936 logging.info("Building Kivy project...")
938 spec = os.path.join(root_dir, 'buildozer.spec')
939 if not os.path.exists(spec):
940 raise BuildException("Expected to find buildozer-compatible spec at {0}"
943 defaults = {'orientation': 'landscape', 'icon': '',
944 'permissions': '', 'android.api': "18"}
945 bconfig = ConfigParser(defaults, allow_no_value=True)
948 distdir = os.path.join('python-for-android', 'dist', 'fdroid')
949 if os.path.exists(distdir):
950 shutil.rmtree(distdir)
952 modules = bconfig.get('app', 'requirements').split(',')
954 cmd = 'ANDROIDSDK=' + config['sdk_path']
955 cmd += ' ANDROIDNDK=' + ndk_path
956 cmd += ' ANDROIDNDKVER=' + build.ndk
957 cmd += ' ANDROIDAPI=' + str(bconfig.get('app', 'android.api'))
958 cmd += ' VIRTUALENV=virtualenv'
959 cmd += ' ./distribute.sh'
960 cmd += ' -m ' + "'" + ' '.join(modules) + "'"
962 p = subprocess.Popen(cmd, cwd='python-for-android', shell=True)
963 if p.returncode != 0:
964 raise BuildException("Distribute build failed")
966 cid = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
968 raise BuildException("Package ID mismatch between metadata and spec")
970 orientation = bconfig.get('app', 'orientation', 'landscape')
971 if orientation == 'all':
972 orientation = 'sensor'
976 '--name', bconfig.get('app', 'title'),
978 '--version', bconfig.get('app', 'version'),
979 '--orientation', orientation
982 perms = bconfig.get('app', 'permissions')
983 for perm in perms.split(','):
984 cmd.extend(['--permission', perm])
986 if config.get('app', 'fullscreen') == 0:
987 cmd.append('--window')
989 icon = bconfig.get('app', 'icon.filename')
991 cmd.extend(['--icon', os.path.join(root_dir, icon)])
993 cmd.append('release')
994 p = FDroidPopen(cmd, cwd=distdir)
996 elif bmethod == 'gradle':
997 logging.info("Building Gradle project...")
999 cmd = [config['gradle']]
1000 if build.gradleprops:
1001 cmd += ['-P' + kv for kv in build.gradleprops]
1005 p = FDroidPopen(cmd, cwd=root_dir)
1007 elif bmethod == 'ant':
1008 logging.info("Building Ant project...")
1010 if build.antcommands:
1011 cmd += build.antcommands
1014 p = FDroidPopen(cmd, cwd=root_dir)
1016 bindir = os.path.join(root_dir, 'bin')
1018 if p is not None and p.returncode != 0:
1019 raise BuildException("Build failed for %s:%s" % (app.id, build.versionName), p.output)
1020 logging.info("Successfully built version " + build.versionName + ' of ' + app.id)
1022 omethod = build.output_method()
1023 if omethod == 'maven':
1024 stdout_apk = '\n'.join([
1025 line for line in p.output.splitlines() if any(
1026 a in line for a in ('.apk', '.ap_', '.jar'))])
1027 m = re.match(r".*^\[INFO\] .*apkbuilder.*/([^/]*)\.apk",
1028 stdout_apk, re.S | re.M)
1030 m = re.match(r".*^\[INFO\] Creating additional unsigned apk file .*/([^/]+)\.apk[^l]",
1031 stdout_apk, re.S | re.M)
1033 m = re.match(r'.*^\[INFO\] [^$]*aapt \[package,[^$]*' + bindir + r'/([^/]+)\.ap[_k][,\]]',
1034 stdout_apk, re.S | re.M)
1037 m = re.match(r".*^\[INFO\] Building jar: .*/" + bindir + r"/(.+)\.jar",
1038 stdout_apk, re.S | re.M)
1040 raise BuildException('Failed to find output')
1042 src = os.path.join(bindir, src) + '.apk'
1043 elif omethod == 'kivy':
1044 src = os.path.join('python-for-android', 'dist', 'default', 'bin',
1045 '{0}-{1}-release.apk'.format(
1046 bconfig.get('app', 'title'),
1047 bconfig.get('app', 'version')))
1048 elif omethod == 'gradle':
1051 os.path.join(root_dir, 'build', 'outputs', 'apk'),
1052 os.path.join(root_dir, 'build', 'apk'),
1054 for apkglob in ['*-release-unsigned.apk', '*-unsigned.apk', '*.apk']:
1055 apks = glob.glob(os.path.join(apks_dir, apkglob))
1058 raise BuildException('More than one resulting apks found in %s' % apks_dir,
1067 raise BuildException('Failed to find any output apks')
1069 elif omethod == 'ant':
1070 stdout_apk = '\n'.join([
1071 line for line in p.output.splitlines() if '.apk' in line])
1072 src = re.match(r".*^.*Creating (.+) for release.*$.*", stdout_apk,
1073 re.S | re.M).group(1)
1074 src = os.path.join(bindir, src)
1075 elif omethod == 'raw':
1076 output_path = common.replace_build_vars(build.output, build)
1077 globpath = os.path.join(root_dir, output_path)
1078 apks = glob.glob(globpath)
1080 raise BuildException('Multiple apks match %s' % globpath, '\n'.join(apks))
1082 raise BuildException('No apks match %s' % globpath)
1083 src = os.path.normpath(apks[0])
1085 # Make sure it's not debuggable...
1086 if common.isApkAndDebuggable(src, config):
1087 raise BuildException("APK is debuggable")
1089 # By way of a sanity check, make sure the version and version
1090 # code in our new apk match what we expect...
1091 logging.debug("Checking " + src)
1092 if not os.path.exists(src):
1093 raise BuildException("Unsigned apk is not at expected location of " + src)
1095 if common.get_file_extension(src) == 'apk':
1096 vercode, version = get_metadata_from_apk(app, build, src)
1097 if (version != build.versionName or vercode != build.versionCode):
1098 raise BuildException(("Unexpected version/version code in output;"
1099 " APK: '%s' / '%s', "
1100 " Expected: '%s' / '%s'")
1101 % (version, str(vercode), build.versionName,
1102 str(build.versionCode)))
1104 vercode = build.versionCode
1105 version = build.versionName
1107 # Add information for 'fdroid verify' to be able to reproduce the build
1110 metadir = os.path.join(tmp_dir, 'META-INF')
1111 if not os.path.exists(metadir):
1113 homedir = os.path.expanduser('~')
1114 for fn in ['buildserverid', 'fdroidserverid']:
1115 shutil.copyfile(os.path.join(homedir, fn),
1116 os.path.join(metadir, fn))
1117 subprocess.call(['jar', 'uf', os.path.abspath(src),
1118 'META-INF/' + fn], cwd=tmp_dir)
1120 # Copy the unsigned apk to our destination directory for further
1121 # processing (by publish.py)...
1122 dest = os.path.join(output_dir, common.get_release_filename(app, build))
1123 shutil.copyfile(src, dest)
1125 # Move the source tarball into the output directory...
1126 if output_dir != tmp_dir and not options.notarball:
1127 shutil.move(os.path.join(tmp_dir, tarname),
1128 os.path.join(output_dir, tarname))
1131 def trybuild(app, build, build_dir, output_dir, log_dir, also_check_dir,
1132 srclib_dir, extlib_dir, tmp_dir, repo_dir, vcs, test,
1133 server, force, onserver, refresh):
1135 Build a particular version of an application, if it needs building.
1137 :param output_dir: The directory where the build output will go. Usually
1138 this is the 'unsigned' directory.
1139 :param repo_dir: The repo directory - used for checking if the build is
1141 :param also_check_dir: An additional location for checking if the build
1142 is necessary (usually the archive repo)
1143 :param test: True if building in test mode, in which case the build will
1144 always happen, even if the output already exists. In test mode, the
1145 output directory should be a temporary location, not any of the real
1148 :returns: True if the build was done, False if it wasn't necessary.
1151 dest_file = common.get_release_filename(app, build)
1153 dest = os.path.join(output_dir, dest_file)
1154 dest_repo = os.path.join(repo_dir, dest_file)
1157 if os.path.exists(dest) or os.path.exists(dest_repo):
1161 dest_also = os.path.join(also_check_dir, dest_file)
1162 if os.path.exists(dest_also):
1165 if build.disable and not options.force:
1168 logging.info("Building version %s (%s) of %s" % (
1169 build.versionName, build.versionCode, app.id))
1172 # When using server mode, still keep a local cache of the repo, by
1173 # grabbing the source now.
1174 vcs.gotorevision(build.commit)
1176 build_server(app, build, vcs, build_dir, output_dir, log_dir, force)
1178 build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver, refresh)
1182 def get_android_tools_versions(ndk_path=None):
1183 '''get a list of the versions of all installed Android SDK/NDK components'''
1186 sdk_path = config['sdk_path']
1187 if sdk_path[-1] != '/':
1191 ndk_release_txt = os.path.join(ndk_path, 'RELEASE.TXT')
1192 if os.path.isfile(ndk_release_txt):
1193 with open(ndk_release_txt, 'r') as fp:
1194 components.append((os.path.basename(ndk_path), fp.read()[:-1]))
1196 pattern = re.compile('^Pkg.Revision=(.+)', re.MULTILINE)
1197 for root, dirs, files in os.walk(sdk_path):
1198 if 'source.properties' in files:
1199 source_properties = os.path.join(root, 'source.properties')
1200 with open(source_properties, 'r') as fp:
1201 m = pattern.search(fp.read())
1203 components.append((root[len(sdk_path):], m.group(1)))
1208 def get_android_tools_version_log(ndk_path):
1209 '''get a list of the versions of all installed Android SDK/NDK components'''
1210 log = '== Installed Android Tools ==\n\n'
1211 components = get_android_tools_versions(ndk_path)
1212 for name, version in sorted(components):
1213 log += '* ' + name + ' (' + version + ')\n'
1218 def parse_commandline():
1219 """Parse the command line. Returns options, parser."""
1221 parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
1222 common.setup_global_opts(parser)
1223 parser.add_argument("appid", nargs='*', help="app-id with optional versionCode in the form APPID[:VERCODE]")
1224 parser.add_argument("-l", "--latest", action="store_true", default=False,
1225 help="Build only the latest version of each package")
1226 parser.add_argument("-s", "--stop", action="store_true", default=False,
1227 help="Make the build stop on exceptions")
1228 parser.add_argument("-t", "--test", action="store_true", default=False,
1229 help="Test mode - put output in the tmp directory only, and always build, even if the output already exists.")
1230 parser.add_argument("--server", action="store_true", default=False,
1231 help="Use build server")
1232 parser.add_argument("--resetserver", action="store_true", default=False,
1233 help="Reset and create a brand new build server, even if the existing one appears to be ok.")
1234 parser.add_argument("--on-server", dest="onserver", action="store_true", default=False,
1235 help="Specify that we're running on the build server")
1236 parser.add_argument("--skip-scan", dest="skipscan", action="store_true", default=False,
1237 help="Skip scanning the source code for binaries and other problems")
1238 parser.add_argument("--dscanner", action="store_true", default=False,
1239 help="Setup an emulator, install the apk on it and perform a drozer scan")
1240 parser.add_argument("--no-tarball", dest="notarball", action="store_true", default=False,
1241 help="Don't create a source tarball, useful when testing a build")
1242 parser.add_argument("--no-refresh", dest="refresh", action="store_false", default=True,
1243 help="Don't refresh the repository, useful when testing a build with no internet connection")
1244 parser.add_argument("-f", "--force", action="store_true", default=False,
1245 help="Force build of disabled apps, and carries on regardless of scan problems. Only allowed in test mode.")
1246 parser.add_argument("-a", "--all", action="store_true", default=False,
1247 help="Build all applications available")
1248 parser.add_argument("-w", "--wiki", default=False, action="store_true",
1249 help="Update the wiki")
1250 metadata.add_metadata_arguments(parser)
1251 options = parser.parse_args()
1252 metadata.warnings_action = options.W
1254 # Force --stop with --on-server to get correct exit code
1255 if options.onserver:
1258 if options.force and not options.test:
1259 parser.error("option %s: Force is only allowed in test mode" % "force")
1261 return options, parser
1266 buildserverid = None
1271 global options, config, buildserverid
1273 options, parser = parse_commandline()
1275 # The defaults for .fdroid.* metadata that is included in a git repo are
1276 # different than for the standard metadata/ layout because expectations
1277 # are different. In this case, the most common user will be the app
1278 # developer working on the latest update of the app on their own machine.
1279 local_metadata_files = common.get_local_metadata_files()
1280 if len(local_metadata_files) == 1: # there is local metadata in an app's source
1281 config = dict(common.default_config)
1282 # `fdroid build` should build only the latest version by default since
1283 # most of the time the user will be building the most recent update
1285 options.latest = True
1286 elif len(local_metadata_files) > 1:
1287 raise FDroidException("Only one local metadata file allowed! Found: "
1288 + " ".join(local_metadata_files))
1290 if not os.path.isdir('metadata') and len(local_metadata_files) == 0:
1291 raise FDroidException("No app metadata found, nothing to process!")
1292 if not options.appid and not options.all:
1293 parser.error("option %s: If you really want to build all the apps, use --all" % "all")
1295 config = common.read_config(options)
1297 if config['build_server_always']:
1298 options.server = True
1299 if options.resetserver and not options.server:
1300 parser.error("option %s: Using --resetserver without --server makes no sense" % "resetserver")
1303 if not os.path.isdir(log_dir):
1304 logging.info("Creating log directory")
1305 os.makedirs(log_dir)
1308 if not os.path.isdir(tmp_dir):
1309 logging.info("Creating temporary directory")
1310 os.makedirs(tmp_dir)
1313 output_dir = tmp_dir
1315 output_dir = 'unsigned'
1316 if not os.path.isdir(output_dir):
1317 logging.info("Creating output directory")
1318 os.makedirs(output_dir)
1320 if config['archive_older'] != 0:
1321 also_check_dir = 'archive'
1323 also_check_dir = None
1328 if not os.path.isdir(build_dir):
1329 logging.info("Creating build directory")
1330 os.makedirs(build_dir)
1331 srclib_dir = os.path.join(build_dir, 'srclib')
1332 extlib_dir = os.path.join(build_dir, 'extlib')
1334 # Read all app and srclib metadata
1335 pkgs = common.read_pkg_args(options.appid, True)
1336 allapps = metadata.read_metadata(not options.onserver, pkgs)
1337 apps = common.read_app_args(options.appid, allapps, True)
1339 for appid, app in list(apps.items()):
1340 if (app.Disabled and not options.force) or not app.RepoType or not app.builds:
1344 raise FDroidException("No apps to process.")
1347 for app in apps.values():
1348 for build in reversed(app.builds):
1349 if build.disable and not options.force:
1351 app.builds = [build]
1356 site = mwclient.Site((config['wiki_protocol'], config['wiki_server']),
1357 path=config['wiki_path'])
1358 site.login(config['wiki_user'], config['wiki_password'])
1360 # Build applications...
1362 build_succeeded = []
1363 for appid, app in apps.items():
1367 for build in app.builds:
1369 tools_version_log = ''
1370 if not options.onserver:
1371 tools_version_log = get_android_tools_version_log(build.ndk_path())
1374 # For the first build of a particular app, we need to set up
1375 # the source repo. We can reuse it on subsequent builds, if
1378 vcs, build_dir = common.setup_vcs(app)
1381 logging.debug("Checking " + build.versionName)
1382 if trybuild(app, build, build_dir, output_dir, log_dir,
1383 also_check_dir, srclib_dir, extlib_dir,
1384 tmp_dir, repo_dir, vcs, options.test,
1385 options.server, options.force,
1386 options.onserver, options.refresh):
1387 toolslog = os.path.join(log_dir,
1388 common.get_toolsversion_logname(app, build))
1389 if not options.onserver and os.path.exists(toolslog):
1390 with open(toolslog, 'r') as f:
1391 tools_version_log = ''.join(f.readlines())
1394 if app.Binaries is not None:
1395 # This is an app where we build from source, and
1396 # verify the apk contents against a developer's
1397 # binary. We get that binary now, and save it
1398 # alongside our built one in the 'unsigend'
1401 url = url.replace('%v', build.versionName)
1402 url = url.replace('%c', str(build.versionCode))
1403 logging.info("...retrieving " + url)
1404 of = common.get_release_filename(app, build) + '.binary'
1405 of = os.path.join(output_dir, of)
1407 net.download_file(url, local_filename=of)
1408 except requests.exceptions.HTTPError as e:
1409 raise FDroidException(
1410 'Downloading Binaries from %s failed. %s' % (url, e))
1412 # Now we check weather the build can be verified to
1413 # match the supplied binary or not. Should the
1414 # comparison fail, we mark this build as a failure
1415 # and remove everything from the unsigend folder.
1416 with tempfile.TemporaryDirectory() as tmpdir:
1418 common.get_release_filename(app, build)
1420 os.path.join(output_dir, unsigned_apk)
1422 common.verify_apks(of, unsigned_apk, tmpdir)
1424 logging.debug('removing %s', unsigned_apk)
1425 os.remove(unsigned_apk)
1426 logging.debug('removing %s', of)
1428 compare_result = compare_result.split('\n')
1429 line_count = len(compare_result)
1430 compare_result = compare_result[:299]
1431 if line_count > len(compare_result):
1433 line_count - len(compare_result)
1434 compare_result.append('%d more lines ...' %
1436 compare_result = '\n'.join(compare_result)
1437 raise FDroidException('compared built binary '
1438 'to supplied reference '
1439 'binary but failed',
1442 logging.info('compared built binary to '
1443 'supplied reference binary '
1446 build_succeeded.append(app)
1447 wikilog = "Build succeeded"
1449 except VCSException as vcse:
1450 reason = str(vcse).split('\n', 1)[0] if options.verbose else str(vcse)
1451 logging.error("VCS error while building app %s: %s" % (
1455 failed_apps[appid] = vcse
1457 except FDroidException as e:
1458 with open(os.path.join(log_dir, appid + '.log'), 'a+') as f:
1459 f.write('\n\n============================================================\n')
1460 f.write('versionCode: %s\nversionName: %s\ncommit: %s\n' %
1461 (build.versionCode, build.versionName, build.commit))
1462 f.write('Build completed at '
1463 + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + '\n')
1464 f.write('\n' + tools_version_log + '\n')
1466 logging.error("Could not build app %s: %s" % (appid, e))
1469 failed_apps[appid] = e
1470 wikilog = e.get_wikitext()
1471 except Exception as e:
1472 logging.error("Could not build app %s due to unknown error: %s" % (
1473 appid, traceback.format_exc()))
1476 failed_apps[appid] = e
1479 if options.wiki and wikilog:
1481 # Write a page with the last build log for this version code
1482 lastbuildpage = appid + '/lastbuild_' + build.versionCode
1483 newpage = site.Pages[lastbuildpage]
1484 with open(os.path.join('tmp', 'fdroidserverid')) as fp:
1485 fdroidserverid = fp.read().rstrip()
1486 txt = "* build completed at " + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + '\n' \
1487 + '* fdroidserverid: [https://gitlab.com/fdroid/fdroidserver/commit/' \
1488 + fdroidserverid + ' ' + fdroidserverid + ']\n\n'
1489 if options.onserver:
1490 txt += '* buildserverid: [https://gitlab.com/fdroid/fdroidserver/commit/' \
1491 + buildserverid + ' ' + buildserverid + ']\n\n'
1492 txt += tools_version_log + '\n\n'
1493 txt += '== Build Log ==\n\n' + wikilog
1494 newpage.save(txt, summary='Build log')
1495 # Redirect from /lastbuild to the most recent build log
1496 newpage = site.Pages[appid + '/lastbuild']
1497 newpage.save('#REDIRECT [[' + lastbuildpage + ']]', summary='Update redirect')
1498 except Exception as e:
1499 logging.error("Error while attempting to publish build log: %s" % e)
1501 for app in build_succeeded:
1502 logging.info("success: %s" % (app.id))
1504 if not options.verbose:
1505 for fa in failed_apps:
1506 logging.info("Build for app %s failed:\n%s" % (fa, failed_apps[fa]))
1508 # perform a drozer scan of all successful builds
1509 if options.dscanner and build_succeeded:
1510 from .dscanner import DockerDriver
1512 docker = DockerDriver()
1515 for app in build_succeeded:
1517 logging.info("Need to sign the app before we can install it.")
1518 subprocess.call("fdroid publish {0}".format(app.id), shell=True)
1522 for f in os.listdir(repo_dir):
1523 if f.endswith('.apk') and f.startswith(app.id):
1524 apk_path = os.path.join(repo_dir, f)
1528 raise Exception("No signed APK found at path: {0}".format(apk_path))
1530 if not os.path.isdir(repo_dir):
1533 logging.info("Performing Drozer scan on {0}.".format(app))
1534 docker.perform_drozer_scan(apk_path, app.id, repo_dir)
1535 except Exception as e:
1536 logging.error(str(e))
1537 logging.error("An exception happened. Making sure to clean up")
1539 logging.info("Scan succeeded.")
1541 logging.info("Cleaning up after ourselves.")
1544 logging.info("Finished.")
1545 if len(build_succeeded) > 0:
1546 logging.info(str(len(build_succeeded)) + ' builds succeeded')
1547 if len(failed_apps) > 0:
1548 logging.info(str(len(failed_apps)) + ' builds failed')
1553 if __name__ == "__main__":