Add json merger tool
Add a simple script to merge two json files. It's necessary for creating
the sp_layout.json required by TF-A for using FIP SPs.
Signed-off-by: Balint Dobszay <balint.dobszay@arm.com>
Change-Id: I944770d263921efcbb12217db3df5170c59f8fec
diff --git a/tools/python/merge_json.py b/tools/python/merge_json.py
new file mode 100644
index 0000000..68ad10b
--- /dev/null
+++ b/tools/python/merge_json.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Copyright (c) 2022, Arm Limited. All rights reserved.
+
+"""
+Merge two json files, the second is merged into the first. If the first file
+doesn't exists yet, it will be created, along with its parent directories.
+"""
+
+import json
+import os.path
+import sys
+
+if os.path.isfile(sys.argv[1]):
+ with open(sys.argv[1], "rt", encoding="ascii") as f:
+ combined = json.load(f)
+else:
+ os.makedirs(os.path.dirname(sys.argv[1]), exist_ok=True)
+ combined = {}
+
+with open(sys.argv[2], "rt", encoding="ascii") as f:
+ current = json.load(f)
+ combined = {**combined, **current}
+
+with open(sys.argv[1], "wt", encoding="ascii") as f:
+ json.dump(combined, f, indent=4)