forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A generic JSON file updater, which can add/update-existing attributes. (
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
1 parent
d803156
commit 2d55a50
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters