chiark / gitweb /
reusable method for checking if a value is a resId or not
[fdroidserver.git] / fdroidserver / update.py
index 4c865d7124cf58a835689d8048035d2d84b26857..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,
@@ -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
 
@@ -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: