misc/collect.py: better error messages
[web/firmware-selector-openwrt-org.git] / misc / collect.py
index e22d4197a795296a28831b952283c16263f32aaa..26b9876b8ee8abb805f0ca02b4cc331f085ea403 100755 (executable)
@@ -15,6 +15,8 @@ import re
 
 SUPPORTED_METADATA_VERSION = 1
 
+assert sys.version_info >= (3, 5), "Python version too old. Python >=3.5.0 needed."
+
 
 # accepts {<file-path>: <file-content>}
 def merge_profiles(profiles, download_url):
@@ -29,7 +31,7 @@ def merge_profiles(profiles, download_url):
                 title.get("vendor", ""), title["model"], title.get("variant", "")
             ).strip()
 
-    def add_profile(id, target, profile, code=None):
+    def add_profile(path, id, target, profile, code=None):
         images = []
         for image in profile["images"]:
             images.append({"name": image["name"], "type": image["type"]})
@@ -41,7 +43,9 @@ def merge_profiles(profiles, download_url):
             title = get_title(entry)
 
             if len(title) == 0:
-                sys.stderr.write("Empty title. Skip title for {}".format(id))
+                sys.stderr.write(
+                    "Empty title. Skip title for {} in {}\n".format(id, path)
+                )
                 continue
 
             output["models"][title] = {"id": id, "target": target, "images": images}
@@ -50,11 +54,11 @@ def merge_profiles(profiles, download_url):
                 output["models"][title]["code"] = code
 
     for path, content in profiles.items():
-        obj = json.loads(content.decode("utf-8"))
+        obj = json.loads(content)
 
         if obj["metadata_version"] != SUPPORTED_METADATA_VERSION:
             sys.stderr.write(
-                "{} has unsupported metadata version: {} => skip".format(
+                "{} has unsupported metadata version: {} => skip\n".format(
                     path, obj["metadata_version"]
                 )
             )
@@ -72,9 +76,9 @@ def merge_profiles(profiles, download_url):
         try:
             if "profiles" in obj:
                 for id in obj["profiles"]:
-                    add_profile(id, obj.get("target"), obj["profiles"][id], code)
+                    add_profile(path, id, obj.get("target"), obj["profiles"][id], code)
             else:
-                add_profile(obj["id"], obj["target"], obj, code)
+                add_profile(path, obj["id"], obj["target"], obj, code)
         except json.decoder.JSONDecodeError as e:
             sys.stderr.write("Skip {}\n   {}\n".format(path, e))
         except KeyError as e:
@@ -86,11 +90,11 @@ def merge_profiles(profiles, download_url):
 
 def update_config(config_path, versions):
     content = ""
-    with open(config_path, "r") as file:
+    with open(str(config_path), "r") as file:
         content = file.read()
 
     content = re.sub("versions:[\\s]*{[^}]*}", "versions: {}".format(versions), content)
-    with open(config_path, "w+") as file:
+    with open(str(config_path), "w+") as file:
         file.write(content)
 
 
@@ -114,7 +118,9 @@ def scrape(args):
             array = json.loads(file.read().decode("utf-8"))
             for profile in filter(lambda x: x.endswith("/profiles.json"), array):
                 with urllib.request.urlopen("{}/{}".format(target, profile)) as file:
-                    profiles["{}/{}".format(target, profile)] = file.read()
+                    profiles["{}/{}".format(target, profile)] = file.read().decode(
+                        "utf-8"
+                    )
         return profiles
 
     if not os.path.isfile(config_path):
@@ -180,7 +186,7 @@ def scrape_wget(args):
 
             profiles = {}
             for ppath in Path(path).rglob("profiles.json"):
-                with open(ppath, "r") as file:
+                with open(str(ppath), "r") as file:
                     profiles[ppath] = file.read()
 
             if len(profiles) == 0:
@@ -214,7 +220,7 @@ def merge(args):
     profiles = {}
 
     def add_path(path):
-        with open(path, "r") as file:
+        with open(str(path), "r") as file:
             profiles[path] = file.read()
 
     for path in input_paths:
@@ -257,7 +263,7 @@ def scan(args):
 
         profiles = {}
         for ppath in Path(path).rglob("profiles.json"):
-            with open(ppath, "r") as file:
+            with open(str(ppath), "r", encoding="utf-8") as file:
                 profiles[ppath] = file.read()
 
         if len(profiles) == 0: