chiark / gitweb /
Properly capitalize all flavours (fixes org.ligi.fast)
[fdroidserver.git] / fdroidserver / build.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
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>
7 #
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.
12 #
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.
17 #
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/>.
20
21 import sys
22 import os
23 import shutil
24 import glob
25 import subprocess
26 import re
27 import tarfile
28 import traceback
29 import time
30 import json
31 from ConfigParser import ConfigParser
32 from optparse import OptionParser, OptionError
33 from distutils.version import LooseVersion
34 import logging
35
36 import common
37 import metadata
38 from common import FDroidException, BuildException, VCSException, FDroidPopen, SdkToolsPopen
39
40 try:
41     import paramiko
42 except ImportError:
43     pass
44
45
46 def get_builder_vm_id():
47     vd = os.path.join('builder', '.vagrant')
48     if os.path.isdir(vd):
49         # Vagrant 1.2 (and maybe 1.1?) it's a directory tree...
50         with open(os.path.join(vd, 'machines', 'default',
51                                'virtualbox', 'id')) as vf:
52             id = vf.read()
53         return id
54     else:
55         # Vagrant 1.0 - it's a json file...
56         with open(os.path.join('builder', '.vagrant')) as vf:
57             v = json.load(vf)
58         return v['active']['default']
59
60
61 def got_valid_builder_vm():
62     """Returns True if we have a valid-looking builder vm
63     """
64     if not os.path.exists(os.path.join('builder', 'Vagrantfile')):
65         return False
66     vd = os.path.join('builder', '.vagrant')
67     if not os.path.exists(vd):
68         return False
69     if not os.path.isdir(vd):
70         # Vagrant 1.0 - if the directory is there, it's valid...
71         return True
72     # Vagrant 1.2 - the directory can exist, but the id can be missing...
73     if not os.path.exists(os.path.join(vd, 'machines', 'default',
74                                        'virtualbox', 'id')):
75         return False
76     return True
77
78
79 def vagrant(params, cwd=None, printout=False):
80     """Run a vagrant command.
81
82     :param: list of parameters to pass to vagrant
83     :cwd: directory to run in, or None for current directory
84     :returns: (ret, out) where ret is the return code, and out
85                is the stdout (and stderr) from vagrant
86     """
87     p = FDroidPopen(['vagrant'] + params, cwd=cwd)
88     return (p.returncode, p.output)
89
90
91 def get_vagrant_sshinfo():
92     """Get ssh connection info for a vagrant VM
93
94     :returns: A dictionary containing 'hostname', 'port', 'user'
95         and 'idfile'
96     """
97     if subprocess.call('vagrant ssh-config >sshconfig',
98                        cwd='builder', shell=True) != 0:
99         raise BuildException("Error getting ssh config")
100     vagranthost = 'default'  # Host in ssh config file
101     sshconfig = paramiko.SSHConfig()
102     sshf = open('builder/sshconfig', 'r')
103     sshconfig.parse(sshf)
104     sshf.close()
105     sshconfig = sshconfig.lookup(vagranthost)
106     idfile = sshconfig['identityfile']
107     if isinstance(idfile, list):
108         idfile = idfile[0]
109     elif idfile.startswith('"') and idfile.endswith('"'):
110         idfile = idfile[1:-1]
111     return {'hostname': sshconfig['hostname'],
112             'port': int(sshconfig['port']),
113             'user': sshconfig['user'],
114             'idfile': idfile}
115
116
117 def get_clean_vm(reset=False):
118     """Get a clean VM ready to do a buildserver build.
119
120     This might involve creating and starting a new virtual machine from
121     scratch, or it might be as simple (unless overridden by the reset
122     parameter) as re-using a snapshot created previously.
123
124     A BuildException will be raised if anything goes wrong.
125
126     :reset: True to force creating from scratch.
127     :returns: A dictionary containing 'hostname', 'port', 'user'
128         and 'idfile'
129     """
130     # Reset existing builder machine to a clean state if possible.
131     vm_ok = False
132     if not reset:
133         logging.info("Checking for valid existing build server")
134
135         if got_valid_builder_vm():
136             logging.info("...VM is present")
137             p = FDroidPopen(['VBoxManage', 'snapshot',
138                              get_builder_vm_id(), 'list',
139                              '--details'], cwd='builder')
140             if 'fdroidclean' in p.output:
141                 logging.info("...snapshot exists - resetting build server to "
142                              "clean state")
143                 retcode, output = vagrant(['status'], cwd='builder')
144
145                 if 'running' in output:
146                     logging.info("...suspending")
147                     vagrant(['suspend'], cwd='builder')
148                     logging.info("...waiting a sec...")
149                     time.sleep(10)
150                 p = FDroidPopen(['VBoxManage', 'snapshot', get_builder_vm_id(),
151                                  'restore', 'fdroidclean'],
152                                 cwd='builder')
153
154                 if p.returncode == 0:
155                     logging.info("...reset to snapshot - server is valid")
156                     retcode, output = vagrant(['up'], cwd='builder')
157                     if retcode != 0:
158                         raise BuildException("Failed to start build server")
159                     logging.info("...waiting a sec...")
160                     time.sleep(10)
161                     sshinfo = get_vagrant_sshinfo()
162                     vm_ok = True
163                 else:
164                     logging.info("...failed to reset to snapshot")
165             else:
166                 logging.info("...snapshot doesn't exist - "
167                              "VBoxManage snapshot list:\n" + p.output)
168
169     # If we can't use the existing machine for any reason, make a
170     # new one from scratch.
171     if not vm_ok:
172         if os.path.exists('builder'):
173             logging.info("Removing broken/incomplete/unwanted build server")
174             vagrant(['destroy', '-f'], cwd='builder')
175             shutil.rmtree('builder')
176         os.mkdir('builder')
177
178         p = subprocess.Popen('vagrant --version', shell=True,
179                              stdout=subprocess.PIPE)
180         vver = p.communicate()[0]
181         if vver.startswith('Vagrant version 1.2'):
182             with open('builder/Vagrantfile', 'w') as vf:
183                 vf.write('Vagrant.configure("2") do |config|\n')
184                 vf.write('config.vm.box = "buildserver"\n')
185                 vf.write('end\n')
186         else:
187             with open('builder/Vagrantfile', 'w') as vf:
188                 vf.write('Vagrant::Config.run do |config|\n')
189                 vf.write('config.vm.box = "buildserver"\n')
190                 vf.write('end\n')
191
192         logging.info("Starting new build server")
193         retcode, _ = vagrant(['up'], cwd='builder')
194         if retcode != 0:
195             raise BuildException("Failed to start build server")
196
197         # Open SSH connection to make sure it's working and ready...
198         logging.info("Connecting to virtual machine...")
199         sshinfo = get_vagrant_sshinfo()
200         sshs = paramiko.SSHClient()
201         sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
202         sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
203                      port=sshinfo['port'], timeout=300,
204                      look_for_keys=False,
205                      key_filename=sshinfo['idfile'])
206         sshs.close()
207
208         logging.info("Saving clean state of new build server")
209         retcode, _ = vagrant(['suspend'], cwd='builder')
210         if retcode != 0:
211             raise BuildException("Failed to suspend build server")
212         logging.info("...waiting a sec...")
213         time.sleep(10)
214         p = FDroidPopen(['VBoxManage', 'snapshot', get_builder_vm_id(),
215                          'take', 'fdroidclean'],
216                         cwd='builder')
217         if p.returncode != 0:
218             raise BuildException("Failed to take snapshot")
219         logging.info("...waiting a sec...")
220         time.sleep(10)
221         logging.info("Restarting new build server")
222         retcode, _ = vagrant(['up'], cwd='builder')
223         if retcode != 0:
224             raise BuildException("Failed to start build server")
225         logging.info("...waiting a sec...")
226         time.sleep(10)
227         # Make sure it worked...
228         p = FDroidPopen(['VBoxManage', 'snapshot', get_builder_vm_id(),
229                          'list', '--details'],
230                         cwd='builder')
231         if 'fdroidclean' not in p.output:
232             raise BuildException("Failed to take snapshot.")
233
234     return sshinfo
235
236
237 def release_vm():
238     """Release the VM previously started with get_clean_vm().
239
240     This should always be called.
241     """
242     logging.info("Suspending build server")
243     subprocess.call(['vagrant', 'suspend'], cwd='builder')
244
245
246 # Note that 'force' here also implies test mode.
247 def build_server(app, thisbuild, vcs, build_dir, output_dir, force):
248     """Do a build on the build server."""
249
250     try:
251         paramiko
252     except NameError:
253         raise BuildException("Paramiko is required to use the buildserver")
254     if options.verbose:
255         logging.getLogger("paramiko").setLevel(logging.DEBUG)
256     else:
257         logging.getLogger("paramiko").setLevel(logging.WARN)
258
259     sshinfo = get_clean_vm()
260
261     try:
262
263         # Open SSH connection...
264         logging.info("Connecting to virtual machine...")
265         sshs = paramiko.SSHClient()
266         sshs.set_missing_host_key_policy(paramiko.AutoAddPolicy())
267         sshs.connect(sshinfo['hostname'], username=sshinfo['user'],
268                      port=sshinfo['port'], timeout=300,
269                      look_for_keys=False, key_filename=sshinfo['idfile'])
270
271         homedir = '/home/' + sshinfo['user']
272
273         # Get an SFTP connection...
274         ftp = sshs.open_sftp()
275         ftp.get_channel().settimeout(15)
276
277         # Put all the necessary files in place...
278         ftp.chdir(homedir)
279
280         # Helper to copy the contents of a directory to the server...
281         def send_dir(path):
282             root = os.path.dirname(path)
283             main = os.path.basename(path)
284             ftp.mkdir(main)
285             for r, d, f in os.walk(path):
286                 rr = os.path.relpath(r, root)
287                 ftp.chdir(rr)
288                 for dd in d:
289                     ftp.mkdir(dd)
290                 for ff in f:
291                     lfile = os.path.join(root, rr, ff)
292                     if not os.path.islink(lfile):
293                         ftp.put(lfile, ff)
294                         ftp.chmod(ff, os.stat(lfile).st_mode)
295                 for i in range(len(rr.split('/'))):
296                     ftp.chdir('..')
297             ftp.chdir('..')
298
299         logging.info("Preparing server for build...")
300         serverpath = os.path.abspath(os.path.dirname(__file__))
301         ftp.put(os.path.join(serverpath, 'build.py'), 'build.py')
302         ftp.put(os.path.join(serverpath, 'common.py'), 'common.py')
303         ftp.put(os.path.join(serverpath, 'metadata.py'), 'metadata.py')
304         ftp.put(os.path.join(serverpath, '..', 'buildserver',
305                              'config.buildserver.py'), 'config.py')
306         ftp.chmod('config.py', 0o600)
307
308         # Copy over the ID (head commit hash) of the fdroidserver in use...
309         subprocess.call('git rev-parse HEAD >' +
310                         os.path.join(os.getcwd(), 'tmp', 'fdroidserverid'),
311                         shell=True, cwd=serverpath)
312         ftp.put('tmp/fdroidserverid', 'fdroidserverid')
313
314         # Copy the metadata - just the file for this app...
315         ftp.mkdir('metadata')
316         ftp.mkdir('srclibs')
317         ftp.chdir('metadata')
318         ftp.put(os.path.join('metadata', app['id'] + '.txt'),
319                 app['id'] + '.txt')
320         # And patches if there are any...
321         if os.path.exists(os.path.join('metadata', app['id'])):
322             send_dir(os.path.join('metadata', app['id']))
323
324         ftp.chdir(homedir)
325         # Create the build directory...
326         ftp.mkdir('build')
327         ftp.chdir('build')
328         ftp.mkdir('extlib')
329         ftp.mkdir('srclib')
330         # Copy any extlibs that are required...
331         if thisbuild['extlibs']:
332             ftp.chdir(homedir + '/build/extlib')
333             for lib in thisbuild['extlibs']:
334                 lib = lib.strip()
335                 libsrc = os.path.join('build/extlib', lib)
336                 if not os.path.exists(libsrc):
337                     raise BuildException("Missing extlib {0}".format(libsrc))
338                 lp = lib.split('/')
339                 for d in lp[:-1]:
340                     if d not in ftp.listdir():
341                         ftp.mkdir(d)
342                     ftp.chdir(d)
343                 ftp.put(libsrc, lp[-1])
344                 for _ in lp[:-1]:
345                     ftp.chdir('..')
346         # Copy any srclibs that are required...
347         srclibpaths = []
348         if thisbuild['srclibs']:
349             for lib in thisbuild['srclibs']:
350                 srclibpaths.append(
351                     common.getsrclib(lib, 'build/srclib', srclibpaths,
352                                      basepath=True, prepare=False))
353
354         # If one was used for the main source, add that too.
355         basesrclib = vcs.getsrclib()
356         if basesrclib:
357             srclibpaths.append(basesrclib)
358         for name, number, lib in srclibpaths:
359             logging.info("Sending srclib '%s'" % lib)
360             ftp.chdir(homedir + '/build/srclib')
361             if not os.path.exists(lib):
362                 raise BuildException("Missing srclib directory '" + lib + "'")
363             fv = '.fdroidvcs-' + name
364             ftp.put(os.path.join('build/srclib', fv), fv)
365             send_dir(lib)
366             # Copy the metadata file too...
367             ftp.chdir(homedir + '/srclibs')
368             ftp.put(os.path.join('srclibs', name + '.txt'),
369                     name + '.txt')
370         # Copy the main app source code
371         # (no need if it's a srclib)
372         if (not basesrclib) and os.path.exists(build_dir):
373             ftp.chdir(homedir + '/build')
374             fv = '.fdroidvcs-' + app['id']
375             ftp.put(os.path.join('build', fv), fv)
376             send_dir(build_dir)
377
378         # Execute the build script...
379         logging.info("Starting build...")
380         chan = sshs.get_transport().open_session()
381         chan.get_pty()
382         cmdline = 'python build.py --on-server'
383         if force:
384             cmdline += ' --force --test'
385         if options.verbose:
386             cmdline += ' --verbose'
387         cmdline += " %s:%s" % (app['id'], thisbuild['vercode'])
388         chan.exec_command('bash -c ". ~/.bsenv && ' + cmdline + '"')
389         output = ''
390         while not chan.exit_status_ready():
391             while chan.recv_ready():
392                 output += chan.recv(1024)
393             time.sleep(0.1)
394         logging.info("...getting exit status")
395         returncode = chan.recv_exit_status()
396         while True:
397             get = chan.recv(1024)
398             if len(get) == 0:
399                 break
400             output += get
401         if returncode != 0:
402             raise BuildException(
403                 "Build.py failed on server for {0}:{1}".format(
404                     app['id'], thisbuild['version']), output)
405
406         # Retrieve the built files...
407         logging.info("Retrieving build output...")
408         if force:
409             ftp.chdir(homedir + '/tmp')
410         else:
411             ftp.chdir(homedir + '/unsigned')
412         apkfile = common.getapkname(app, thisbuild)
413         tarball = common.getsrcname(app, thisbuild)
414         try:
415             ftp.get(apkfile, os.path.join(output_dir, apkfile))
416             if not options.notarball:
417                 ftp.get(tarball, os.path.join(output_dir, tarball))
418         except:
419             raise BuildException(
420                 "Build failed for %s:%s - missing output files".format(
421                     app['id'], thisbuild['version']), output)
422         ftp.close()
423
424     finally:
425
426         # Suspend the build server.
427         release_vm()
428
429
430 def adapt_gradle(build_dir):
431     for root, dirs, files in os.walk(build_dir):
432         if 'build.gradle' in files:
433             path = os.path.join(root, 'build.gradle')
434             logging.debug("Adapting build.gradle at %s" % path)
435
436             FDroidPopen(['sed', '-i',
437                          r's@buildToolsVersion\([ =]*\)["\'][0-9\.]*["\']@buildToolsVersion\1"'
438                          + config['build_tools'] + '"@g', path])
439
440
441 def capitalize_intact(string):
442     """Like str.capitalize(), but leave the rest of the string intact without
443     switching it to lowercase."""
444     if len(string) == 0:
445         return string
446     if len(string) == 1:
447         return string.upper()
448     return string[0].upper() + string[1:]
449
450
451 def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver):
452     """Do a build locally."""
453
454     if thisbuild['buildjni'] and thisbuild['buildjni'] != ['no']:
455         if not thisbuild['ndk_path']:
456             logging.critical("Android NDK version '%s' could not be found!" % thisbuild['ndk'])
457             logging.critical("Configured versions:")
458             for k, v in config['ndk_paths'].iteritems():
459                 if k.endswith("_orig"):
460                     continue
461                 logging.critical("  %s: %s" % (k, v))
462             sys.exit(3)
463         elif not os.path.isdir(thisbuild['ndk_path']):
464             logging.critical("Android NDK '%s' is not a directory!" % thisbuild['ndk_path'])
465             sys.exit(3)
466
467     # Set up environment vars that depend on each build
468     for n in ['ANDROID_NDK', 'NDK']:
469         common.env[n] = thisbuild['ndk_path']
470
471     # Prepare the source code...
472     root_dir, srclibpaths = common.prepare_source(vcs, app, thisbuild,
473                                                   build_dir, srclib_dir,
474                                                   extlib_dir, onserver)
475
476     # We need to clean via the build tool in case the binary dirs are
477     # different from the default ones
478     p = None
479     gradletasks = []
480     if thisbuild['type'] == 'maven':
481         logging.info("Cleaning Maven project...")
482         cmd = [config['mvn3'], 'clean', '-Dandroid.sdk.path=' + config['sdk_path']]
483
484         if '@' in thisbuild['maven']:
485             maven_dir = os.path.join(root_dir, thisbuild['maven'].split('@', 1)[1])
486             maven_dir = os.path.normpath(maven_dir)
487         else:
488             maven_dir = root_dir
489
490         p = FDroidPopen(cmd, cwd=maven_dir)
491
492     elif thisbuild['type'] == 'gradle':
493
494         logging.info("Cleaning Gradle project...")
495
496         if thisbuild['preassemble']:
497             gradletasks += thisbuild['preassemble']
498
499         flavours = thisbuild['gradle']
500         if flavours == ['yes']:
501             flavours = []
502
503         flavours_cmd = ''.join([capitalize_intact(f) for f in flavours])
504
505         gradletasks += ['assemble' + flavours_cmd + 'Release']
506
507         adapt_gradle(build_dir)
508         for name, number, libpath in srclibpaths:
509             adapt_gradle(libpath)
510
511         cmd = [config['gradle']]
512         cmd += ['clean' + capitalize_intact(task) for task in gradletasks]
513
514         p = FDroidPopen(cmd, cwd=root_dir)
515
516     elif thisbuild['type'] == 'kivy':
517         pass
518
519     elif thisbuild['type'] == 'ant':
520         logging.info("Cleaning Ant project...")
521         p = FDroidPopen(['ant', 'clean'], cwd=root_dir)
522
523     if p is not None and p.returncode != 0:
524         raise BuildException("Error cleaning %s:%s" %
525                              (app['id'], thisbuild['version']), p.output)
526
527     for root, dirs, files in os.walk(build_dir):
528         # Don't remove possibly necessary 'gradle' dirs if 'gradlew' is not there
529         if 'gradlew' in files:
530             logging.debug("Getting rid of Gradle wrapper stuff in %s" % root)
531             os.remove(os.path.join(root, 'gradlew'))
532             if 'gradlew.bat' in files:
533                 os.remove(os.path.join(root, 'gradlew.bat'))
534             if 'gradle' in dirs:
535                 shutil.rmtree(os.path.join(root, 'gradle'))
536
537     if not options.skipscan:
538         # Scan before building...
539         logging.info("Scanning source for common problems...")
540         count = common.scan_source(build_dir, root_dir, thisbuild)
541         if count > 0:
542             if force:
543                 logging.warn('Scanner found %d problems:' % count)
544             else:
545                 raise BuildException("Can't build due to %d errors while scanning" % count)
546
547     if not options.notarball:
548         # Build the source tarball right before we build the release...
549         logging.info("Creating source tarball...")
550         tarname = common.getsrcname(app, thisbuild)
551         tarball = tarfile.open(os.path.join(tmp_dir, tarname), "w:gz")
552
553         def tarexc(f):
554             return any(f.endswith(s) for s in ['.svn', '.git', '.hg', '.bzr'])
555         tarball.add(build_dir, tarname, exclude=tarexc)
556         tarball.close()
557
558     # Run a build command if one is required...
559     if thisbuild['build']:
560         logging.info("Running 'build' commands in %s" % root_dir)
561         cmd = common.replace_config_vars(thisbuild['build'], thisbuild)
562
563         # Substitute source library paths into commands...
564         for name, number, libpath in srclibpaths:
565             libpath = os.path.relpath(libpath, root_dir)
566             cmd = cmd.replace('$$' + name + '$$', libpath)
567
568         p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
569
570         if p.returncode != 0:
571             raise BuildException("Error running build command for %s:%s" %
572                                  (app['id'], thisbuild['version']), p.output)
573
574     # Build native stuff if required...
575     if thisbuild['buildjni'] and thisbuild['buildjni'] != ['no']:
576         logging.info("Building the native code")
577         jni_components = thisbuild['buildjni']
578
579         if jni_components == ['yes']:
580             jni_components = ['']
581         cmd = [os.path.join(thisbuild['ndk_path'], "ndk-build"), "-j1"]
582         for d in jni_components:
583             if d:
584                 logging.info("Building native code in '%s'" % d)
585             else:
586                 logging.info("Building native code in the main project")
587             manifest = root_dir + '/' + d + '/AndroidManifest.xml'
588             if os.path.exists(manifest):
589                 # Read and write the whole AM.xml to fix newlines and avoid
590                 # the ndk r8c or later 'wordlist' errors. The outcome of this
591                 # under gnu/linux is the same as when using tools like
592                 # dos2unix, but the native python way is faster and will
593                 # work in non-unix systems.
594                 manifest_text = open(manifest, 'U').read()
595                 open(manifest, 'w').write(manifest_text)
596                 # In case the AM.xml read was big, free the memory
597                 del manifest_text
598             p = FDroidPopen(cmd, cwd=os.path.join(root_dir, d))
599             if p.returncode != 0:
600                 raise BuildException("NDK build failed for %s:%s" % (app['id'], thisbuild['version']), p.output)
601
602     p = None
603     # Build the release...
604     if thisbuild['type'] == 'maven':
605         logging.info("Building Maven project...")
606
607         if '@' in thisbuild['maven']:
608             maven_dir = os.path.join(root_dir, thisbuild['maven'].split('@', 1)[1])
609         else:
610             maven_dir = root_dir
611
612         mvncmd = [config['mvn3'], '-Dandroid.sdk.path=' + config['sdk_path'],
613                   '-Dmaven.jar.sign.skip=true', '-Dmaven.test.skip=true',
614                   '-Dandroid.sign.debug=false', '-Dandroid.release=true',
615                   'package']
616         if thisbuild['target']:
617             target = thisbuild["target"].split('-')[1]
618             FDroidPopen(['sed', '-i',
619                          's@<platform>[0-9]*</platform>@<platform>'
620                          + target + '</platform>@g',
621                          'pom.xml'],
622                         cwd=root_dir)
623             if '@' in thisbuild['maven']:
624                 FDroidPopen(['sed', '-i',
625                              's@<platform>[0-9]*</platform>@<platform>'
626                              + target + '</platform>@g',
627                              'pom.xml'],
628                             cwd=maven_dir)
629
630         p = FDroidPopen(mvncmd, cwd=maven_dir)
631
632         bindir = os.path.join(root_dir, 'target')
633
634     elif thisbuild['type'] == 'kivy':
635         logging.info("Building Kivy project...")
636
637         spec = os.path.join(root_dir, 'buildozer.spec')
638         if not os.path.exists(spec):
639             raise BuildException("Expected to find buildozer-compatible spec at {0}"
640                                  .format(spec))
641
642         defaults = {'orientation': 'landscape', 'icon': '',
643                     'permissions': '', 'android.api': "18"}
644         bconfig = ConfigParser(defaults, allow_no_value=True)
645         bconfig.read(spec)
646
647         distdir = 'python-for-android/dist/fdroid'
648         if os.path.exists(distdir):
649             shutil.rmtree(distdir)
650
651         modules = bconfig.get('app', 'requirements').split(',')
652
653         cmd = 'ANDROIDSDK=' + config['sdk_path']
654         cmd += ' ANDROIDNDK=' + thisbuild['ndk_path']
655         cmd += ' ANDROIDNDKVER=' + thisbuild['ndk']
656         cmd += ' ANDROIDAPI=' + str(bconfig.get('app', 'android.api'))
657         cmd += ' VIRTUALENV=virtualenv'
658         cmd += ' ./distribute.sh'
659         cmd += ' -m ' + "'" + ' '.join(modules) + "'"
660         cmd += ' -d fdroid'
661         p = FDroidPopen(cmd, cwd='python-for-android', shell=True)
662         if p.returncode != 0:
663             raise BuildException("Distribute build failed")
664
665         cid = bconfig.get('app', 'package.domain') + '.' + bconfig.get('app', 'package.name')
666         if cid != app['id']:
667             raise BuildException("Package ID mismatch between metadata and spec")
668
669         orientation = bconfig.get('app', 'orientation', 'landscape')
670         if orientation == 'all':
671             orientation = 'sensor'
672
673         cmd = ['./build.py'
674                '--dir', root_dir,
675                '--name', bconfig.get('app', 'title'),
676                '--package', app['id'],
677                '--version', bconfig.get('app', 'version'),
678                '--orientation', orientation
679                ]
680
681         perms = bconfig.get('app', 'permissions')
682         for perm in perms.split(','):
683             cmd.extend(['--permission', perm])
684
685         if config.get('app', 'fullscreen') == 0:
686             cmd.append('--window')
687
688         icon = bconfig.get('app', 'icon.filename')
689         if icon:
690             cmd.extend(['--icon', os.path.join(root_dir, icon)])
691
692         cmd.append('release')
693         p = FDroidPopen(cmd, cwd=distdir)
694
695     elif thisbuild['type'] == 'gradle':
696         logging.info("Building Gradle project...")
697
698         # Avoid having to use lintOptions.abortOnError false
699         if thisbuild['gradlepluginver'] >= LooseVersion('0.7'):
700             with open(os.path.join(root_dir, 'build.gradle'), "a") as f:
701                 f.write("\nandroid { lintOptions { checkReleaseBuilds false } }\n")
702
703         commands = [config['gradle']] + gradletasks
704
705         p = FDroidPopen(commands, cwd=root_dir)
706
707     elif thisbuild['type'] == 'ant':
708         logging.info("Building Ant project...")
709         cmd = ['ant']
710         if thisbuild['antcommands']:
711             cmd += thisbuild['antcommands']
712         else:
713             cmd += ['release']
714         p = FDroidPopen(cmd, cwd=root_dir)
715
716         bindir = os.path.join(root_dir, 'bin')
717
718     if p is not None and p.returncode != 0:
719         raise BuildException("Build failed for %s:%s" % (app['id'], thisbuild['version']), p.output)
720     logging.info("Successfully built version " + thisbuild['version'] + ' of ' + app['id'])
721
722     if thisbuild['type'] == 'maven':
723         stdout_apk = '\n'.join([
724             line for line in p.output.splitlines() if any(
725                 a in line for a in ('.apk', '.ap_', '.jar'))])
726         m = re.match(r".*^\[INFO\] .*apkbuilder.*/([^/]*)\.apk",
727                      stdout_apk, re.S | re.M)
728         if not m:
729             m = re.match(r".*^\[INFO\] Creating additional unsigned apk file .*/([^/]+)\.apk[^l]",
730                          stdout_apk, re.S | re.M)
731         if not m:
732             m = re.match(r'.*^\[INFO\] [^$]*aapt \[package,[^$]*' + bindir + r'/([^/]+)\.ap[_k][,\]]',
733                          stdout_apk, re.S | re.M)
734
735         if not m:
736             m = re.match(r".*^\[INFO\] Building jar: .*/" + bindir + r"/(.+)\.jar",
737                          stdout_apk, re.S | re.M)
738         if not m:
739             raise BuildException('Failed to find output')
740         src = m.group(1)
741         src = os.path.join(bindir, src) + '.apk'
742     elif thisbuild['type'] == 'kivy':
743         src = 'python-for-android/dist/default/bin/{0}-{1}-release.apk'.format(
744             bconfig.get('app', 'title'), bconfig.get('app', 'version'))
745     elif thisbuild['type'] == 'gradle':
746
747         if thisbuild['gradlepluginver'] >= LooseVersion('0.11'):
748             apks_dir = os.path.join(root_dir, 'build', 'outputs', 'apk')
749         else:
750             apks_dir = os.path.join(root_dir, 'build', 'apk')
751
752         apks = glob.glob(os.path.join(apks_dir, '*-release-unsigned.apk'))
753         if len(apks) > 1:
754             raise BuildException('More than one resulting apks found in %s' % apks_dir,
755                                  '\n'.join(apks))
756         if len(apks) < 1:
757             raise BuildException('Failed to find gradle output in %s' % apks_dir)
758         src = apks[0]
759     elif thisbuild['type'] == 'ant':
760         stdout_apk = '\n'.join([
761             line for line in p.output.splitlines() if '.apk' in line])
762         src = re.match(r".*^.*Creating (.+) for release.*$.*", stdout_apk,
763                        re.S | re.M).group(1)
764         src = os.path.join(bindir, src)
765     elif thisbuild['type'] == 'raw':
766         src = os.path.join(root_dir, thisbuild['output'])
767         src = os.path.normpath(src)
768
769     # Make sure it's not debuggable...
770     if common.isApkDebuggable(src, config):
771         raise BuildException("APK is debuggable")
772
773     # By way of a sanity check, make sure the version and version
774     # code in our new apk match what we expect...
775     logging.debug("Checking " + src)
776     if not os.path.exists(src):
777         raise BuildException("Unsigned apk is not at expected location of " + src)
778
779     p = SdkToolsPopen(['aapt', 'dump', 'badging', src], output=False)
780
781     vercode = None
782     version = None
783     foundid = None
784     nativecode = None
785     for line in p.output.splitlines():
786         if line.startswith("package:"):
787             pat = re.compile(".*name='([a-zA-Z0-9._]*)'.*")
788             m = pat.match(line)
789             if m:
790                 foundid = m.group(1)
791             pat = re.compile(".*versionCode='([0-9]*)'.*")
792             m = pat.match(line)
793             if m:
794                 vercode = m.group(1)
795             pat = re.compile(".*versionName='([^']*)'.*")
796             m = pat.match(line)
797             if m:
798                 version = m.group(1)
799         elif line.startswith("native-code:"):
800             nativecode = line[12:]
801
802     # Ignore empty strings or any kind of space/newline chars that we don't
803     # care about
804     if nativecode is not None:
805         nativecode = nativecode.strip()
806         nativecode = None if not nativecode else nativecode
807
808     if thisbuild['buildjni'] and thisbuild['buildjni'] != ['no']:
809         if nativecode is None:
810             raise BuildException("Native code should have been built but none was packaged")
811     if thisbuild['novcheck']:
812         vercode = thisbuild['vercode']
813         version = thisbuild['version']
814     if not version or not vercode:
815         raise BuildException("Could not find version information in build in output")
816     if not foundid:
817         raise BuildException("Could not find package ID in output")
818     if foundid != app['id']:
819         raise BuildException("Wrong package ID - build " + foundid + " but expected " + app['id'])
820
821     # Some apps (e.g. Timeriffic) have had the bonkers idea of
822     # including the entire changelog in the version number. Remove
823     # it so we can compare. (TODO: might be better to remove it
824     # before we compile, in fact)
825     index = version.find(" //")
826     if index != -1:
827         version = version[:index]
828
829     if (version != thisbuild['version'] or
830             vercode != thisbuild['vercode']):
831         raise BuildException(("Unexpected version/version code in output;"
832                               " APK: '%s' / '%s', "
833                               " Expected: '%s' / '%s'")
834                              % (version, str(vercode), thisbuild['version'],
835                                 str(thisbuild['vercode']))
836                              )
837
838     # Add information for 'fdroid verify' to be able to reproduce the build
839     # environment.
840     if onserver:
841         metadir = os.path.join(tmp_dir, 'META-INF')
842         if not os.path.exists(metadir):
843             os.mkdir(metadir)
844         homedir = os.path.expanduser('~')
845         for fn in ['buildserverid', 'fdroidserverid']:
846             shutil.copyfile(os.path.join(homedir, fn),
847                             os.path.join(metadir, fn))
848             subprocess.call(['jar', 'uf', os.path.abspath(src),
849                              'META-INF/' + fn], cwd=tmp_dir)
850
851     # Copy the unsigned apk to our destination directory for further
852     # processing (by publish.py)...
853     dest = os.path.join(output_dir, common.getapkname(app, thisbuild))
854     shutil.copyfile(src, dest)
855
856     # Move the source tarball into the output directory...
857     if output_dir != tmp_dir and not options.notarball:
858         shutil.move(os.path.join(tmp_dir, tarname),
859                     os.path.join(output_dir, tarname))
860
861
862 def trybuild(app, thisbuild, build_dir, output_dir, also_check_dir, srclib_dir, extlib_dir,
863              tmp_dir, repo_dir, vcs, test, server, force, onserver):
864     """
865     Build a particular version of an application, if it needs building.
866
867     :param output_dir: The directory where the build output will go. Usually
868        this is the 'unsigned' directory.
869     :param repo_dir: The repo directory - used for checking if the build is
870        necessary.
871     :paaram also_check_dir: An additional location for checking if the build
872        is necessary (usually the archive repo)
873     :param test: True if building in test mode, in which case the build will
874        always happen, even if the output already exists. In test mode, the
875        output directory should be a temporary location, not any of the real
876        ones.
877
878     :returns: True if the build was done, False if it wasn't necessary.
879     """
880
881     dest_apk = common.getapkname(app, thisbuild)
882
883     dest = os.path.join(output_dir, dest_apk)
884     dest_repo = os.path.join(repo_dir, dest_apk)
885
886     if not test:
887         if os.path.exists(dest) or os.path.exists(dest_repo):
888             return False
889
890         if also_check_dir:
891             dest_also = os.path.join(also_check_dir, dest_apk)
892             if os.path.exists(dest_also):
893                 return False
894
895     if thisbuild['disable'] and not options.force:
896         return False
897
898     logging.info("Building version %s (%s) of %s" % (
899         thisbuild['version'], thisbuild['vercode'], app['id']))
900
901     if server:
902         # When using server mode, still keep a local cache of the repo, by
903         # grabbing the source now.
904         vcs.gotorevision(thisbuild['commit'])
905
906         build_server(app, thisbuild, vcs, build_dir, output_dir, force)
907     else:
908         build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, force, onserver)
909     return True
910
911
912 def parse_commandline():
913     """Parse the command line. Returns options, args."""
914
915     parser = OptionParser(usage="Usage: %prog [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
916     parser.add_option("-v", "--verbose", action="store_true", default=False,
917                       help="Spew out even more information than normal")
918     parser.add_option("-q", "--quiet", action="store_true", default=False,
919                       help="Restrict output to warnings and errors")
920     parser.add_option("-l", "--latest", action="store_true", default=False,
921                       help="Build only the latest version of each package")
922     parser.add_option("-s", "--stop", action="store_true", default=False,
923                       help="Make the build stop on exceptions")
924     parser.add_option("-t", "--test", action="store_true", default=False,
925                       help="Test mode - put output in the tmp directory only, and always build, even if the output already exists.")
926     parser.add_option("--server", action="store_true", default=False,
927                       help="Use build server")
928     parser.add_option("--resetserver", action="store_true", default=False,
929                       help="Reset and create a brand new build server, even if the existing one appears to be ok.")
930     parser.add_option("--on-server", dest="onserver", action="store_true", default=False,
931                       help="Specify that we're running on the build server")
932     parser.add_option("--skip-scan", dest="skipscan", action="store_true", default=False,
933                       help="Skip scanning the source code for binaries and other problems")
934     parser.add_option("--no-tarball", dest="notarball", action="store_true", default=False,
935                       help="Don't create a source tarball, useful when testing a build")
936     parser.add_option("-f", "--force", action="store_true", default=False,
937                       help="Force build of disabled apps, and carries on regardless of scan problems. Only allowed in test mode.")
938     parser.add_option("-a", "--all", action="store_true", default=False,
939                       help="Build all applications available")
940     parser.add_option("-w", "--wiki", default=False, action="store_true",
941                       help="Update the wiki")
942     options, args = parser.parse_args()
943
944     # Force --stop with --on-server to get correct exit code
945     if options.onserver:
946         options.stop = True
947
948     if options.force and not options.test:
949         raise OptionError("Force is only allowed in test mode", "force")
950
951     return options, args
952
953 options = None
954 config = None
955
956
957 def main():
958
959     global options, config
960
961     options, args = parse_commandline()
962     if not args and not options.all:
963         raise OptionError("If you really want to build all the apps, use --all", "all")
964
965     config = common.read_config(options)
966
967     if config['build_server_always']:
968         options.server = True
969     if options.resetserver and not options.server:
970         raise OptionError("Using --resetserver without --server makes no sense", "resetserver")
971
972     log_dir = 'logs'
973     if not os.path.isdir(log_dir):
974         logging.info("Creating log directory")
975         os.makedirs(log_dir)
976
977     tmp_dir = 'tmp'
978     if not os.path.isdir(tmp_dir):
979         logging.info("Creating temporary directory")
980         os.makedirs(tmp_dir)
981
982     if options.test:
983         output_dir = tmp_dir
984     else:
985         output_dir = 'unsigned'
986         if not os.path.isdir(output_dir):
987             logging.info("Creating output directory")
988             os.makedirs(output_dir)
989
990     if config['archive_older'] != 0:
991         also_check_dir = 'archive'
992     else:
993         also_check_dir = None
994
995     repo_dir = 'repo'
996
997     build_dir = 'build'
998     if not os.path.isdir(build_dir):
999         logging.info("Creating build directory")
1000         os.makedirs(build_dir)
1001     srclib_dir = os.path.join(build_dir, 'srclib')
1002     extlib_dir = os.path.join(build_dir, 'extlib')
1003
1004     # Read all app and srclib metadata
1005     allapps = metadata.read_metadata(xref=not options.onserver)
1006
1007     apps = common.read_app_args(args, allapps, True)
1008     for appid, app in apps.items():
1009         if (app['Disabled'] and not options.force) or not app['Repo Type'] or not app['builds']:
1010             del apps[appid]
1011
1012     if not apps:
1013         raise FDroidException("No apps to process.")
1014
1015     if options.latest:
1016         for app in apps.itervalues():
1017             for build in reversed(app['builds']):
1018                 if build['disable'] and not options.force:
1019                     continue
1020                 app['builds'] = [build]
1021                 break
1022
1023     if options.wiki:
1024         import mwclient
1025         site = mwclient.Site((config['wiki_protocol'], config['wiki_server']),
1026                              path=config['wiki_path'])
1027         site.login(config['wiki_user'], config['wiki_password'])
1028
1029     # Build applications...
1030     failed_apps = {}
1031     build_succeeded = []
1032     for appid, app in apps.iteritems():
1033
1034         first = True
1035
1036         for thisbuild in app['builds']:
1037             wikilog = None
1038             try:
1039
1040                 # For the first build of a particular app, we need to set up
1041                 # the source repo. We can reuse it on subsequent builds, if
1042                 # there are any.
1043                 if first:
1044                     if app['Repo Type'] == 'srclib':
1045                         build_dir = os.path.join('build', 'srclib', app['Repo'])
1046                     else:
1047                         build_dir = os.path.join('build', appid)
1048
1049                     # Set up vcs interface and make sure we have the latest code...
1050                     logging.debug("Getting {0} vcs interface for {1}"
1051                                   .format(app['Repo Type'], app['Repo']))
1052                     vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
1053
1054                     first = False
1055
1056                 logging.debug("Checking " + thisbuild['version'])
1057                 if trybuild(app, thisbuild, build_dir, output_dir,
1058                             also_check_dir, srclib_dir, extlib_dir,
1059                             tmp_dir, repo_dir, vcs, options.test,
1060                             options.server, options.force,
1061                             options.onserver):
1062                     build_succeeded.append(app)
1063                     wikilog = "Build succeeded"
1064             except BuildException as be:
1065                 logfile = open(os.path.join(log_dir, appid + '.log'), 'a+')
1066                 logfile.write(str(be))
1067                 logfile.close()
1068                 print("Could not build app %s due to BuildException: %s" % (appid, be))
1069                 if options.stop:
1070                     sys.exit(1)
1071                 failed_apps[appid] = be
1072                 wikilog = be.get_wikitext()
1073             except VCSException as vcse:
1074                 reason = str(vcse).split('\n', 1)[0] if options.verbose else str(vcse)
1075                 logging.error("VCS error while building app %s: %s" % (
1076                     appid, reason))
1077                 if options.stop:
1078                     sys.exit(1)
1079                 failed_apps[appid] = vcse
1080                 wikilog = str(vcse)
1081             except Exception as e:
1082                 logging.error("Could not build app %s due to unknown error: %s" % (
1083                     appid, traceback.format_exc()))
1084                 if options.stop:
1085                     sys.exit(1)
1086                 failed_apps[appid] = e
1087                 wikilog = str(e)
1088
1089             if options.wiki and wikilog:
1090                 try:
1091                     # Write a page with the last build log for this version code
1092                     lastbuildpage = appid + '/lastbuild_' + thisbuild['vercode']
1093                     newpage = site.Pages[lastbuildpage]
1094                     txt = "Build completed at " + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + "\n\n" + wikilog
1095                     newpage.save(txt, summary='Build log')
1096                     # Redirect from /lastbuild to the most recent build log
1097                     newpage = site.Pages[appid + '/lastbuild']
1098                     newpage.save('#REDIRECT [[' + lastbuildpage + ']]', summary='Update redirect')
1099                 except:
1100                     logging.error("Error while attempting to publish build log")
1101
1102     for app in build_succeeded:
1103         logging.info("success: %s" % (app['id']))
1104
1105     if not options.verbose:
1106         for fa in failed_apps:
1107             logging.info("Build for app %s failed:\n%s" % (fa, failed_apps[fa]))
1108
1109     logging.info("Finished.")
1110     if len(build_succeeded) > 0:
1111         logging.info(str(len(build_succeeded)) + ' builds succeeded')
1112     if len(failed_apps) > 0:
1113         logging.info(str(len(failed_apps)) + ' builds failed')
1114
1115     sys.exit(0)
1116
1117 if __name__ == "__main__":
1118     main()