chiark / gitweb /
Merge branch 'gradleFlavor' into 'master'
[fdroidserver.git] / fdroidserver / common.py
index 2857ac06323489b1f4f7f0822185f18707e486d2..6ed43d4c4e465eda49d752eb7ceea09d7eb6af5a 100644 (file)
@@ -36,10 +36,11 @@ import socket
 import base64
 import zipfile
 import tempfile
+import json
 import xml.etree.ElementTree as XMLElementTree
 
 from binascii import hexlify
-from datetime import datetime
+from datetime import datetime, timedelta
 from distutils.version import LooseVersion
 from queue import Queue
 from zipfile import ZipFile
@@ -79,6 +80,7 @@ default_config = {
         'r13b': None,
         'r14b': None,
         'r15c': None,
+        'r16': None,
     },
     'qt_sdk_path': None,
     'build_tools': "25.0.2",
@@ -131,6 +133,41 @@ def setup_global_opts(parser):
                         help=_("Restrict output to warnings and errors"))
 
 
+def _add_java_paths_to_config(pathlist, thisconfig):
+    def path_version_key(s):
+        versionlist = []
+        for u in re.split('[^0-9]+', s):
+            try:
+                versionlist.append(int(u))
+            except ValueError:
+                pass
+        return versionlist
+
+    for d in sorted(pathlist, key=path_version_key):
+        if os.path.islink(d):
+            continue
+        j = os.path.basename(d)
+        # the last one found will be the canonical one, so order appropriately
+        for regex in [
+                r'^1\.([6-9])\.0\.jdk$',  # OSX
+                r'^jdk1\.([6-9])\.0_[0-9]+.jdk$',  # OSX and Oracle tarball
+                r'^jdk1\.([6-9])\.0_[0-9]+$',  # Oracle Windows
+                r'^jdk([6-9])-openjdk$',  # Arch
+                r'^java-([6-9])-openjdk$',  # Arch
+                r'^java-([6-9])-jdk$',  # Arch (oracle)
+                r'^java-1\.([6-9])\.0-.*$',  # RedHat
+                r'^java-([6-9])-oracle$',  # Debian WebUpd8
+                r'^jdk-([6-9])-oracle-.*$',  # Debian make-jpkg
+                r'^java-([6-9])-openjdk-[^c][^o][^m].*$',  # Debian
+                ]:
+            m = re.match(regex, j)
+            if not m:
+                continue
+            for p in [d, os.path.join(d, 'Contents', 'Home')]:
+                if os.path.exists(os.path.join(p, 'bin', 'javac')):
+                    thisconfig['java_paths'][m.group(1)] = p
+
+
 def fill_config_defaults(thisconfig):
     for k, v in default_config.items():
         if k not in thisconfig:
@@ -166,29 +203,7 @@ def fill_config_defaults(thisconfig):
             pathlist.append(os.getenv('JAVA_HOME'))
         if os.getenv('PROGRAMFILES') is not None:
             pathlist += glob.glob(os.path.join(os.getenv('PROGRAMFILES'), 'Java', 'jdk1.[6-9].*'))
-        for d in sorted(pathlist):
-            if os.path.islink(d):
-                continue
-            j = os.path.basename(d)
-            # the last one found will be the canonical one, so order appropriately
-            for regex in [
-                    r'^1\.([6-9])\.0\.jdk$',  # OSX
-                    r'^jdk1\.([6-9])\.0_[0-9]+.jdk$',  # OSX and Oracle tarball
-                    r'^jdk1\.([6-9])\.0_[0-9]+$',  # Oracle Windows
-                    r'^jdk([6-9])-openjdk$',  # Arch
-                    r'^java-([6-9])-openjdk$',  # Arch
-                    r'^java-([6-9])-jdk$',  # Arch (oracle)
-                    r'^java-1\.([6-9])\.0-.*$',  # RedHat
-                    r'^java-([6-9])-oracle$',  # Debian WebUpd8
-                    r'^jdk-([6-9])-oracle-.*$',  # Debian make-jpkg
-                    r'^java-([6-9])-openjdk-[^c][^o][^m].*$',  # Debian
-                    ]:
-                m = re.match(regex, j)
-                if not m:
-                    continue
-                for p in [d, os.path.join(d, 'Contents', 'Home')]:
-                    if os.path.exists(os.path.join(p, 'bin', 'javac')):
-                        thisconfig['java_paths'][m.group(1)] = p
+        _add_java_paths_to_config(pathlist, thisconfig)
 
     for java_version in ('7', '8', '9'):
         if java_version not in thisconfig['java_paths']:
@@ -301,6 +316,30 @@ def read_config(opts, config_file='config.py'):
     return config
 
 
+def assert_config_keystore(config):
+    """Check weather keystore is configured correctly and raise exception if not."""
+
+    nosigningkey = False
+    if 'repo_keyalias' not in config:
+        nosigningkey = True
+        logging.critical(_("'repo_keyalias' not found in config.py!"))
+    if 'keystore' not in config:
+        nosigningkey = True
+        logging.critical(_("'keystore' not found in config.py!"))
+    elif not os.path.exists(config['keystore']):
+        nosigningkey = True
+        logging.critical("'" + config['keystore'] + "' does not exist!")
+    if 'keystorepass' not in config:
+        nosigningkey = True
+        logging.critical(_("'keystorepass' not found in config.py!"))
+    if 'keypass' not in config:
+        nosigningkey = True
+        logging.critical(_("'keypass' not found in config.py!"))
+    if nosigningkey:
+        raise FDroidException("This command requires a signing key, " +
+                              "you can create one using: fdroid update --create-key")
+
+
 def find_sdk_tools_cmd(cmd):
     '''find a working path to a tool from the Android SDK'''
 
@@ -391,7 +430,7 @@ def ensure_build_tools_exists(thisconfig):
     versioned_build_tools = os.path.join(build_tools, thisconfig['build_tools'])
     if not os.path.isdir(versioned_build_tools):
         raise FDroidException(
-            _("Android Build Tools path '{path}' does not exist!")
+            _("Android build-tools path '{path}' does not exist!")
             .format(path=versioned_build_tools))
 
 
@@ -405,17 +444,16 @@ def get_local_metadata_files():
     return glob.glob('.fdroid.[a-jl-z]*[a-rt-z]')
 
 
-def read_pkg_args(args, allow_vercodes=False):
+def read_pkg_args(appid_versionCode_pairs, allow_vercodes=False):
     """
-    :param args: arguments in the form of multiple appid:[vc] strings
+    :param appids: arguments in the form of multiple appid:[vc] strings
     :returns: a dictionary with the set of vercodes specified for each package
     """
-
     vercodes = {}
-    if not args:
+    if not appid_versionCode_pairs:
         return vercodes
 
-    for p in args:
+    for p in appid_versionCode_pairs:
         if allow_vercodes and ':' in p:
             package, vercode = p.split(':')
         else:
@@ -429,13 +467,17 @@ def read_pkg_args(args, allow_vercodes=False):
     return vercodes
 
 
-def read_app_args(args, allapps, allow_vercodes=False):
-    """
-    On top of what read_pkg_args does, this returns the whole app metadata, but
-    limiting the builds list to the builds matching the vercodes specified.
+def read_app_args(appid_versionCode_pairs, allapps, allow_vercodes=False):
+    """Build a list of App instances for processing
+
+    On top of what read_pkg_args does, this returns the whole app
+    metadata, but limiting the builds list to the builds matching the
+    appid_versionCode_pairs and vercodes specified.  If no appid_versionCode_pairs are specified, then
+    all App and Build instances are returned.
+
     """
 
-    vercodes = read_pkg_args(args, allow_vercodes)
+    vercodes = read_pkg_args(appid_versionCode_pairs, allow_vercodes)
 
     if not vercodes:
         return allapps
@@ -481,7 +523,7 @@ def get_extension(filename):
 
 
 def has_extension(filename, ext):
-    _, f_ext = get_extension(filename)
+    _ignored, f_ext = get_extension(filename)
     return ext == f_ext
 
 
@@ -511,11 +553,23 @@ def publishednameinfo(filename):
     return result
 
 
-apk_release_filename = re.compile('(?P<appid>[a-z0-9_\.]+)_(?P<vercode>[0-9]+)\.apk')
-apk_release_filename_with_sigfp = re.compile('(?P<appid>[a-z0-9_\.]+)_(?P<vercode>[0-9]+)_(?P<sigfp>[0-9a-f]{7})\.apk')
+apk_release_filename = re.compile('(?P<appid>[a-zA-Z0-9_\.]+)_(?P<vercode>[0-9]+)\.apk')
+apk_release_filename_with_sigfp = re.compile('(?P<appid>[a-zA-Z0-9_\.]+)_(?P<vercode>[0-9]+)_(?P<sigfp>[0-9a-f]{7})\.apk')
 
 
 def apk_parse_release_filename(apkname):
+    """Parses the name of an APK file according the F-Droids APK naming
+    scheme and returns the tokens.
+
+    WARNING: Returned values don't necessarily represent the APKs actual
+    properties, the are just paresed from the file name.
+
+    :returns: A triplet containing (appid, versionCode, signer), where appid
+        should be the package name, versionCode should be the integer
+        represion of the APKs version and signer should be the first 7 hex
+        digists of the sha256 signing key fingerprint which was used to sign
+        this APK.
+    """
     m = apk_release_filename_with_sigfp.match(apkname)
     if m:
         return m.group('appid'), m.group('vercode'), m.group('sigfp')
@@ -625,6 +679,13 @@ class vcs:
     def repotype(self):
         return None
 
+    def clientversion(self):
+        versionstr = FDroidPopen(self.clientversioncmd()).output
+        return versionstr[0:versionstr.find('\n')]
+
+    def clientversioncmd(self):
+        return None
+
     def gotorevision(self, rev, refresh=True):
         """Take the local repository to a clean version of the given
         revision, which is specificed in the VCS's native
