diff --git a/fdroidserver/common.py b/fdroidserver/common.py index fbfc8ddb..c30fb548 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -74,6 +74,7 @@ VERCODE_OPERATION_RE = re.compile(r'^([ 0-9/*+-]|%c)+$') # A signature block file with a .DSA, .RSA, or .EC extension CERT_PATH_REGEX = re.compile(r'^META-INF/.*\.(DSA|EC|RSA)$') APK_NAME_REGEX = re.compile(r'^([a-zA-Z][\w.]*)_(-?[0-9]+)_?([0-9a-f]{7})?\.apk') +APK_ID_TRIPLET_REGEX = re.compile(r"^package: name='(\w[^']*)' versionCode='([^']+)' versionName='([^']*)'") STANDARD_FILE_NAME_REGEX = re.compile(r'^(\w[\w.]*)_(-?[0-9]+)\.\w+') FDROID_PACKAGE_NAME_REGEX = re.compile(r'''^[a-f0-9]+$''', re.IGNORECASE) STRICT_APPLICATION_ID_REGEX = re.compile(r'''(?:^[a-z_]+(?:\d*[a-zA-Z_]*)*)(?:\.[a-z_]+(?:\d*[a-zA-Z_]*)*)*$''') @@ -2118,12 +2119,11 @@ def get_apk_id_androguard(apkfile): def get_apk_id_aapt(apkfile): - r = re.compile("^package: name='(?P.*)' versionCode='(?P.*)' versionName='(?P.*?)'(?: platformBuildVersionName='.*')?") p = SdkToolsPopen(['aapt', 'dump', 'badging', apkfile], output=False) for line in p.output.splitlines(): - m = r.match(line) + m = APK_ID_TRIPLET_REGEX.match(line) if m: - return m.group('appid'), m.group('vercode'), m.group('vername') + return m.group(1), m.group(2), m.group(3) raise FDroidException(_("Reading packageName/versionCode/versionName failed, APK invalid: '{apkfilename}'") .format(apkfilename=apkfile)) diff --git a/tests/common.TestCase b/tests/common.TestCase index 9e2076d0..21929dbf 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -2,6 +2,7 @@ # http://www.drdobbs.com/testing/unit-testing-with-python/240165163 +import glob import inspect import logging import optparse @@ -570,7 +571,7 @@ class CommonTest(unittest.TestCase): print('\n\nSKIPPING test_sign_apk min SDK check, aapt is not installed!\n') return - def test_get_api_id(self): + def test_get_apk_id(self): config = dict() fdroidserver.common.fill_config_defaults(config) @@ -608,7 +609,6 @@ class CommonTest(unittest.TestCase): ('repo/urzip-; Рахма́, [rɐxˈmanʲɪnəf] سيرجي_رخمانينوف 谢·.apk', 'info.guardianproject.urzip', '100', '0.1'), ] for apkfilename, appid, versionCode, versionName in testcases: - print('\n\nAPKFILENAME\n', apkfilename) if 'aapt' in config: a, vc, vn = fdroidserver.common.get_apk_id_aapt(apkfilename) self.assertEqual(appid, a) @@ -623,6 +623,19 @@ class CommonTest(unittest.TestCase): with self.assertRaises(FDroidException): fdroidserver.common.get_apk_id('nope') + 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)) + for f in files: + appid, versionCode = os.path.splitext(os.path.basename(f))[0][12:].split('_') + with open(f) as fp: + m = fdroidserver.common.APK_ID_TRIPLET_REGEX.match(fp.read()) + if m: + self.assertEqual(appid, m.group(1)) + self.assertEqual(versionCode, m.group(2)) + else: + self.fail('could not parse aapt output: {}'.format(f)) + def test_get_minSdkVersion_aapt(self): config = dict() fdroidserver.common.fill_config_defaults(config)