chiark / gitweb /
reusable method for checking if a value is a resId or not
[fdroidserver.git] / fdroidserver / update.py
index 058cd5ea7bd11b7e567e0fb67389eab807898a7a..ebd29a2c508ce8e0a2ba1c7ac78ef630dd8d9ce9 100644 (file)
@@ -53,7 +53,7 @@ UNSET_VERSION_CODE = -0x100000000
 APK_NAME_PAT = re.compile(".*name='([a-zA-Z0-9._]*)'.*")
 APK_VERCODE_PAT = re.compile(".*versionCode='([0-9]*)'.*")
 APK_VERNAME_PAT = re.compile(".*versionName='([^']*)'.*")
-APK_LABEL_ICON_PAT = re.compile(".*\s+label='(.*)'\s+icon='(.*)'")
+APK_LABEL_ICON_PAT = re.compile(".*\s+label='(.*)'\s+icon='(.*?)'")
 APK_SDK_VERSION_PAT = re.compile(".*'([0-9]*)'.*")
 APK_PERMISSION_PAT = \
     re.compile(".*(name='(?P<name>.*?)')(.*maxSdkVersion='(?P<maxSdkVersion>.*?)')?.*")
@@ -272,12 +272,9 @@ def update_wiki(apps, sortedids, apks):
         # Make a redirect from the name to the ID too, unless there's
         # already an existing page with the name and it isn't a redirect.
         noclobber = False