@@ -722,6 +783,40 @@ class vcs_git(vcs):
     def repotype(self):
         return 'git'
 
+    def clientversioncmd(self):
+        return ['git', '--version']
+
+    def GitFetchFDroidPopen(self, gitargs, envs=dict(), cwd=None, output=True):
+        '''Prevent git fetch/clone/submodule from hanging at the username/password prompt
+
+        While fetch/pull/clone respect the command line option flags,
+        it seems that submodule commands do not.  They do seem to
+        follow whatever is in env vars, if the version of git is new
+        enough.  So we just throw the kitchen sink at it to see what
+        sticks.
+
+        '''
+        if cwd is None:
+            cwd = self.local
+        git_config = []
+        for domain in ('bitbucket.org', 'github.com', 'gitlab.com'):
+            git_config.append('-c')
+            git_config.append('url.https://u:p@' + domain + '/.insteadOf=git@' + domain + ':')
+            git_config.append('-c')
+            git_config.append('url.https://u:p@' + domain + '.insteadOf=git://' + domain)
+            git_config.append('-c')
+            git_config.append('url.https://u:p@' + domain + '.insteadOf=https://' + domain)
+        # add helpful tricks supported in git >= 2.3
+        ssh_command = 'ssh -oBatchMode=yes -oStrictHostKeyChecking=yes'
+        git_config.append('-c')
+        git_config.append('core.sshCommand="' + ssh_command + '"')  # git >= 2.10
+        envs.update({
+            'GIT_TERMINAL_PROMPT': '0',
+            'GIT_SSH_COMMAND': ssh_command,  # git >= 2.3
+        })
+        return FDroidPopen(['git', ] + git_config + gitargs,
+                           envs=envs, cwd=cwd, output=output)
+
     def checkrepo(self):
         """If the local directory exists, but is somehow not a git repository,
         git will traverse up the directory tree until it finds one
@@ -738,7 +833,7 @@ class vcs_git(vcs):
     def gotorevisionx(self, rev):
         if not os.path.exists(self.local):
             # Brand new checkout
-            p = FDroidPopen(['git', 'clone', self.remote, self.local])
+            p = FDroidPopen(['git', 'clone', self.remote, self.local], cwd=None)
             if p.returncode != 0:
                 self.clone_failed = True
                 raise VCSException("Git clone failed", p.output)
@@ -758,10 +853,10 @@ class vcs_git(vcs):
                 raise VCSException(_("Git clean failed"), p.output)
             if not self.refreshed:
                 # Get latest commits and tags from remote
-                p = FDroidPopen(['git', 'fetch', 'origin'], cwd=self.local)
+                p = self.GitFetchFDroidPopen(['fetch', 'origin'])
                 if p.returncode != 0:
                     raise VCSException(_("Git fetch failed"), p.output)
-                p = FDroidPopen(['git', 'fetch', '--prune', '--tags', 'origin'], cwd=self.local, output=False)
+                p = self.GitFetchFDroidPopen(['fetch', '--prune', '--tags', 'origin'], output=False)
                 if p.returncode != 0:
                     raise VCSException(_("Git fetch failed"), p.output)
                 # Recreate origin/HEAD as git clone would do it, in case it disappeared
@@ -797,16 +892,14 @@ class vcs_git(vcs):
             lines = f.readlines()
         with open(submfile, 'w') as f:
             for line in lines:
-                if 'git@github.com' in line:
-                    line = line.replace('git@github.com:', 'https://github.com/')
-                if 'git@gitlab.com' in line:
-                    line = line.replace('git@gitlab.com:', 'https://gitlab.com/')
+                for domain in ('bitbucket.org', 'github.com', 'gitlab.com'):
+                    line = re.sub('git@' + domain + ':', 'https://u:p@' + domain + '/', line)
                 f.write(line)
 
         p = FDroidPopen(['git', 'submodule', 'sync'], cwd=self.local, output=False)
         if p.returncode != 0:
             raise VCSException(_("Git submodule sync failed"), p.output)
-        p = FDroidPopen(['git', 'submodule', 'update', '--init', '--force', '--recursive'], cwd=self.local)
+        p = self.GitFetchFDroidPopen(['submodule', 'update', '--init', '--force', '--recursive'])
         if p.returncode != 0:
             raise VCSException(_("Git submodule update failed"), p.output)
 
@@ -834,6 +927,9 @@ class vcs_gitsvn(vcs):
     def repotype(self):
         return 'git-svn'
 
+    def clientversioncmd(self):
+        return ['git', 'svn', '--version']
+
     def checkrepo(self):
         """If the local directory exists, but is somehow not a git repository,
         git will traverse up the directory tree until it finds one that
