scripts: make-index-json: rework for old Python versions
authorEric Fahlgren <[email protected]>
Mon, 23 Jun 2025 22:50:23 +0000 (15:50 -0700)
committerPaul Spooren <[email protected]>
Tue, 24 Jun 2025 21:59:06 +0000 (23:59 +0200)
The current code uses functions and features only found in newer
versions of Python, so rework to allow use on systems only supporting
older Python.  Tested on Python 3.8 (released Oct 2019), but should
work on 3.7 also.

Suggested-by: Chen Minqiang <[email protected]>
Signed-off-by: Eric Fahlgren <[email protected]>
scripts/make-index-json.py

index af5c6cf0735d4dcc3ed0b89903b8c2f62b948252..4d71c3fca32b8178b55c58dca306b1b90ea9de92 100755 (executable)
@@ -14,6 +14,12 @@ import email.parser
 import json
 
 
+def removesuffix(src, suffix):
+    # For compatibility with Python < 3.9.
+    suffix_length = len(suffix)
+    return src[:-suffix_length] if suffix_length and src.endswith(suffix) else src
+
+
 def parse_args():
     from argparse import ArgumentParser
 
@@ -42,7 +48,7 @@ def parse_apk(text: str) -> dict:
         for tag in package.get("tags", []):
             if tag.startswith("openwrt:abiversion="):
                 package_abi: str = tag.split("=")[-1]
-                package_name = package_name.removesuffix(package_abi)
+                package_name = removesuffix(package_name, package_abi)
                 break
 
         packages[package_name] = package["version"]
@@ -58,8 +64,9 @@ def parse_opkg(text: str) -> dict:
     for chunk in chunks:
         package: dict = parser.parsestr(chunk, headersonly=True)
         package_name: str = package["Package"]
-        if package_abi := package.get("ABIVersion"):
-            package_name = package_name.removesuffix(package_abi)
+        package_abi = package.get("ABIVersion")
+        if package_abi:
+            package_name = removesuffix(package_name, package_abi)
 
         packages[package_name] = package["Version"]