chiark / gitweb /
Apply some autopep8-python2 suggestions
[fdroidserver.git] / fdroidserver / build.py
index 9139a361346ec0ceb92af662cea4ee51f685a754..677e4c88fa3caf1d523b576ebf992807344d061c 100644 (file)
@@ -21,6 +21,7 @@
 import sys
 import os
 import shutil
+import glob
 import subprocess
 import re
 import tarfile
@@ -34,7 +35,7 @@ import logging
 
 import common
 import metadata
-from common import BuildException, VCSException, FDroidPopen, SilentPopen
+from common import FDroidException, BuildException, VCSException, FDroidPopen, SdkToolsPopen
 
 try:
     import paramiko
@@ -47,7 +48,7 @@ def get_builder_vm_id():
     if os.path.isdir(vd):
         # Vagrant 1.2 (and maybe 1.1?) it's a directory tree...
         with open(os.path.join(vd, 'machines', 'default',
-                  'virtualbox', 'id')) as vf:
+                               'virtualbox', 'id')) as vf:
             id = vf.read()
         return id
     else:
@@ -70,7 +71,7 @@ def got_valid_builder_vm():
         return True
     # Vagrant 1.2 - the directory can exist, but the id can be missing...
     if not os.path.exists(os.path.join(vd, 'machines', 'default',
-                          'virtualbox', 'id')):
+                                       'virtualbox', 'id')):
         return False
     return True
 
@@ -301,7 +302,7 @@ def build_server(app, thisbuild, vcs, build_dir, output_dir, force):
         ftp.put(os.path.join(serverpath, 'common.py'), 'common.py')
         ftp.put(os.path.join(serverpath, 'metadata.py'), 'metadata.py')
         ftp.put(os.path.join(serverpath, '..', 'buildserver',
-                'config.buildserver.py'), 'config.py')
+                             'config.buildserver.py'), 'config.py')
         ftp.chmod('config.py', 0o600)
 
         # Copy over the ID (head commit hash) of the fdroidserver in use...
@@ -521,25 +522,6 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
         tarball.add(build_dir, tarname, exclude=tarexc)
         tarball.close()
 
-    if onserver:
-        manifest = os.path.join(root_dir, 'AndroidManifest.xml')
-        if os.path.exists(manifest):
-            homedir = os.path.expanduser('~')
-            with open(os.path.join(homedir, 'buildserverid'), 'r') as f:
-                buildserverid = f.read()
-            with open(os.path.join(homedir, 'fdroidserverid'), 'r') as f:
-                fdroidserverid = f.read()
-            with open(manifest, 'r') as f:
-                manifestcontent = f.read()
-            manifestcontent = manifestcontent.replace('</manifest>',
-                                                      '<fdroid buildserverid="'
-                                                      + buildserverid + '"'
-                                                      + ' fdroidserverid="'
-                                                      + fdroidserverid + '"'
-                                                      + '/></manifest>')
-            with open(manifest, 'w') as f:
-                f.write(manifestcontent)
-
     # Run a build command if one is required...
     if thisbuild['build']:
         logging.info("Running 'build' commands in %s" % root_dir)
@@ -679,14 +661,13 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
 
     elif thisbuild['type'] == 'gradle':
         logging.info("Building Gradle project...")
-        flavours = thisbuild['gradle'].split(',')
-
-        if len(flavours) == 1 and flavours[0] in ['main', 'yes', '']:
-            flavours[0] = ''
+        flavours = thisbuild['gradle']
+        if flavours == ['yes']:
+            flavours = []
 
         commands = [config['gradle']]
         if thisbuild['preassemble']:
-            commands += thisbuild['preassemble'].split()
+            commands += thisbuild['preassemble']
 
         flavours_cmd = ''.join(flavours)
         if flavours_cmd:
@@ -695,16 +676,17 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
         commands += ['assemble' + flavours_cmd + 'Release']
 
         # Avoid having to use lintOptions.abortOnError false
-        if thisbuild['gradlepluginver'] >= LooseVersion('0.8'):
-            commands += ['-x', 'lintVital' + flavours_cmd + 'Release']
+        if thisbuild['gradlepluginver'] >= LooseVersion('0.7'):
+            with open(os.path.join(root_dir, 'build.gradle'), "a") as f:
+                f.write("\nandroid { lintOptions { checkReleaseBuilds false } }\n")
 
         p = FDroidPopen(commands, cwd=root_dir)
 
     elif thisbuild['type'] == 'ant':
         logging.info("Building Ant project...")
         cmd = ['ant']