@@ -960,6 +1056,9 @@ class vcs_hg(vcs):
     def repotype(self):
         return 'hg'
 
+    def clientversioncmd(self):
+        return ['hg', '--version']
+
     def gotorevisionx(self, rev):
         if not os.path.exists(self.local):
             p = FDroidPopen(['hg', 'clone', self.remote, self.local], output=False)
@@ -1007,6 +1106,9 @@ class vcs_bzr(vcs):
     def repotype(self):
         return 'bzr'
 
+    def clientversioncmd(self):
+        return ['bzr', '--version']
+
     def gotorevisionx(self, rev):
         if not os.path.exists(self.local):
             p = FDroidPopen(['bzr', 'branch', self.remote, self.local], output=False)
@@ -1153,7 +1255,7 @@ def remove_debuggable_flags(root_dir):
     # Remove forced debuggable flags
     logging.debug("Removing debuggable flags from %s" % root_dir)
     for root, dirs, files in os.walk(root_dir):
-        if 'AndroidManifest.xml' in files:
+        if 'AndroidManifest.xml' in files and os.path.isfile(os.path.join(root, 'AndroidManifest.xml')):
             regsub_file(r'android:debuggable="[^"]*"',
                         '',
                         os.path.join(root, 'AndroidManifest.xml'))
@@ -1200,27 +1302,58 @@ def parse_androidmanifests(paths, app):
         vercode = None
         package = None
 
+        flavour = ""
+        if app.builds and 'gradle' in app.builds[-1] and app.builds[-1].gradle:
+                flavour = app.builds[-1].gradle[-1]
+
         if has_extension(path, 'gradle'):
+            # first try to get version name and code from correct flavour
             with open(path, 'r') as f:
