From: Hans-Christoph Steiner Date: Tue, 14 Jun 2016 09:43:07 +0000 (+0200) Subject: parse targetSdkVersion from APKs X-Git-Tag: 0.7.0~52^2 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=1b7a8f85fc1b90c663a0af6f0bb36cc23e5ce28e;p=fdroidserver.git parse targetSdkVersion from APKs The default targetSdkVersion is minSdkVersion, according to the docs: https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#target https://gitlab.com/fdroid/fdroidclient/issues/682 --- diff --git a/.gitignore b/.gitignore index 9a46bd2d..1c57aac4 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ docs/html/ # files generated by tests tmp/ +tests/repo/icons* diff --git a/fdroidserver/update.py b/fdroidserver/update.py index 570d249b..cd75449d 100644 --- a/fdroidserver/update.py +++ b/fdroidserver/update.py @@ -524,6 +524,16 @@ def scan_apks(apps, apkcache, repodir, knownapks, use_date_from_apk=False): + ' is not a valid minSdkVersion!') else: apk['minSdkVersion'] = m.group(1) + # if target not set, default to min + if 'targetSdkVersion' not in apk: + apk['targetSdkVersion'] = m.group(1) + elif line.startswith("targetSdkVersion:"): + m = re.match(sdkversion_pat, line) + if m is None: + logging.error(line.replace('targetSdkVersion:', '') + + ' is not a valid targetSdkVersion!') + else: + apk['targetSdkVersion'] = m.group(1) elif line.startswith("maxSdkVersion:"): apk['maxSdkVersion'] = re.match(sdkversion_pat, line).group(1) elif line.startswith("native-code:"): @@ -801,7 +811,7 @@ def make_index(apps, sortedids, apks, repodir, archive, categories): for mirror in config.get('mirrors', []): addElement('mirror', urllib.parse.urljoin(mirror, urlbasepath), doc, repoel) - repoel.setAttribute("version", "15") + repoel.setAttribute("version", "16") repoel.setAttribute("timestamp", str(int(time.time()))) nosigningkey = False @@ -937,6 +947,8 @@ def make_index(apps, sortedids, apks, repodir, archive, categories): addElement('sig', apk['sig'], doc, apkel) addElement('size', str(apk['size']), doc, apkel) addElement('sdkver', str(apk['minSdkVersion']), doc, apkel) + if 'targetSdkVersion' in apk: + addElement('targetSdkVersion', str(apk['targetSdkVersion']), doc, apkel) if 'maxSdkVersion' in apk: addElement('maxsdkver', str(apk['maxSdkVersion']), doc, apkel) if 'added' in apk: diff --git a/tests/repo/urzip.apk b/tests/repo/urzip.apk new file mode 100644 index 00000000..ee5e5cba Binary files /dev/null and b/tests/repo/urzip.apk differ diff --git a/tests/update.TestCase b/tests/update.TestCase index 6de1cf21..1005a158 100755 --- a/tests/update.TestCase +++ b/tests/update.TestCase @@ -83,6 +83,32 @@ class UpdateTest(unittest.TestCase): pysig = fdroidserver.update.getsig(apkfile) self.assertIsNone(pysig, "python sig should be None: " + str(sig)) + def testScanApks(self): + os.chdir(os.path.dirname(__file__)) + if os.path.basename(os.getcwd()) != 'tests': + raise Exception('This test must be run in the "tests/" subdir') + + config = dict() + fdroidserver.common.fill_config_defaults(config) + config['ndk_paths'] = dict() + config['accepted_formats'] = ['json', 'txt', 'xml', 'yml'] + fdroidserver.common.config = config + fdroidserver.update.config = config + + fdroidserver.update.options = type('', (), {})() + fdroidserver.update.options.clean = True + + alltestapps = fdroidserver.metadata.read_metadata(xref=True) + apps = dict() + apps['info.guardianproject.urzip'] = alltestapps['info.guardianproject.urzip'] + knownapks = fdroidserver.common.KnownApks() + apks, cachechanged = fdroidserver.update.scan_apks(apps, {}, 'repo', knownapks, False) + self.assertEqual(len(apks), 1) + apk = apks[0] + self.assertEqual(apk['minSdkVersion'], '4') + self.assertEqual(apk['targetSdkVersion'], '18') + self.assertFalse('maxSdkVersion' in apk) + if __name__ == "__main__": parser = optparse.OptionParser()