-        if thisbuild['antcommand']:
-            cmd += [thisbuild['antcommand']]
+        if thisbuild['antcommands']:
+            cmd += thisbuild['antcommands']
         else:
             cmd += ['release']
         p = FDroidPopen(cmd, cwd=root_dir)
@@ -717,7 +699,8 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
 
     if thisbuild['type'] == 'maven':
         stdout_apk = '\n'.join([
-            line for line in p.output.splitlines() if any(a in line for a in ('.apk', '.ap_'))])
+            line for line in p.output.splitlines() if any(
+                a in line for a in ('.apk', '.ap_', '.jar'))])
         m = re.match(r".*^\[INFO\] .*apkbuilder.*/([^/]*)\.apk",
                      stdout_apk, re.S | re.M)
         if not m:
@@ -726,6 +709,10 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
         if not m:
             m = re.match(r'.*^\[INFO\] [^$]*aapt \[package,[^$]*' + bindir + r'/([^/]+)\.ap[_k][,\]]',
                          stdout_apk, re.S | re.M)
+
+        if not m:
+            m = re.match(r".*^\[INFO\] Building jar: .*/" + bindir + r"/(.+)\.jar",
+                         stdout_apk, re.S | re.M)
         if not m:
             raise BuildException('Failed to find output')
         src = m.group(1)
@@ -734,23 +721,19 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
         src = 'python-for-android/dist/default/bin/{0}-{1}-release.apk'.format(
             bconfig.get('app', 'title'), bconfig.get('app', 'version'))
     elif thisbuild['type'] == 'gradle':
-        basename = app['id']
-        dd = build_dir
-        if thisbuild['subdir']:
-            dd = os.path.join(dd, thisbuild['subdir'])
-            basename = os.path.basename(thisbuild['subdir'])
-        if '@' in thisbuild['gradle']:
-            dd = os.path.join(dd, thisbuild['gradle'].split('@')[1])
-            basename = app['id']
-        if len(flavours) == 1 and flavours[0] == '':
-            name = '-'.join([basename, 'release', 'unsigned'])
-        else:
-            name = '-'.join([basename, '-'.join(flavours), 'release', 'unsigned'])
-        dd = os.path.normpath(dd)
+
         if thisbuild['gradlepluginver'] >= LooseVersion('0.11'):
-            src = os.path.join(dd, 'build', 'outputs', 'apk', name + '.apk')
+            apks_dir = os.path.join(root_dir, 'build', 'outputs', 'apk')
         else:
-            src = os.path.join(dd, 'build', 'apk', name + '.apk')
+            apks_dir = os.path.join(root_dir, 'build', 'apk')
+
+        apks = glob.glob(os.path.join(apks_dir, '*-release-unsigned.apk'))
+        if len(apks) > 1:
+            raise BuildException('More than one resulting apks found in %s' % apks_dir,
+                                 '\n'.join(apks))
+        if len(apks) < 1:
+            raise BuildException('Failed to find gradle output in %s' % apks_dir)
+        src = apks[0]
     elif thisbuild['type'] == 'ant':
         stdout_apk = '\n'.join([
             line for line in p.output.splitlines() if '.apk' in line])
@@ -771,7 +754,7 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
     if not os.path.exists(src):
         raise BuildException("Unsigned apk is not at expected location of " + src)
 
-    p = SilentPopen([config['aapt'], 'dump', 'badging', src])
+    p = SdkToolsPopen(['aapt', 'dump', 'badging', src], output=False)
 
     vercode = None
     version = None
@@ -830,6 +813,19 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
                                 str(thisbuild['vercode']))
                              )
 
+    # Add information for 'fdroid verify' to be able to reproduce the build
+    # environment.
+    if onserver:
+        metadir = os.path.join(tmp_dir, 'META-INF')
+        if not os.path.exists(metadir):
+            os.mkdir(metadir)
+        homedir = os.path.expanduser('~')
+        for fn in ['buildserverid', 'fdroidserverid']:
+            shutil.copyfile(os.path.join(homedir, fn),
+                            os.path.join(metadir, fn))
+            subprocess.call(['jar', 'uf', os.path.abspath(src),
+                             'META-INF/' + fn], cwd=tmp_dir)
+
     # Copy the unsigned apk to our destination directory for further
     # processing (by publish.py)...
     dest = os.path.join(output_dir, common.getapkname(app, thisbuild))
@@ -874,7 +870,7 @@ def trybuild(app, thisbuild, build_dir, output_dir, also_check_dir, srclib_dir,
             if os.path.exists(dest_also):
                 return False
 
-    if thisbuild['disable']:
+    if thisbuild['disable'] and not options.force:
         return False
 
     logging.info("Building version %s (%s) of %s" % (
@@ -987,16 +983,17 @@ def main():
     allapps = metadata.read_metadata(xref=not options.onserver)
 
     apps = common.read_app_args(args, allapps, True)
-    apps = [app for app in apps if (options.force or not app['Disabled']) and
-            len(app['Repo Type']) > 0 and len(app['builds']) > 0]
+    for appid, app in apps.items():
+        if (app['Disabled'] and not options.force) or not app['Repo Type'] or not app['builds']:
+            del apps[appid]
 
-    if len(apps) == 0:
-        raise Exception("No apps to process.")
+    if not apps:
+        raise FDroidException("No apps to process.")
 
     if options.latest:
-        for app in apps:
+        for app in apps.itervalues():
             for build in reversed(app['builds']):
-                if build['disable']:
+                if build['disable'] and not options.force:
                     continue
                 app['builds'] = [build]
                 break
@@ -1010,7 +1007,7 @@ def main():
     # Build applications...
     failed_apps = {}
     build_succeeded = []
-    for app in apps:
+    for appid, app in apps.iteritems():
 
         first = True
 
@@ -1025,7 +1022,7 @@ def main():
                     if app['Repo Type'] == 'srclib':
                         build_dir = os.path.join('build', 'srclib', app['Repo'])
                     else:
-                        build_dir = os.path.join('build', app['id'])
+                        build_dir = os.path.join('build', appid)
 
                     # Set up vcs interface and make sure we have the latest code...
                     logging.debug("Getting {0} vcs interface for {1}"
@@ -1043,37 +1040,40 @@ def main():
                     build_succeeded.append(app)
                     wikilog = "Build succeeded"
             except BuildException as be:
-                logfile = open(os.path.join(log_dir, app['id'] + '.log'), 'a+')
+                logfile = open(os.path.join(log_dir, appid + '.log'), 'a+')
                 logfile.write(str(be))
                 logfile.close()
-                reason = str(be).split('\n', 1)[0] if options.verbose else str(be)
-                logging.error("Could not build app %s due to BuildException: %s" % (
-                    app['id'], reason))
+                print("Could not build app %s due to BuildException: %s" % (appid, be))
                 if options.stop:
                     sys.exit(1)
-                failed_apps[app['id']] = be
+                failed_apps[appid] = be
                 wikilog = be.get_wikitext()
             except VCSException as vcse:
                 reason = str(vcse).split('\n', 1)[0] if options.verbose else str(vcse)
                 logging.error("VCS error while building app %s: %s" % (
-                    app['id'], reason))
+                    appid, reason))
                 if options.stop:
                     sys.exit(1)
-                failed_apps[app['id']] = vcse
+                failed_apps[appid] = vcse
                 wikilog = str(vcse)
             except Exception as e:
                 logging.error("Could not build app %s due to unknown error: %s" % (
-                    app['id'], traceback.format_exc()))
+                    appid, traceback.format_exc()))
                 if options.stop:
                     sys.exit(1)
-                failed_apps[app['id']] = e
+                failed_apps[appid] = e
                 wikilog = str(e)
 
             if options.wiki and wikilog:
                 try:
-                    newpage = site.Pages[app['id'] + '/lastbuild']
+                    # Write a page with the last build log for this version code
+                    lastbuildpage = appid + '/lastbuild_' + thisbuild['vercode']
+                    newpage = site.Pages[lastbuildpage]
                     txt = "Build completed at " + time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime()) + "\n\n" + wikilog
                     newpage.save(txt, summary='Build log')
+                    # Redirect from /lastbuild to the most recent build log
+                    newpage = site.Pages[appid + '/lastbuild']
+                    newpage.save('#REDIRECT [[' + lastbuildpage + ']]', summary='Update redirect')
                 except:
                     logging.error("Error while attempting to publish build log")