-                for line in f:
-                    if gradle_comment.match(line):
-                        continue
-                    # Grab first occurence of each to avoid running into
-                    # alternative flavours and builds.
-                    if not package:
-                        matches = psearch_g(line)
-                        if matches:
-                            s = matches.group(2)
-                            if app_matches_packagename(app, s):
-                                package = s
-                    if not version:
-                        matches = vnsearch_g(line)
-                        if matches:
-                            version = matches.group(2)
-                    if not vercode:
-                        matches = vcsearch_g(line)
-                        if matches:
-                            vercode = matches.group(1)
+                buildfile = f.read()
+
+                regex_string = r"" + flavour + ".*?}"
+                search = re.compile(regex_string, re.DOTALL)
+                result = search.search(buildfile)
+
+            if result is not None:
+                resultgroup = result.group()
+
+                if not package:
+                    matches = psearch_g(resultgroup)
+                    if matches:
+                        s = matches.group(2)
+                        if app_matches_packagename(app, s):
+                            package = s
+                if not version:
+                    matches = vnsearch_g(resultgroup)
+                    if matches:
+                        version = matches.group(2)
+                if not vercode:
+                    matches = vcsearch_g(resultgroup)
+                    if matches:
+                        vercode = matches.group(1)
+            else:
+                # fall back to parse file line by line
+                with open(path, 'r') as f:
+                    for line in f:
+                        if gradle_comment.match(line):
+                            continue
+                        # Grab first occurence of each to avoid running into
+                        # alternative flavours and builds.
+                        if not package:
+                            matches = psearch_g(line)
+                            if matches:
+                                s = matches.group(2)
+                                if app_matches_packagename(app, s):
+                                    package = s
+                        if not version:
+                            matches = vnsearch_g(line)
+                            if matches:
+                                version = matches.group(2)
+                        if not vercode:
+                            matches = vcsearch_g(line)
+                            if matches:
+                                vercode = matches.group(1)
         else:
             try:
                 xml = parse_xml(path)
@@ -1514,10 +1647,11 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=
             dest = os.path.join(build_dir, part)
             logging.info("Removing {0}".format(part))
             if os.path.lexists(dest):
-                if os.path.islink(dest):
-                    FDroidPopen(['unlink', dest], output=False)
+                # rmtree can only handle directories that are not symlinks, so catch anything else
+                if not os.path.isdir(dest) or os.path.islink(dest):
+                    os.remove(dest)
                 else:
-                    FDroidPopen(['rm', '-rf', dest], output=False)
+                    shutil.rmtree(dest)
             else:
                 logging.info("...but it didn't exist")
 
@@ -1616,6 +1750,23 @@ def natural_key(s):
     return [int(sp) if sp.isdigit() else sp for sp in re.split(r'(\d+)', s)]
 
 
+def check_system_clock(dt_obj, path):
+    """Check if system clock is updated based on provided date
+
+    If an APK has files newer than the system time, suggest updating
+    the system clock.  This is useful for offline systems, used for
+    signing, which do not have another source of clock sync info. It
+    has to be more than 24 hours newer because ZIP/APK files do not
+    store timezone info
+
+    """
+    checkdt = dt_obj - timedelta(1)
+    if datetime.today() < checkdt:
+        logging.warning(_('System clock is older than date in {path}!').format(path=path)
+                        + '\n' + _('Set clock to that time using:') + '\n'
+                        + 'sudo date -s "' + str(dt_obj) + '"')
+
+
 class KnownApks:
     """permanent store of existing APKs with the date they were added
 
@@ -1644,6 +1795,7 @@ class KnownApks:
                         date = datetime.strptime(t[-1], '%Y-%m-%d')
                         filename = line[0:line.rfind(appid) - 1]
                         self.apks[filename] = (appid, date)
+                        check_system_clock(date, self.path)
         self.changed = False
 
     def writeifchanged(self):
@@ -1675,7 +1827,7 @@ class KnownApks:
                 default_date = datetime.utcnow()
             self.apks[apkName] = (app, default_date)
             self.changed = True
-        _, added = self.apks[apkName]
+        _ignored, added = self.apks[apkName]
         return added
 
     def getapp(self, apkname):
@@ -2036,7 +2188,7 @@ def signer_fingerprint_short(sig):
     Extracts the first 7 hexadecimal digits of sha256 signing-key fingerprint
     for a given pkcs7 signature.
 
-    :param sig: Contents of an APK signature.
+    :param sig: Contents of an APK signing certificate.
     :returns: shortened signing-key fingerprint.
     """
     return signer_fingerprint(sig)[:7]
@@ -2099,6 +2251,40 @@ def metadata_get_sigdir(appid, vercode=None):
         return os.path.join('metadata', appid, 'signatures')
 
 
+def metadata_find_developer_signature(appid, vercode=None):
+    """Tires to find the developer signature for given appid.
+
+    This picks the first signature file found in metadata an returns its
+    signature.
+
+    :returns: sha256 signing key fingerprint of the developer signing key.
+        None in case no signature can not be found."""
+
+    # fetch list of dirs for all versions of signatures
+    appversigdirs = []
+    if vercode:
+        appversigdirs.append(metadata_get_sigdir(appid, vercode))
+    else:
+        appsigdir = metadata_get_sigdir(appid)
+        if os.path.isdir(appsigdir):
+            numre = re.compile('[0-9]+')
+            for ver in os.listdir(appsigdir):
+                if numre.match(ver):
+                    appversigdir = os.path.join(appsigdir, ver)
+                    appversigdirs.append(appversigdir)
+
+    for sigdir in appversigdirs:
+        sigs = glob.glob(os.path.join(sigdir, '*.DSA')) + \
+            glob.glob(os.path.join(sigdir, '*.EC')) + \
+            glob.glob(os.path.join(sigdir, '*.RSA'))
+        if len(sigs) > 1:
+            raise FDroidException('ambiguous signatures, please make sure there is only one signature in \'{}\'. (The signature has to be the App maintainers signature for version of the APK.)'.format(sigdir))
+        for sig in sigs:
+            with open(sig, 'rb') as f:
+                return signer_fingerprint(f.read())
+    return None
+
+
 def metadata_find_signing_files(appid, vercode):
     """Gets a list of singed manifests and signatures.
 
@@ -2147,19 +2333,19 @@ def apk_strip_signatures(signed_apk, strip_manifest=False):
         os.rename(signed_apk, tmp_apk)
         with ZipFile(tmp_apk, 'r') as in_apk:
             with ZipFile(signed_apk, 'w') as out_apk:
-                for f in in_apk.infolist():
-                    if not apk_sigfile.match(f.filename):
+                for info in in_apk.infolist():
+                    if not apk_sigfile.match(info.filename):
                         if strip_manifest:
-                            if f.filename != 'META-INF/MANIFEST.MF':
-                                buf = in_apk.read(f.filename)
-                                out_apk.writestr(f.filename, buf)
+                            if info.filename != 'META-INF/MANIFEST.MF':
+                                buf = in_apk.read(info.filename)
+                                out_apk.writestr(info, buf)
                         else:
-                            buf = in_apk.read(f.filename)
-                            out_apk.writestr(f.filename, buf)
+                            buf = in_apk.read(info.filename)
+                            out_apk.writestr(info, buf)
 
 
 def apk_implant_signatures(apkpath, signaturefile, signedfile, manifest):
-    """Implats a signature from out metadata into an APK.
+    """Implats a signature from metadata into an APK.
 
     Note: this changes there supplied APK in place. So copy it if you
     need the original to be preserved.
@@ -2168,19 +2354,21 @@ def apk_implant_signatures(apkpath, signaturefile, signedfile, manifest):
     """
     # get list of available signature files in metadata
     with tempfile.TemporaryDirectory() as tmpdir:
-        # orig_apk = os.path.join(tmpdir, 'orig.apk')
-        # os.rename(apkpath, orig_apk)
         apkwithnewsig = os.path.join(tmpdir, 'newsig.apk')
         with ZipFile(apkpath, 'r') as in_apk:
             with ZipFile(apkwithnewsig, 'w') as out_apk:
                 for sig_file in [signaturefile, signedfile, manifest]:
-                    out_apk.write(sig_file, arcname='META-INF/' +
-                                  os.path.basename(sig_file))
-                for f in in_apk.infolist():
-                    if not apk_sigfile.match(f.filename):
-                        if f.filename != 'META-INF/MANIFEST.MF':
-                            buf = in_apk.read(f.filename)
-                            out_apk.writestr(f.filename, buf)
+                    with open(sig_file, 'rb') as fp:
+                        buf = fp.read()
+                    info = zipfile.ZipInfo('META-INF/' + os.path.basename(sig_file))
+                    info.compress_type = zipfile.ZIP_DEFLATED
+                    info.create_system = 0  # "Windows" aka "FAT", what Android SDK uses
+                    out_apk.writestr(info, buf)
+                for info in in_apk.infolist():
+                    if not apk_sigfile.match(info.filename):
+                        if info.filename != 'META-INF/MANIFEST.MF':
+                            buf = in_apk.read(info.filename)
+                            out_apk.writestr(info, buf)
         os.remove(apkpath)
         p = SdkToolsPopen(['zipalign', '-v', '4', apkwithnewsig, apkpath])
         if p.returncode != 0:
