Skip to content

Commit

Permalink
A generic JSON file updater, which can add/update-existing attributes. (
Browse files Browse the repository at this point in the history
sonic-net#770)

* A generic JSON file updater, which can add/update-existing attributes.
This tool would be used to update /etc/sonic/core_analyzer.rc.json file to add
credentials by HW proxy.

* Updated per review comments.
The option is better named.
  • Loading branch information
renukamanavalan committed Dec 17, 2019
1 parent d803156 commit 2d55a50
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions scripts/update_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#! /usr/bin/env python

import os
import sys
import json
import argparse

TMP_SUFFIX = ".tmp"
BAK_SUFFIX = ".bak"

def dict_update(dst, patch):
for k in patch.keys():
if type(patch[k]) == dict:
dst[k] = dict_update(dst[k], patch[k])
else:
dst[k] = patch[k]
return dst

def do_update(rcf, patchf):
dst = {}
patch = {}

tmpf = rcf + TMP_SUFFIX
bakf = rcf + BAK_SUFFIX

with open(rcf, "r") as f:
dst = json.load(f)

with open(patchf, "r") as f:
patch = json.load(f)

dst = dict_update(dst, patch)

with open(tmpf, "w") as f:
json.dump(dst, f, indent = 4)

os.rename(rcf, bakf)
os.rename(tmpf, rcf)


def main():
parser = argparse.ArgumentParser(description="Update JSON based file")
parser.add_argument("-u", "--update", help="JSON file to be updated")
parser.add_argument("-p", "--patch", help="JSON file holding patch")
args = parser.parse_args()

if not args.update or not args.patch:
raise Exception("check usage")

do_update(args.update, args.patch)

if __name__ == '__main__':
main()


1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
'scripts/route_check_test.sh',
'scripts/sfpshow',
'scripts/teamshow',
'scripts/update_json.py',
'scripts/warm-reboot',
'scripts/watermarkstat',
'scripts/watermarkcfg'
Expand Down

0 comments on commit 2d55a50

Please sign in to comment.