-        apppagename = app.Name.replace('_', ' ')
-        apppagename = apppagename.replace('{', '')
-        apppagename = apppagename.replace('}', ' ')
-        apppagename = apppagename.replace(':', ' ')
-        apppagename = apppagename.replace('[', ' ')
-        apppagename = apppagename.replace(']', ' ')
+        apppagename = app.Name
+        for ch in '_{}:[]|':
+            apppagename = apppagename.replace(ch, ' ')
         # Drop double spaces caused mostly by replacing ':' above
         apppagename = apppagename.replace('  ', ' ')
         for expagename in site.allpages(prefix=apppagename,
@@ -302,7 +299,7 @@ def update_wiki(apps, sortedids, apks):
         for page in catpages:
             existingpages.append(page.name)
             if page.name in genp:
-                pagetxt = page.edit()
+                pagetxt = page.text()
                 if pagetxt != genp[page.name]:
                     logging.debug("Updating modified page " + page.name)
                     page.save(genp[page.name], summary='Auto-updated')
@@ -996,7 +993,7 @@ def scan_repo_files(apkcache, repodir, knownapks, use_date_from_file=False):
             repo_file['hash'] = shasum
             repo_file['hashType'] = 'sha256'
             repo_file['versionCode'] = 0
-            repo_file['versionName'] = shasum
+            repo_file['versionName'] = shasum[0:7]
             # the static ID is the SHA256 unless it is set in the metadata
             repo_file['packageName'] = shasum
 
@@ -1086,7 +1083,7 @@ def _get_apk_icons_src(apkfile, icon_name):
 
     """
     icons_src = dict()
-    density_re = re.compile('^res/(.*)/' + icon_name + '\.(png|xml)$')
+    density_re = re.compile('^res/(.*)/{}\.(png|xml)$'.format(icon_name))
     with zipfile.ZipFile(apkfile) as zf:
         for filename in zf.namelist():
             m = density_re.match(filename)
@@ -1184,6 +1181,46 @@ def scan_apk_aapt(apk, apkfile):
     apk['icons_src'] = _get_apk_icons_src(apkfile, icon_name)
 
 
+def _ensure_final_value(packageName, arsc, value):
+    """Ensure incoming value is always the value, not the resid
+
+    androguard will sometimes return the Android "resId" aka
+    Resource ID instead of the actual value.  This checks whether
+    the value is actually a resId, then performs the Android
+    Resource lookup as needed.
+
+    """
+    if value:
+        returnValue = value
+        if value[0] == '@':
+            try:  # can be a literal value or a resId
+                res_id = int(value.replace("@", "0x"), 16)
+                res_id = arsc.get_id(packageName, res_id)[1]
+                returnValue = arsc.get_string(packageName, res_id)[1]
+            except ValueError:
+                pass
+        return returnValue
+
+
+def _sanitize_sdk_version(value):
+    """Sanitize the raw values from androguard to handle bad values
+
+    minSdkVersion/targetSdkVersion/maxSdkVersion must be integers,
+    but that doesn't stop devs from doing strange things like
+    setting them using Android XML strings.
+
+    https://gitlab.com/souch/SMSbypass/blob/v0.9/app/src/main/AndroidManifest.xml#L29
+    https://gitlab.com/souch/SMSbypass/blob/v0.9/app/src/main/res/values/strings.xml#L27
+    """
+    try:
+        sdk_version = int(value)
+        if sdk_version > 0:
+            return str(sdk_version)  # heinous, but this is still str in the codebase
+    except (TypeError, ValueError):
+        pass
+    return None
+
+
 def scan_apk_androguard(apk, apkfile):
     try:
         from androguard.core.bytecodes.apk import APK
@@ -1211,19 +1248,22 @@ def scan_apk_androguard(apk, apkfile):
 
     apk['packageName'] = apkobject.get_package()
     apk['versionCode'] = int(apkobject.get_androidversion_code())
-    apk['versionName'] = apkobject.get_androidversion_name()
-    if apk['versionName'][0] == "@":
-        version_id = int(apk['versionName'].replace("@", "0x"), 16)
-        version_id = arsc.get_id(apk['packageName'], version_id)[1]
-        apk['versionName'] = arsc.get_string(apk['packageName'], version_id)[1]
     apk['name'] = apkobject.get_app_name()
 
-    if apkobject.get_max_sdk_version() is not None:
-        apk['maxSdkVersion'] = apkobject.get_max_sdk_version()
-    if apkobject.get_min_sdk_version() is not None:
-        apk['minSdkVersion'] = apkobject.get_min_sdk_version()
-    if apkobject.get_target_sdk_version() is not None:
-        apk['targetSdkVersion'] = apkobject.get_target_sdk_version()
+    apk['versionName'] = _ensure_final_value(apk['packageName'], arsc,
+                                             apkobject.get_androidversion_name())
+
+    minSdkVersion = _sanitize_sdk_version(apkobject.get_min_sdk_version())
+    if minSdkVersion is not None:
+        apk['minSdkVersion'] = minSdkVersion
+
+    targetSdkVersion = _sanitize_sdk_version(apkobject.get_target_sdk_version())
+    if targetSdkVersion is not None:
+        apk['targetSdkVersion'] = targetSdkVersion
+
+    maxSdkVersion = _sanitize_sdk_version(apkobject.get_max_sdk_version())
+    if maxSdkVersion is not None:
+        apk['maxSdkVersion'] = maxSdkVersion
 
     icon_id_str = apkobject.get_element("application", "icon")
     if icon_id_str:
@@ -1242,10 +1282,13 @@ def scan_apk_androguard(apk, apkfile):
         apk['nativecode'].extend(sorted(list(arch)))
 
     xml = apkobject.get_android_manifest_xml()
+    xmlns = xml.nsmap.get('android')
+    if not xmlns:
+        xmlns = 'http://schemas.android.com/apk/res/android'
 
     for item in xml.findall('uses-permission'):
-        name = str(item.attrib['{' + xml.nsmap['android'] + '}name'])
-        maxSdkVersion = item.attrib.get('{' + xml.nsmap['android'] + '}maxSdkVersion')
+        name = str(item.attrib['{' + xmlns + '}name'])
+        maxSdkVersion = item.attrib.get('{' + xmlns + '}maxSdkVersion')
         maxSdkVersion = int(maxSdkVersion) if maxSdkVersion else None
         permission = UsesPermission(
             name,
@@ -1260,8 +1303,8 @@ def scan_apk_androguard(apk, apkfile):
         apk['uses-permission'].append(permission)
 
     for item in xml.findall('uses-permission-sdk-23'):
-        name = str(item.attrib['{' + xml.nsmap['android'] + '}name'])
-        maxSdkVersion = item.attrib.get('{' + xml.nsmap['android'] + '}maxSdkVersion')
+        name = str(item.attrib['{' + xmlns + '}name'])
+        maxSdkVersion = item.attrib.get('{' + xmlns + '}maxSdkVersion')
         maxSdkVersion = int(maxSdkVersion) if maxSdkVersion else None
         permission_sdk_23 = UsesPermissionSdk23(
             name,
@@ -1270,7 +1313,7 @@ def scan_apk_androguard(apk, apkfile):
         apk['uses-permission-sdk-23'].append(permission_sdk_23)
 
     for item in xml.findall('uses-feature'):
-        key = '{' + xml.nsmap['android'] + '}name'
+        key = '{' + xmlns + '}name'
         if key not in item.attrib:
             continue
         feature = str(item.attrib[key])
@@ -1278,7 +1321,7 @@ def scan_apk_androguard(apk, apkfile):
                 and feature != "android.hardware.screen.landscape":
             if feature.startswith("android.feature."):
                 feature = feature[16:]
-        required = item.attrib.get('{' + xml.nsmap['android'] + '}required')
+        required = item.attrib.get('{' + xmlns + '}required')
         if required is None or required == 'true':
             apk['features'].append(feature)