@@ -2301,7 +2489,7 @@ def verify_apk_signature(apk, min_sdk_version=None):
         try:
             verify_jar_signature(apk)
             return True
-        except:
+        except Exception:
             pass
     return False
 
@@ -2518,6 +2706,34 @@ def get_certificate(certificate_file):
     return encoder.encode(cert)
 
 
+def load_stats_fdroid_signing_key_fingerprints():
+    """Load list of signing-key fingerprints stored by fdroid publish from file.
+
+    :returns: list of dictionanryies containing the singing-key fingerprints.
+    """
+    jar_file = os.path.join('stats', 'publishsigkeys.jar')
+    if not os.path.isfile(jar_file):
+        return {}
+    cmd = [config['jarsigner'], '-strict', '-verify', jar_file]
+    p = FDroidPopen(cmd, output=False)
+    if p.returncode != 4:
+        raise FDroidException("Signature validation of '{}' failed! "
+                              "Please run publish again to rebuild this file.".format(jar_file))
+
+    jar_sigkey = apk_signer_fingerprint(jar_file)
+    repo_key_sig = config.get('repo_key_sha256')
+    if repo_key_sig:
+        if jar_sigkey != repo_key_sig:
+            raise FDroidException("Signature key fingerprint of file '{}' does not match repo_key_sha256 in config.py (found fingerprint: '{}')".format(jar_file, jar_sigkey))
+    else:
+        logging.warning("repo_key_sha256 not in config.py, setting it to the signature key fingerprint of '{}'".format(jar_file))
+        config['repo_key_sha256'] = jar_sigkey
+        write_to_config(config, 'repo_key_sha256')
+
+    with zipfile.ZipFile(jar_file, 'r') as f:
+        return json.loads(str(f.read('publishsigkeys.json'), 'utf-8'))
+
+
 def write_to_config(thisconfig, key, value=None, config_file=None):
     '''write a key/value to the local config.py
 
@@ -2584,6 +2800,28 @@ def string_is_integer(string):
         return False
 
 
+def local_rsync(options, fromdir, todir):
+    '''Rsync method for local to local copying of things
+
+    This is an rsync wrapper with all the settings for safe use within
+    the various fdroidserver use cases. This uses stricter rsync
+    checking on all files since people using offline mode are already
+    prioritizing security above ease and speed.
+
+    '''
+    rsyncargs = ['rsync', '--recursive', '--safe-links', '--times', '--perms',
+                 '--one-file-system', '--delete', '--chmod=Da+rx,Fa-x,a+r,u+w']
+    if not options.no_checksum:
+        rsyncargs.append('--checksum')
+    if options.verbose:
+        rsyncargs += ['--verbose']
+    if options.quiet:
+        rsyncargs += ['--quiet']
+    logging.debug(' '.join(rsyncargs + [fromdir, todir]))
+    if subprocess.call(rsyncargs + [fromdir, todir]) != 0:
+        raise FDroidException()
+
+
 def get_per_app_repos():
     '''per-app repos are dirs named with the packageName of a single app'''
 
@@ -2623,3 +2861,26 @@ def is_repo_file(filename):
             b'index-v1.json',
             b'categories.txt',
         ]
+
+
+def get_examples_dir():
+    '''Return the dir where the fdroidserver example files are available'''
+    examplesdir = None
+    tmp = os.path.dirname(sys.argv[0])
+    if os.path.basename(tmp) == 'bin':
+        egg_links = glob.glob(os.path.join(tmp, '..',
+                                           'local/lib/python3.*/site-packages/fdroidserver.egg-link'))
+        if egg_links:
+            # installed from local git repo
+            examplesdir = os.path.join(open(egg_links[0]).readline().rstrip(), 'examples')
+        else:
+            # try .egg layout
+            examplesdir = os.path.dirname(os.path.dirname(__file__)) + '/share/doc/fdroidserver/examples'
+            if not os.path.exists(examplesdir):  # use UNIX layout
+                examplesdir = os.path.dirname(tmp) + '/share/doc/fdroidserver/examples'
+    else:
+        # we're running straight out of the git repo
+        prefix = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
+        examplesdir = prefix + '/examples'
+
+    return examplesdir