[PATCH 2/3] build/json: factor initramfs json data into profiles.json

Moritz Warning moritzwarning at web.de
Fri Jan 8 18:51:05 EST 2021


The initramfs json data does not contain all fields.
E.g. no default_packages. As such, the python scripts
need to properly merge the json data of the images.

Signed-off-by: Moritz Warning <moritzwarning at web.de>
---
 scripts/json_add_image_info.py      | 28 ++++++++++++------
 scripts/json_overview_image_info.py | 44 ++++++++++++++++++-----------
 2 files changed, 46 insertions(+), 26 deletions(-)

diff --git a/scripts/json_add_image_info.py b/scripts/json_add_image_info.py
index d394cf4d1f..7d87ccd621 100755
--- a/scripts/json_add_image_info.py
+++ b/scripts/json_add_image_info.py
@@ -14,24 +14,31 @@ json_path = Path(argv[1])
 bin_dir = Path(getenv("BIN_DIR"))
 image_file = bin_dir / getenv("IMAGE_NAME")

+
 if not image_file.is_file():
-    print("Skip JSON creation for non existing image ", image_file)
+    print("Skip JSON creation for non existing image: {}".format(image_file))
     exit(0)


+def set(obj, key, value):
+    if value != None and len(value) != 0:
+        obj[key] = value
+
+
 def get_titles():
     titles = []
     for prefix in ["", "ALT0_", "ALT1_", "ALT2_"]:
         title = {}
         for var in ["vendor", "model", "variant"]:
-            if getenv("DEVICE_{}{}".format(prefix, var.upper())):
-                title[var] = getenv("DEVICE_{}{}".format(prefix, var.upper()))
+            set(title, var, getenv("DEVICE_{}{}".format(prefix, var.upper())))

         if title:
             titles.append(title)

     if not titles:
-        titles.append({"title": getenv("DEVICE_TITLE")})
+        title = getenv("DEVICE_TITLE")
+        if title:
+            titles.append({"title": title})

     return titles

@@ -47,7 +54,6 @@ image_info = {
     "source_date_epoch": getenv("SOURCE_DATE_EPOCH"),
     "profiles": {
         device_id: {
-            "image_prefix": getenv("IMAGE_PREFIX"),
             "images": [
                 {
                     "type": getenv("IMAGE_TYPE"),
@@ -55,12 +61,16 @@ image_info = {
                     "name": getenv("IMAGE_NAME"),
                     "sha256": image_hash,
                 }
-            ],
-            "device_packages": getenv("DEVICE_PACKAGES").split(),
-            "supported_devices": getenv("SUPPORTED_DEVICES").split(),
-            "titles": get_titles(),
+            ]
         }
     },
 }

+profile = image_info["profiles"][device_id]
+
+set(profile, "titles", get_titles())
+set(profile, "device_packages", getenv("DEVICE_PACKAGES", "").split())
+set(profile, "supported_devices", getenv("SUPPORTED_DEVICES", "").split())
+set(profile, "image_prefix", getenv("IMAGE_PREFIX"))
+
 json_path.write_text(json.dumps(image_info, separators=(",", ":")))
diff --git a/scripts/json_overview_image_info.py b/scripts/json_overview_image_info.py
index ca6fbbc962..7741154f61 100755
--- a/scripts/json_overview_image_info.py
+++ b/scripts/json_overview_image_info.py
@@ -14,23 +14,33 @@ output_path = Path(argv[1])

 assert getenv("WORK_DIR"), "$WORK_DIR required"

-work_dir = Path(getenv("WORK_DIR"))
-
-output = {}
-
-for json_file in work_dir.glob("*.json"):
-    image_info = json.loads(json_file.read_text())
-    if not output:
-        output.update(image_info)
-    else:
-        # get first (and only) profile in json file
-        device_id = next(iter(image_info["profiles"].keys()))
-        if device_id not in output["profiles"]:
-            output["profiles"].update(image_info["profiles"])
-        else:
-            output["profiles"][device_id]["images"].append(
-                image_info["profiles"][device_id]["images"][0]
-            )
+# merge profiles
+def get_output(work_dir):
+    output = None
+
+    for json_file in work_dir.glob("*.json"):
+        image_info = json.loads(json_file.read_text())
+
+        # use first json file as template
+        if not output:
+            output = image_info
+
+        # get first (and probably only) profile in json file
+        for device_id, profile in image_info["profiles"].items():
+            output["profiles"][device_id] = {
+                **profile,
+                **output["profiles"].get(device_id, {}),
+            }
+            output["profiles"][device_id]["images"].extend(profile["images"])
+
+    # make image lists unique by name
+    for device_id, profile in output["profiles"].items():
+        profile["images"] = list({e["name"]: e for e in profile["images"]}.values())
+
+    return output
+
+
+output = get_output(Path(getenv("WORK_DIR")))

 if output:
     default_packages, output["arch_packages"] = run(
--
2.30.0




More information about the openwrt-devel mailing list