From c6dcc82ca4f62fc69dff31c4cdd026f72fc39d5f Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 27 Apr 2022 13:18:04 +0200 Subject: [PATCH] allow common.get_apk_id() to be used in the API If a project uses fdroidserver as a library, then just calls common.get_apk_id(), it will now work. Before, that project would have had to include something like `common.config = {}` to avoid a stacktrace. --- fdroidserver/common.py | 6 ++++-- tests/common.TestCase | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 6aa44a28..36ec0071 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -2689,9 +2689,11 @@ def get_apk_id(apkfile): try: return get_apk_id_androguard(apkfile) except zipfile.BadZipFile as e: - logging.error(apkfile + ': ' + str(e)) - if 'aapt' in config: + if config and 'aapt' in config: + logging.error(apkfile + ': ' + str(e)) return get_apk_id_aapt(apkfile) + else: + raise e def get_apk_id_androguard(apkfile): diff --git a/tests/common.TestCase b/tests/common.TestCase index 955de457..c511901d 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -21,7 +21,7 @@ import textwrap import yaml import gzip import stat -from zipfile import ZipFile, ZipInfo +from zipfile import BadZipFile, ZipFile, ZipInfo from unittest import mock from pathlib import Path @@ -959,9 +959,24 @@ class CommonTest(unittest.TestCase): self.assertEqual(versionCode, vc, 'aapt versionCode parsing failed for ' + apkfilename) self.assertEqual(versionName, vn, 'aapt versionName parsing failed for ' + apkfilename) + def test_get_apk_id_bad_path(self): with self.assertRaises(FDroidException): fdroidserver.common.get_apk_id('nope') + def test_get_apk_id_api_call(self): + self.assertEqual( + ('info.guardianproject.urzip', '100', '0.1'), + fdroidserver.common.get_apk_id('urzip.apk'), + ) + + def test_get_apk_id_bad_zip(self): + os.chdir(self.tmpdir) + badzip = 'badzip.apk' + with open(badzip, 'w') as fp: + fp.write('not a ZIP') + with self.assertRaises(BadZipFile): + fdroidserver.common.get_apk_id(badzip) + def test_get_apk_id_aapt_regex(self): files = glob.glob(os.path.join(self.basedir, 'build-tools', '[1-9]*.*', '*.txt')) self.assertNotEqual(0, len(files))