[gnome-runtime-images/abderrahim/json-glib] rewrite-flatpak-manifest: use json-glib instead of python's json



commit c7f2c69a80910f5cb30595b1fd446756a8dbc78e
Author: Abderrahim Kitouni <akitouni gnome org>
Date:   Wed Apr 29 14:30:11 2020 +0100

    rewrite-flatpak-manifest: use json-glib instead of python's json
    
    it implements some extensions over json that flatpak manifests use

 base                     |  3 +-
 rewrite-flatpak-manifest | 71 ++++++++++++++++++++++++++++++++----------------
 2 files changed, 50 insertions(+), 24 deletions(-)
---
diff --git a/base b/base
index b8985e9..a74009e 100644
--- a/base
+++ b/base
@@ -24,7 +24,8 @@ RUN cat /dev/urandom | tr -dc a-f0-9 | head -c32 > /etc/machine-id && echo "" >>
 
 RUN dnf -y update && \
     dnf install -y flatpak flatpak-builder librsvg2 ostree fuse elfutils \
-    dconf dbus-daemon git bzr xorg-x11-server-Xvfb dbus-x11 python3-ruamel-yaml && \
+    dconf dbus-daemon git bzr xorg-x11-server-Xvfb dbus-x11 python3-ruamel-yaml \
+    python3-gobject json-glib && \
     dnf clean all
 
 COPY rewrite-flatpak-manifest /usr/local/bin/rewrite-flatpak-manifest
diff --git a/rewrite-flatpak-manifest b/rewrite-flatpak-manifest
index 4783b71..2d0e53e 100755
--- a/rewrite-flatpak-manifest
+++ b/rewrite-flatpak-manifest
@@ -3,27 +3,59 @@
 # The latest version of this script can be found at
 # https://gitlab.gnome.org/GNOME/gnome-runtime-images
 
-import json
+import gi
+gi.require_version('Json', '1.0')
+
+from gi.repository import Json
 from ruamel import yaml
 import os, sys
 
 from contextlib import contextmanager
 
-@contextmanager
-def rewrite_manifest(filename):
+def rewrite_json_manifest(filename):
+    with open(filename) as f:
+        data = Json.from_string(f.read())
+
+    mods = data.get_object().get_array_member('modules')
+    for i in range(mods.get_length()):
+        mod = mods.get_element(i)
+        if mod.get_node_type() != Json.NodeType.OBJECT:
+            continue
+        mod = mod.get_object()
+
+        if mod.get_string_member('name') == modulename:
+            sources = mod.get_array_member('sources')
+            new_sources = Json.Array()
+            for i in range(sources.get_length()):
+                if sources.get_object_element(i).get_string_member('type') != 'git':
+                    new_sources.add_object_element(sources.get_object_element(i))
+                    continue
+
+                path = os.path.relpath('.', os.path.dirname(manifestfile))
+                new_sources.add_element(Json.from_string('{"type": "dir", "path": "%s"}' % path))
+
+            mod.set_array_member('sources', new_sources)
+
+    with open(filename, 'w') as f:
+        f.write(Json.to_string(data, True))
+
+def rewrite_yaml_manifest(filename):
     with open(filename) as f:
-        if filename.endswith('.json'):
-            data = json.load(f)
-        else:
-            data = yaml.round_trip_load(f, preserve_quotes=True)
+        data = yaml.round_trip_load(f, preserve_quotes=True)
+
+    for mod in data['modules']:
+        if not isinstance(mod, dict):
+            continue
 
-    yield data
+        if mod['name'] == modulename:
+            for i in range(len(mod['sources'])):
+                if mod['sources'][i]['type'] != 'git':
+                    continue
+                path = os.path.relpath('.', os.path.dirname(manifestfile))
+                mod['sources'][i] = {'type': 'dir', 'path': path}
 
     with open(filename, 'w') as f:
-        if filename.endswith('.json'):
-            json.dump(data, f, indent=4, separators=(',', ' : '))
-        else:
-            yaml.round_trip_dump(data, f)
+        yaml.round_trip_dump(data, f)
 
 if __name__ == '__main__':
     try:
@@ -32,14 +64,7 @@ if __name__ == '__main__':
         print("usage: {} flatpak_manifest module_name".format(sys.argv[0]))
         sys.exit(1)
 
-    with rewrite_manifest(manifestfile) as data:
-        for mod in data['modules']:
-            if not isinstance(mod, dict):
-                continue
-
-            if mod['name'] == modulename:
-                for i in range(0, len(mod['sources'])):
-                    if not mod['sources'][i]['type'] == 'git':
-                        continue
-                    path = os.path.relpath('.', os.path.dirname(manifestfile))
-                    mod['sources'][i] = {'type': 'dir', 'path': path}
+    if manifestfile.endswith('.json'):
+        rewrite_json_manifest(manifestfile)
+    else:
+        rewrite_yaml_manifest(manifestfile)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]