chiark / gitweb /
parse targetSdkVersion from APKs
authorHans-Christoph Steiner <hans@eds.org>
Tue, 14 Jun 2016 09:43:07 +0000 (11:43 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Tue, 14 Jun 2016 09:43:07 +0000 (11:43 +0200)
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

.gitignore
fdroidserver/update.py
tests/repo/urzip.apk [new file with mode: 0644]
tests/update.TestCase

index 9a46bd2de5e1c2c8351507a4f79d9f29a016fa22..1c57aac4c89329ef613eeda72604d7e0f2b04b50 100644 (file)
@@ -16,3 +16,4 @@ docs/html/
 
 # files generated by tests
 tmp/
+tests/repo/icons*
index 570d249b2cfb36e650bd502ad83b16572109859e..cd75449d34f5aab06f619dab1645543bde8476d0 100644 (file)
@@ -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 (file)
index 0000000..ee5e5cb
Binary files /dev/null and b/tests/repo/urzip.apk differ
index 6de1cf218601599ef62a00ad1028a3e180cf0db8..1005a1582608c138dd76078350890bc152a784bd 100755 (executable)
@@ -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()