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/>.
31 from configparser import ConfigParser
32 from argparse import ArgumentParser
34 from gettext import ngettext
38 from . import metadata
41 from .common import FDroidPopen, SdkToolsPopen
42 from .exception import FDroidException, BuildException, VCSException
50 # Note that 'force' here also implies test mode.
51 def build_server(app, build, vcs, build_dir, output_dir, log_dir, force):
52 """Do a build on the builder vm.
54 :param app: app metadata dict
56 :param vcs: version control system controller object
57 :param build_dir: local source-code checkout of app
58 :param output_dir: target folder for the build result
67 raise BuildException("Paramiko is required to use the buildserver")
69 logging.getLogger("paramiko").setLevel(logging.INFO)
71 logging.getLogger("paramiko").setLevel(logging.WARN)
73 sshinfo = vmtools.get_clean_builder('builder')
77 buildserverid = subprocess.check_output(['vagrant', 'ssh', '-c',
78 'cat /home/vagrant/buildserverid'],
79 cwd='builder').rstrip()
81 # Open SSH connection...
82 logging.info("Connecting to virtual machine...")
83 sshs = paramiko.SSHClient()
84 sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
85 sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
86 port=sshinfo['port'], timeout=300,
87 look_for_keys=False, key_filename=sshinfo['idfile'])
89 homedir = '/home/' + sshinfo['user']
91 # Get an SFTP connection...
92 ftp = sshs.open_sftp()
93 ftp.get_channel().settimeout(60)
95 # Put all the necessary files in place...
98 # Helper to copy the contents of a directory to the server...
100 root = os.path.dirname(path)
101 main = os.path.basename(path)
103 for r, d, f in os.walk(path):
104 rr = os.path.relpath(r, root)
109 lfile = os.path.join(root, rr, ff)
110 if not os.path.islink(lfile):
112 ftp.chmod(ff, os.stat(lfile).st_mode)
113 for i in range(len(rr.split('/'))):
117 logging.info("Preparing server for build...")
118 serverpath = os.path.abspath(os.path.dirname(__file__))
119 ftp.mkdir('fdroidserver')
120 ftp.chdir('fdroidserver')
121 ftp.put(os.path.join(serverpath, '..', 'fdroid'), 'fdroid')
122 ftp.chmod('fdroid', 0o755)
123 send_dir(os.path.join(serverpath))
126 ftp.put(os.path.join(serverpath, '..', 'buildserver',
127 'config.buildserver.py'), 'config.py')
128 ftp.chmod('config.py', 0o600)
130 # Copy over the ID (head commit hash) of the fdroidserver in use...
131 subprocess.call('git rev-parse HEAD >' +
132 os.path.join(os.getcwd(), 'tmp', 'fdroidserverid'),
133 shell=True, cwd=serverpath)
134 ftp.put('tmp/fdroidserverid', 'fdroidserverid')
136 # Copy the metadata - just the file for this app...
137 ftp.mkdir('metadata')
139 ftp.chdir('metadata')
140 ftp.put(app.metadatapath, os.path.basename(app.metadatapath))
142 # And patches if there are any...
143 if os.path.exists(os.path.join('metadata', app.id)):
144 send_dir(os.path.join('metadata', app.id))
147 # Create the build directory...
152 # Copy any extlibs that are required...
154 ftp.chdir(homedir + '/build/extlib')
155 for lib in build.extlibs:
157 libsrc = os.path.join('build/extlib', lib)
158 if not os.path.exists(libsrc):
159 raise BuildException("Missing extlib {0}".format(libsrc))
162 if d not in ftp.listdir():
165 ftp.put(libsrc, lp[-1])
168 # Copy any srclibs that are required...
171 for lib in build.srclibs:
173 common.getsrclib(lib, 'build/srclib', basepath=True, prepare=False))
175 # If one was used for the main source, add that too.
176 basesrclib = vcs.getsrclib()
178 srclibpaths.append(basesrclib)
179 for name, number, lib in srclibpaths:
180 logging.info("Sending srclib '%s'" % lib)
181 ftp.chdir(homedir + '/build/srclib')
182 if not os.path.exists(lib):
183 raise BuildException("Missing srclib directory '" + lib + "'")
184 fv = '.fdroidvcs-' + name
185 ftp.put(os.path.join('build/srclib', fv), fv)
187 # Copy the metadata file too...
188 ftp.chdir(homedir + '/srclibs')
189 ftp.put(os.path.join('srclibs', name + '.txt'),
191 # Copy the main app source code
192 # (no need if it's a srclib)
193 if (not basesrclib) and os.path.exists(build_dir):
194 ftp.chdir(homedir + '/build')
195 fv = '.fdroidvcs-' + app.id
196 ftp.put(os.path.join('build', fv), fv)
199 # Execute the build script...
200 logging.info("Starting build...")
201 chan = sshs.get_transport().open_session()
203 cmdline = os.path.join(homedir, 'fdroidserver', 'fdroid')
204 cmdline += ' build --on-server'
206 cmdline += ' --force --test'
208 cmdline += ' --verbose'
210 cmdline += ' --skip-scan'
211 cmdline += " %s:%s" % (app.id, build.versionCode)
212 chan.exec_command('bash --login -c "' + cmdline + '"')
214 # Fetch build process output ...
216 cmd_stdout = chan.makefile('rb', 1024)
218 output += get_android_tools_version_log(build.ndk_path()).encode()
219 while not chan.exit_status_ready():
220 line = cmd_stdout.readline()
223 logging.debug("buildserver > " + str(line, 'utf-8').rstrip())
227 for line in cmd_stdout.readlines():
229 logging.debug("buildserver > " + str(line, 'utf-8').rstrip())
234 # Check build process exit status ...
235 logging.info("...getting exit status")
236 returncode = chan.recv_exit_status()
238 raise BuildException(
239 "Build.py failed on server for {0}:{1}".format(
240 app.id, build.versionName), None if options.verbose else str(output, 'utf-8'))
243 toolsversion_log = common.get_toolsversion_logname(app, build)
245 ftp.chdir(os.path.join(homedir, log_dir))
246 ftp.get(toolsversion_log, os.path.join(log_dir, toolsversion_log))
247 logging.debug('retrieved %s', toolsversion_log)
248 except Exception as e:
249 logging.warn('could not get %s from builder vm: %s' % (toolsversion_log, e))
251 # Retrieve the built files...
252 logging.info("Retrieving build output...")
254 ftp.chdir(homedir + '/tmp')
256 ftp.chdir(homedir + '/unsigned')
257 apkfile = common.get_release_filename(app, build)
258 tarball = common.getsrcname(app, build)
260 ftp.get(apkfile, os.path.join(output_dir, apkfile))
261 if not options.notarball:
262 ftp.get(tarball, os.path.join(output_dir, tarball))
264 raise BuildException(
265 "Build failed for {0}:{1} - missing output files".format(
266 app.id, build.versionName), None if options.verbose else str(output, 'utf-8'))
270 # Suspend the build server.
271 vm = vmtools.get_build_vm('builder')
275 def force_gradle_build_tools(build_dir, build_tools):
276 for root, dirs, files in os.walk(build_dir):
277 for filename in files:
278 if not filename.endswith('.gradle'):
280 path = os.path.join(root, filename)
281 if not os.path.isfile(path):
283 logging.debug("Forcing build-tools %s in %s" % (build_tools, path))
284 common.regsub_file(r"""(\s*)buildToolsVersion([\s=]+).*""",
285 r"""\1buildToolsVersion\2'%s'""" % build_tools,
289 def transform_first_char(string, method):
290 """Uses method() on the first character of string."""
294 return method(string)
295 return method(string[0]) + string[1:]
298 def has_native_code(apkobj):
299 """aapt checks if there are architecture folders under the lib/ folder
300 so we are simulating the same behaviour"""
301 arch_re = re.compile("^lib/(.*)/.*$")
302 arch = [file for file in apkobj.get_files() if arch_re.match(file)]
303 return False if not arch else True
306 def get_apk_metadata_aapt(apkfile):
307 """aapt function to extract versionCode, versionName, packageName and nativecode"""
313 p = SdkToolsPopen(['aapt', 'dump', 'badging', apkfile], output=False)
315 for line in p.output.splitlines():
316 if line.startswith("package:"):
317 pat = re.compile(".*name='([a-zA-Z0-9._]*)'.*")
321 pat = re.compile(".*versionCode='([0-9]*)'.*")
325 pat = re.compile(".*versionName='([^']*)'.*")
329 elif line.startswith("native-code:"):
330 nativecode = line[12:]
332 return vercode, version, foundid, nativecode
335 def get_apk_metadata_androguard(apkfile):
336 """androguard function to extract versionCode, versionName, packageName and nativecode"""
338 from androguard.core.bytecodes.apk import APK
339 apkobject = APK(apkfile)
341 raise BuildException("androguard library is not installed and aapt binary not found")
342 except FileNotFoundError:
343 raise BuildException("Could not open apk file for metadata analysis")
345 if not apkobject.is_valid_APK():
346 raise BuildException("Invalid APK provided")
348 foundid = apkobject.get_package()
349 vercode = apkobject.get_androidversion_code()
350 version = apkobject.get_androidversion_name()
351 nativecode = has_native_code(apkobject)
353 return vercode, version, foundid, nativecode
356 def get_metadata_from_apk(app, build, apkfile):
357 """get the required metadata from the built APK"""
359 if common.SdkToolsPopen(['aapt', 'version'], output=False):
360 vercode, version, foundid, nativecode = get_apk_metadata_aapt(apkfile)
362 vercode, version, foundid, nativecode = get_apk_metadata_androguard(apkfile)
364 # Ignore empty strings or any kind of space/newline chars that we don't
366 if nativecode is not None:
367 nativecode = nativecode.strip()
368 nativecode = None if not nativecode else nativecode
370 if build.buildjni and build.buildjni != ['no']:
371 if nativecode is None:
372 raise BuildException("Native code should have been built but none was packaged")
374 vercode = build.versionCode
375 version = build.versionName
376 if not version or not vercode:
377 raise BuildException("Could not find version information in build in output")
379 raise BuildException("Could not find package ID in output")
380 if foundid != app.id:
381 raise BuildException("Wrong package ID - build " + foundid + " but expected " + app.id)
383 return vercode, version
386 def build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver, refresh):
387 """Do a build locally."""
388 ndk_path = build.ndk_path()
389 if build.ndk or (build.buildjni and build.buildjni != ['no']):
391 logging.critical("Android NDK version '%s' could not be found!" % build.ndk or 'r12b')
392 logging.critical("Configured versions:")
393 for k, v in config['ndk_paths'].items():
394 if k.endswith("_orig"):
396 logging.critical(" %s: %s" % (k, v))
397 raise FDroidException()
398 elif not os.path.isdir(ndk_path):
399 logging.critical("Android NDK '%s' is not a directory!" % ndk_path)
400 raise FDroidException()
402 common.set_FDroidPopen_env(build)
404 # create ..._toolsversion.log when running in builder vm
406 # before doing anything, run the sudo commands to setup the VM
408 logging.info("Running 'sudo' commands in %s" % os.getcwd())
410 p = FDroidPopen(['sudo', 'bash', '-x', '-c', build.sudo])
411 if p.returncode != 0:
412 raise BuildException("Error running sudo command for %s:%s" %
413 (app.id, build.versionName), p.output)
415 log_path = os.path.join(log_dir,
416 common.get_toolsversion_logname(app, build))
417 with open(log_path, 'w') as f:
418 f.write(get_android_tools_version_log(build.ndk_path()))
421 logging.warning('%s:%s runs this on the buildserver with sudo:\n\t%s'
422 % (app.id, build.versionName, build.sudo))
424 # Prepare the source code...
425 root_dir, srclibpaths = common.prepare_source(vcs, app, build,
426 build_dir, srclib_dir,
427 extlib_dir, onserver, refresh)
429 # We need to clean via the build tool in case the binary dirs are
430 # different from the default ones
433 bmethod = build.build_method()
434 if bmethod == 'maven':
435 logging.info("Cleaning Maven project...")
436 cmd = [config['mvn3'], 'clean', '-Dandroid.sdk.path=' + config['sdk_path']]
438 if '@' in build.maven:
439 maven_dir = os.path.join(root_dir, build.maven.split('@', 1)[1])
440 maven_dir = os.path.normpath(maven_dir)
444 p = FDroidPopen(cmd, cwd=maven_dir)
446 elif bmethod == 'gradle':
448 logging.info("Cleaning Gradle project...")
450 if build.preassemble:
451 gradletasks += build.preassemble
453 flavours = build.gradle
454 if flavours == ['yes']:
457 flavours_cmd = ''.join([transform_first_char(flav, str.upper) for flav in flavours])
459 gradletasks += ['assemble' + flavours_cmd + 'Release']
461 if config['force_build_tools']:
462 force_gradle_build_tools(build_dir, config['build_tools'])
463 for name, number, libpath in srclibpaths:
464 force_gradle_build_tools(libpath, config['build_tools'])
466 cmd = [config['gradle']]
467 if build.gradleprops:
468 cmd += ['-P' + kv for kv in build.gradleprops]
472 p = FDroidPopen(cmd, cwd=root_dir)
474 elif bmethod == 'kivy':
477 elif bmethod == 'buildozer':
480 elif bmethod == 'ant':
481 logging.info("Cleaning Ant project...")
482 p = FDroidPopen(['ant', 'clean'], cwd=root_dir)
484 if p is not None and p.returncode != 0:
485 raise BuildException("Error cleaning %s:%s" %
486 (app.id, build.versionName), p.output)
488 for root, dirs, files in os.walk(build_dir):
493 shutil.rmtree(os.path.join(root, d))
498 os.remove(os.path.join(root, f))
500 if 'build.gradle' in files:
501 # Even when running clean, gradle stores task/artifact caches in
502 # .gradle/ as binary files. To avoid overcomplicating the scanner,
503 # manually delete them, just like `gradle clean` should have removed
505 del_dirs(['build', '.gradle'])
506 del_files(['gradlew', 'gradlew.bat'])
508 if 'pom.xml' in files:
511 if any(f in files for f in ['ant.properties', 'project.properties', 'build.xml']):
512 del_dirs(['bin', 'gen'])
519 raise BuildException("Refusing to skip source scan since scandelete is present")
521 # Scan before building...
522 logging.info("Scanning source for common problems...")
523 count = scanner.scan_source(build_dir, build)
526 logging.warning(ngettext('Scanner found {} problem',
527 'Scanner found {} problems', count).format(count))
529 raise BuildException(ngettext(
530 "Can't build due to {} error while scanning",
531 "Can't build due to {} errors while scanning", count).format(count))
533 if not options.notarball:
534 # Build the source tarball right before we build the release...
535 logging.info("Creating source tarball...")
536 tarname = common.getsrcname(app, build)
537 tarball = tarfile.open(os.path.join(tmp_dir, tarname), "w:gz")
540 return any(f.endswith(s) for s in ['.svn', '.git', '.hg', '.bzr'])
541 tarball.add(build_dir, tarname, exclude=tarexc)
544 # Run a build command if one is required...
546 logging.info("Running 'build' commands in %s" % root_dir)
547 cmd = common.replace_config_vars(build.build, build)
549 # Substitute source library paths into commands...
550 for name, number, libpath in srclibpaths:
551 libpath = os.path.relpath(libpath, root_dir)
552 cmd = cmd.replace('$$' + name + '$$', libpath)
554 p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
556 if p.returncode != 0:
557 raise BuildException("Error running build command for %s:%s" %
558 (app.id, build.versionName), p.output)
560 # Build native stuff if required...
561 if build.buildjni and build.buildjni != ['no']:
562 logging.info("Building the native code")
563 jni_components = build.buildjni
565 if jni_components == ['yes']:
566 jni_components = ['']
567 cmd = [os.path.join(ndk_path, "ndk-build"), "-j1"]
568 for d in jni_components:
570 logging.info("Building native code in '%s'" % d)
572 logging.info("Building native code in the main project")
573 manifest = os.path.join(root_dir, d, 'AndroidManifest.xml')
574 if os.path.exists(manifest):
575 # Read and write the whole AM.xml to fix newlines and avoid
576 # the ndk r8c or later 'wordlist' errors. The outcome of this
577 # under gnu/linux is the same as when using tools like
578 # dos2unix, but the native python way is faster and will
579 # work in non-unix systems.
580 manifest_text = open(manifest, 'U').read()
581 open(manifest, 'w').write(manifest_text)
582 # In case the AM.xml read was big, free the memory
584 p = FDroidPopen(cmd, cwd=os.path.join(root_dir, d))
585 if p.returncode != 0:
586 raise BuildException("NDK build failed for %s:%s" % (app.id, build.versionName), p.output)
589 # Build the release...
590 if bmethod == 'maven':
591 logging.info("Building Maven project...")
593 if '@' in build.maven:
594 maven_dir = os.path.join(root_dir, build.maven.split('@', 1)[1])
598 mvncmd = [config['mvn3'], '-Dandroid.sdk.path=' + config['sdk_path'],
599 '-Dmaven.jar.sign.skip=true', '-Dmaven.test.skip=true',
600 '-Dandroid.sign.debug=false', '-Dandroid.release=true',
603 target = build.target.split('-')[1]
604 common.regsub_file(r'<platform>[0-9]*</platform>',
605 r'<platform>%s</platform>' % target,
606 os.path.join(root_dir, 'pom.xml'))
607 if '@' in build.maven:
608 common.regsub_file(r'<platform>[0-9]*</platform>',
609 r'<platform>%s</platform>' % target,
610 os.path.join(maven_dir, 'pom.xml'))
612 p = FDroidPopen(mvncmd, cwd=maven_dir)
614 bindir = os.path.join(root_dir, 'target')
616 elif bmethod == 'kivy':
617 logging.info("Building Kivy project...")
619 spec = os.path.join(root_dir, 'buildozer.spec')
620 if not os.path.exists(spec):
621 raise BuildException("Expected to find buildozer-compatible spec at {0}"
624 defaults = {'orientation': 'landscape', 'icon': '',
625 'permissions': '', 'android.api': "18"}
626 bconfig = ConfigParser(defaults, allow_no_value=True)
629 distdir = os.path.join('python-for-android', 'dist', 'fdroid')
630 if os.path.exists(distdir):
631 shutil.rmtree(distdir)
633 modules = bconfig.get('app', 'requirements').split(',')
635 cmd = 'ANDROIDSDK=' + config['sdk_path']
636 cmd += ' ANDROIDNDK=' + ndk_path
637 cmd += ' ANDROIDNDKVER=' + build.ndk
638 cmd += ' ANDROIDAPI=' + str(bconfig.get('app', 'android.api'))
639 cmd += ' VIRTUALENV=virtualenv'
640 cmd += ' ./distribute.sh'
641 cmd += ' -m ' + "'" + ' '.join(modules) + "'"
643 p = subprocess.Popen(cmd, cwd='python-for-android', shell=True)
644 if p.returncode != 0:
645 raise BuildException("Distribute build failed")
647 cid = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
649 raise BuildException("Package ID mismatch between metadata and spec")
651 orientation = bconfig.get('app', 'orientation', 'landscape')
652 if orientation == 'all':
653 orientation = 'sensor'
657 '--name', bconfig.get('app', 'title'),
659 '--version', bconfig.get('app', 'version'),
660 '--orientation', orientation
663 perms = bconfig.get('app', 'permissions')
664 for perm in perms.split(','):
665 cmd.extend(['--permission', perm])
667 if config.get('app', 'fullscreen') == 0:
668 cmd.append('--window')
670 icon = bconfig.get('app', 'icon.filename')
672 cmd.extend(['--icon', os.path.join(root_dir, icon)])
674 cmd.append('release')
675 p = FDroidPopen(cmd, cwd=distdir)
677 elif bmethod == 'buildozer':
678 logging.info("Building Kivy project using buildozer...")
680 # parse buildozer.spez
681 spec = os.path.join(root_dir, 'buildozer.spec')
682 if not os.path.exists(spec):
683 raise BuildException("Expected to find buildozer-compatible spec at {0}"
685 defaults = {'orientation': 'landscape', 'icon': '',
686 'permissions': '', 'android.api': "19"}
687 bconfig = ConfigParser(defaults, allow_no_value=True)
690 # update spec with sdk and ndk locations to prevent buildozer from
692 loc_ndk = common.env['ANDROID_NDK']
693 loc_sdk = common.env['ANDROID_SDK']
694 if loc_ndk == '$ANDROID_NDK':
695 loc_ndk = loc_sdk + '/ndk-bundle'
700 bc_ndk = bconfig.get('app', 'android.sdk_path')
704 bc_sdk = bconfig.get('app', 'android.ndk_path')
709 bconfig.set('app', 'android.sdk_path', loc_sdk)
711 bconfig.set('app', 'android.ndk_path', loc_ndk)
713 fspec = open(spec, 'w')
717 logging.info("sdk_path = %s" % loc_sdk)
718 logging.info("ndk_path = %s" % loc_ndk)
722 cmd = ['buildozer', 'android', 'release']
724 p = FDroidPopen(cmd, cwd=root_dir)
728 # buidozer not installed ? clone repo and run
729 if (p is None or p.returncode != 0):
730 cmd = ['git', 'clone', 'https://github.com/kivy/buildozer.git']
731 p = subprocess.Popen(cmd, cwd=root_dir, shell=False)
733 if p.returncode != 0:
734 raise BuildException("Distribute build failed")
736 cmd = ['python', 'buildozer/buildozer/scripts/client.py', 'android', 'release']
737 p = FDroidPopen(cmd, cwd=root_dir)
740 # Signing will fail if not set by environnment vars (cf. p4a docs).
741 # But the unsigned apk will be ok.
744 elif bmethod == 'gradle':
745 logging.info("Building Gradle project...")
747 cmd = [config['gradle']]
748 if build.gradleprops:
749 cmd += ['-P' + kv for kv in build.gradleprops]
753 p = FDroidPopen(cmd, cwd=root_dir)
755 elif bmethod == 'ant':
756 logging.info("Building Ant project...")
758 if build.antcommands:
759 cmd += build.antcommands
762 p = FDroidPopen(cmd, cwd=root_dir)
764 bindir = os.path.join(root_dir, 'bin')
766 if p is not None and p.returncode != 0:
767 raise BuildException("Build failed for %s:%s" % (app.id, build.versionName), p.output)
768 logging.info("Successfully built version " + build.versionName + ' of ' + app.id)
770 omethod = build.output_method()
771 if omethod == 'maven':
772 stdout_apk = '\n'.join([
773 line for line in p.output.splitlines() if any(
774 a in line for a in ('.apk', '.ap_', '.jar'))])
775 m = re.match(r".*^\[INFO\] .*apkbuilder.*/([^/]*)\.apk",
776 stdout_apk, re.S | re.M)
778 m = re.match(r".*^\[INFO\] Creating additional unsigned apk file .*/([^/]+)\.apk[^l]",
779 stdout_apk, re.S | re.M)
781 m = re.match(r'.*^\[INFO\] [^$]*aapt \[package,[^$]*' + bindir + r'/([^/]+)\.ap[_k][,\]]',
782 stdout_apk, re.S | re.M)
785 m = re.match(r".*^\[INFO\] Building jar: .*/" + bindir + r"/(.+)\.jar",
786 stdout_apk, re.S | re.M)
788 raise BuildException('Failed to find output')
790 src = os.path.join(bindir, src) + '.apk'
791 elif omethod == 'kivy':
792 src = os.path.join('python-for-android', 'dist', 'default', 'bin',
793 '{0}-{1}-release.apk'.format(
794 bconfig.get('app', 'title'),
795 bconfig.get('app', 'version')))
797 elif omethod == 'buildozer':
800 os.path.join(root_dir, '.buildozer', 'android', 'platform', 'build', 'dists', bconfig.get('app', 'title'), 'bin'),
802 for apkglob in ['*-release-unsigned.apk', '*-unsigned.apk', '*.apk']:
803 apks = glob.glob(os.path.join(apks_dir, apkglob))
806 raise BuildException('More than one resulting apks found in %s' % apks_dir,
815 raise BuildException('Failed to find any output apks')
817 elif omethod == 'gradle':
820 # gradle plugin >= 3.0
821 os.path.join(root_dir, 'build', 'outputs', 'apk', 'release'),
822 # gradle plugin < 3.0 and >= 0.11
823 os.path.join(root_dir, 'build', 'outputs', 'apk'),
825 os.path.join(root_dir, 'build', 'apk'),
827 # If we build with gradle flavours with gradle plugin >= 3.0 the apk will be in
828 # a subdirectory corresponding to the flavour command used, but with different
831 apk_dirs.append(os.path.join(root_dir, 'build', 'outputs', 'apk', transform_first_char(flavours_cmd, str.lower), 'release'))
832 for apks_dir in apk_dirs:
833 for apkglob in ['*-release-unsigned.apk', '*-unsigned.apk', '*.apk']:
834 apks = glob.glob(os.path.join(apks_dir, apkglob))
837 raise BuildException('More than one resulting apks found in %s' % apks_dir,
846 raise BuildException('Failed to find any output apks')
848 elif omethod == 'ant':
849 stdout_apk = '\n'.join([
850 line for line in p.output.splitlines() if '.apk' in line])
851 src = re.match(r".*^.*Creating (.+) for release.*$.*", stdout_apk,
852 re.S | re.M).group(1)
853 src = os.path.join(bindir, src)
854 elif omethod == 'raw':
855 output_path = common.replace_build_vars(build.output, build)
856 globpath = os.path.join(root_dir, output_path)
857 apks = glob.glob(globpath)
859 raise BuildException('Multiple apks match %s' % globpath, '\n'.join(apks))
861 raise BuildException('No apks match %s' % globpath)
862 src = os.path.normpath(apks[0])
864 # Make sure it's not debuggable...
865 if common.isApkAndDebuggable(src):
866 raise BuildException("APK is debuggable")
868 # By way of a sanity check, make sure the version and version
869 # code in our new apk match what we expect...
870 logging.debug("Checking " + src)
871 if not os.path.exists(src):
872 raise BuildException("Unsigned apk is not at expected location of " + src)
874 if common.get_file_extension(src) == 'apk':
875 vercode, version = get_metadata_from_apk(app, build, src)
876 if (version != build.versionName or vercode != build.versionCode):
877 raise BuildException(("Unexpected version/version code in output;"
878 " APK: '%s' / '%s', "
879 " Expected: '%s' / '%s'")
880 % (version, str(vercode), build.versionName,
881 str(build.versionCode)))
883 vercode = build.versionCode
884 version = build.versionName
886 # Add information for 'fdroid verify' to be able to reproduce the build
889 metadir = os.path.join(tmp_dir, 'META-INF')
890 if not os.path.exists(metadir):
892 homedir = os.path.expanduser('~')
893 for fn in ['buildserverid', 'fdroidserverid']:
894 shutil.copyfile(os.path.join(homedir, fn),
895 os.path.join(metadir, fn))
896 subprocess.call(['jar', 'uf', os.path.abspath(src),
897 'META-INF/' + fn], cwd=tmp_dir)
899 # Copy the unsigned apk to our destination directory for further
900 # processing (by publish.py)...
901 dest = os.path.join(output_dir, common.get_release_filename(app, build))
902 shutil.copyfile(src, dest)
904 # Move the source tarball into the output directory...
905 if output_dir != tmp_dir and not options.notarball:
906 shutil.move(os.path.join(tmp_dir, tarname),
907 os.path.join(output_dir, tarname))
910 def trybuild(app, build, build_dir, output_dir, log_dir, also_check_dir,
911 srclib_dir, extlib_dir, tmp_dir, repo_dir, vcs, test,
912 server, force, onserver, refresh):
914 Build a particular version of an application, if it needs building.
916 :param output_dir: The directory where the build output will go. Usually
917 this is the 'unsigned' directory.
918 :param repo_dir: The repo directory - used for checking if the build is
920 :param also_check_dir: An additional location for checking if the build
921 is necessary (usually the archive repo)
922 :param test: True if building in test mode, in which case the build will
923 always happen, even if the output already exists. In test mode, the
924 output directory should be a temporary location, not any of the real
927 :returns: True if the build was done, False if it wasn't necessary.
930 dest_file = common.get_release_filename(app, build)
932 dest = os.path.join(output_dir, dest_file)
933 dest_repo = os.path.join(repo_dir, dest_file)
936 if os.path.exists(dest) or os.path.exists(dest_repo):
940 dest_also = os.path.join(also_check_dir, dest_file)
941 if os.path.exists(dest_also):
944 if build.disable and not options.force:
947 logging.info("Building version %s (%s) of %s" % (
948 build.versionName, build.versionCode, app.id))
951 # When using server mode, still keep a local cache of the repo, by
952 # grabbing the source now.
953 vcs.gotorevision(build.commit)
955 build_server(app, build, vcs, build_dir, output_dir, log_dir, force)
957 build_local(app, build, vcs, build_dir, output_dir, log_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver, refresh)
961 def get_android_tools_versions(ndk_path=None):
962 '''get a list of the versions of all installed Android SDK/NDK components'''
965 sdk_path = config['sdk_path']
966 if sdk_path[-1] != '/':
970 ndk_release_txt = os.path.join(ndk_path, 'RELEASE.TXT')
971 if os.path.isfile(ndk_release_txt):
972 with open(ndk_release_txt, 'r') as fp:
973 components.append((os.path.basename(ndk_path), fp.read()[:-1]))
975 pattern = re.compile('^Pkg.Revision=(.+)', re.MULTILINE)
976 for root, dirs, files in os.walk(sdk_path):
977 if 'source.properties' in files:
978 source_properties = os.path.join(root, 'source.properties')
979 with open(source_properties, 'r') as fp:
980 m = pattern.search(fp.read())
982 components.append((root[len(sdk_path):], m.group(1)))
987 def get_android_tools_version_log(ndk_path):
988 '''get a list of the versions of all installed Android SDK/NDK components'''
989 log = '== Installed Android Tools ==\n\n'
990 components = get_android_tools_versions(ndk_path)
991 for name, version in sorted(components):
992 log += '* ' + name + ' (' + version + ')\n'
997 def parse_commandline():
998 """Parse the command line. Returns options, parser."""
1000 parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
1001 common.setup_global_opts(parser)
1002 parser.add_argument("appid", nargs='*', help="app-id with optional versionCode in the form APPID[:VERCODE]")
1003 parser.add_argument("-l", "--latest", action="store_true", default=False,
1004 help="Build only the latest version of each package")
1005 parser.add_argument("-s", "--stop", action="store_true", default=False,
1006 help="Make the build stop on exceptions")
1007 parser.add_argument("-t", "--test", action="store_true", default=False,
1008 help="Test mode - put output in the tmp directory only, and always build, even if the output already exists.")
1009 parser.add_argument("--server", action="store_true", default=False,
1010 help="Use build server")
1011 parser.add_argument("--resetserver", action="store_true", default=False,
1012 help="Reset and create a brand new build server, even if the existing one appears to be ok.")
1013 parser.add_argument("--on-server", dest="onserver", action="store_true", default=False,
1014 help="Specify that we're running on the build server")
1015 parser.add_argument("--skip-scan", dest="skipscan", action="store_true", default=False,
1016 help="Skip scanning the source code for binaries and other problems")
1017 parser.add_argument("--dscanner", action="store_true", default=False,
1018 help="Setup an emulator, install the apk on it and perform a drozer scan")
1019 parser.add_argument("--no-tarball", dest="notarball", action="store_true", default=False,
1020 help="Don't create a source tarball, useful when testing a build")
1021 parser.add_argument("--no-refresh", dest="refresh", action="store_false", default=True,
1022 help="Don't refresh the repository, useful when testing a build with no internet connection")
1023 parser.add_argument("-f", "--force", action="store_true", default=False,
1024 help="Force build of disabled apps, and carries on regardless of scan problems. Only allowed in test mode.")
1025 parser.add_argument("-a", "--all", action="store_true", default=False,
1026 help="Build all applications available")
1027 parser.add_argument("-w", "--wiki", default=False, action="store_true",
1028 help="Update the wiki")
1029 metadata.add_metadata_arguments(parser)
1030 options = parser.parse_args()
1031 metadata.warnings_action = options.W
1033 # Force --stop with --on-server to get correct exit code
1034 if options.onserver:
1037 if options.force and not options.test:
1038 parser.error("option %s: Force is only allowed in test mode" % "force")
1040 return options, parser
1045 buildserverid = None
1050 global options, config, buildserverid
1052 options, parser = parse_commandline()
1054 # The defaults for .fdroid.* metadata that is included in a git repo are
1055 # different than for the standard metadata/ layout because expectations
1056 # are different. In this case, the most common user will be the app
1057 # developer working on the latest update of the app on their own machine.
1058 local_metadata_files = common.get_local_metadata_files()
1059 if len(local_metadata_files) == 1: # there is local metadata in an app's source
1060 config = dict(common.default_config)
1061 # `fdroid build` should build only the latest version by default since
1062 # most of the time the user will be building the most recent update
1064 options.latest = True
1065 elif len(local_metadata_files) > 1:
1066 raise FDroidException("Only one local metadata file allowed! Found: "
1067 + " ".join(local_metadata_files))
1069 if not os.path.isdir('metadata') and len(local_metadata_files) == 0:
1070 raise FDroidException("No app metadata found, nothing to process!")
1071 if not options.appid and not options.all:
1072 parser.error("option %s: If you really want to build all the apps, use --all" % "all")
1074 config = common.read_config(options)
1076 if config['build_server_always']:
1077 options.server = True
1078 if options.resetserver and not options.server:
1079 parser.error("option %s: Using --resetserver without --server makes no sense" % "resetserver")
1082 if not os.path.isdir(log_dir):
1083 logging.info("Creating log directory")
1084 os.makedirs(log_dir)
1087 if not os.path.isdir(tmp_dir):
1088 logging.info("Creating temporary directory")
1089 os.makedirs(tmp_dir)
1092 output_dir = tmp_dir
1094 output_dir = 'unsigned'
1095 if not os.path.isdir(output_dir):
1096 logging.info("Creating output directory")
1097 os.makedirs(output_dir)
1099 if config['archive_older'] != 0:
1100 also_check_dir = 'archive'
1102 also_check_dir = None
1107 if not os.path.isdir(build_dir):
1108 logging.info("Creating build directory")
1109 os.makedirs(build_dir)
1110 srclib_dir = os.path.join(build_dir, 'srclib')
1111 extlib_dir = os.path.join(build_dir, 'extlib')
1113 # Read all app and srclib metadata
1114 pkgs = common.read_pkg_args(options.appid, True)
1115 allapps = metadata.read_metadata(not options.onserver, pkgs)
1116 apps = common.read_app_args(options.appid, allapps, True)
1118 for appid, app in list(apps.items()):
1119 if (app.Disabled and not options.force) or not app.RepoType or not app.builds:
1123 raise FDroidException("No apps to process.")
1126 for app in apps.values():
1127 for build in reversed(app.builds):
1128 if build.disable and not options.force:
1130 app.builds = [build]
1135 site = mwclient.Site((config['wiki_protocol'], config['wiki_server']),
1136 path=config['wiki_path'])
1137 site.login(config['wiki_user'], config['wiki_password'])
1139 # Build applications...
1141 build_succeeded = []
1142 for appid, app in apps.items():
1146 for build in app.builds:
1148 tools_version_log = ''
1149 if not options.onserver:
1150 tools_version_log = get_android_tools_version_log(build.ndk_path())
1153 # For the first build of a particular app, we need to set up
1154 # the source repo. We can reuse it on subsequent builds, if
1157 vcs, build_dir = common.setup_vcs(app)
1160 logging.debug("Checking " + build.versionName)
1161 if trybuild(app, build, build_dir, output_dir, log_dir,
1162 also_check_dir, srclib_dir, extlib_dir,
1163 tmp_dir, repo_dir, vcs, options.test,
1164 options.server, options.force,
1165 options.onserver, options.refresh):
1166 toolslog = os.path.join(log_dir,
1167 common.get_toolsversion_logname(app, build))
1168 if not options.onserver and os.path.exists(toolslog):
1169 with open(toolslog, 'r') as f:
1170 tools_version_log = ''.join(f.readlines())
1173 if app.Binaries is not None:
1174 # This is an app where we build from source, and
1175 # verify the apk contents against a developer's
1176 # binary. We get that binary now, and save it
1177 # alongside our built one in the 'unsigend'
1180 url = url.replace('%v', build.versionName)
1181 url = url.replace('%c', str(build.versionCode))
1182 logging.info("...retrieving " + url)
1183 of = re.sub(r'.apk$', '.binary.apk', common.get_release_filename(app, build))
1184 of = os.path.join(output_dir, of)
1186 net.download_file(url, local_filename=of)
1187 except requests.exceptions.HTTPError as e:
1188 raise FDroidException(
1189 'Downloading Binaries from %s failed. %s' % (url, e))
1191 # Now we check weather the build can be verified to
1192 # match the supplied binary or not. Should the
1193 # comparison fail, we mark this build as a failure
1194 # and remove everything from the unsigend folder.
1195 with tempfile.TemporaryDirectory() as tmpdir:
1197 common.get_release_filename(app, build)
1199 os.path.join(output_dir, unsigned_apk)
1201 common.verify_apks(of, unsigned_apk, tmpdir)
1203 logging.debug('removing %s', unsigned_apk)
1204 os.remove(unsigned_apk)
1205 logging.debug('removing %s', of)
1207 compare_result = compare_result.split('\n')
1208 line_count = len(compare_result)
1209 compare_result = compare_result[:299]
1210 if line_count > len(compare_result):
1212 line_count - len(compare_result)
1213 compare_result.append('%d more lines ...' %
1215 compare_result = '\n'.join(compare_result)
1216 raise FDroidException('compared built binary '
1217 'to supplied reference '
1218 'binary but failed',
1221 logging.info('compared built binary to '
1222 'supplied reference binary '
1225 build_succeeded.append(app)
1226 wikilog = "Build succeeded"
1228 except VCSException as vcse:
1229 reason = str(vcse).split('\n', 1)[0] if options.verbose else str(vcse)
1230 logging.error("VCS error while building app %s: %s" % (
1234 failed_apps[appid] = vcse
1236 except FDroidException as e:
1237 with open(os.path.join(log_dir, appid + '.log'), 'a+') as f:
1238 f.write('\n\n============================================================\n')
1239 f.write('versionCode: %s\nversionName: %s\ncommit: %s\n' %
1240 (build.versionCode, build.versionName, build.commit))
1241 f.write('Build completed at '
1242 + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + '\n')
1243 f.write('\n' + tools_version_log + '\n')
1245 logging.error("Could not build app %s: %s" % (appid, e))
1248 failed_apps[appid] = e
1249 wikilog = e.get_wikitext()
1250 except Exception as e:
1251 logging.error("Could not build app %s due to unknown error: %s" % (
1252 appid, traceback.format_exc()))
1255 failed_apps[appid] = e
1258 if options.wiki and wikilog:
1260 # Write a page with the last build log for this version code
1261 lastbuildpage = appid + '/lastbuild_' + build.versionCode
1262 newpage = site.Pages[lastbuildpage]
1263 with open(os.path.join('tmp', 'fdroidserverid')) as fp:
1264 fdroidserverid = fp.read().rstrip()
1265 txt = "* build completed at " + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + '\n' \
1266 + '* fdroidserverid: [https://gitlab.com/fdroid/fdroidserver/commit/' \
1267 + fdroidserverid + ' ' + fdroidserverid + ']\n\n'
1268 if options.onserver:
1269 txt += '* buildserverid: [https://gitlab.com/fdroid/fdroidserver/commit/' \
1270 + buildserverid + ' ' + buildserverid + ']\n\n'
1271 txt += tools_version_log + '\n\n'
1272 txt += '== Build Log ==\n\n' + wikilog
1273 newpage.save(txt, summary='Build log')
1274 # Redirect from /lastbuild to the most recent build log
1275 newpage = site.Pages[appid + '/lastbuild']
1276 newpage.save('#REDIRECT [[' + lastbuildpage + ']]', summary='Update redirect')
1277 except Exception as e:
1278 logging.error("Error while attempting to publish build log: %s" % e)
1280 for app in build_succeeded:
1281 logging.info("success: %s" % (app.id))
1283 if not options.verbose:
1284 for fa in failed_apps:
1285 logging.info("Build for app %s failed:\n%s" % (fa, failed_apps[fa]))
1287 # perform a drozer scan of all successful builds
1288 if options.dscanner and build_succeeded:
1289 from .dscanner import DockerDriver
1291 docker = DockerDriver()
1294 for app in build_succeeded:
1296 logging.info("Need to sign the app before we can install it.")
1297 subprocess.call("fdroid publish {0}".format(app.id), shell=True)
1301 for f in os.listdir(repo_dir):
1302 if f.endswith('.apk') and f.startswith(app.id):
1303 apk_path = os.path.join(repo_dir, f)
1307 raise Exception("No signed APK found at path: {0}".format(apk_path))
1309 if not os.path.isdir(repo_dir):
1312 logging.info("Performing Drozer scan on {0}.".format(app))
1313 docker.perform_drozer_scan(apk_path, app.id, repo_dir)
1314 except Exception as e:
1315 logging.error(str(e))
1316 logging.error("An exception happened. Making sure to clean up")
1318 logging.info("Scan succeeded.")
1320 logging.info("Cleaning up after ourselves.")
1323 logging.info("Finished.")
1324 if len(build_succeeded) > 0:
1325 logging.info(ngettext("{} build succeeded",
1326 "{} builds succeeded", len(build_succeeded)).format(len(build_succeeded)))
1327 if len(failed_apps) > 0:
1328 logging.info(ngettext("{} build failed",
1329 "{} builds failed", len(failed_apps)).format(len(failed_apps)))
1334 if __name__ == "__main__":