From cbaffc8a5d16b9540ab9dbdbc6c2f7eeb39ad005 Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Fri, 18 Jan 2019 10:08:38 -0800 Subject: [PATCH 1/3] Add utility to compare JSON from two database's. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- utils/check_json.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 utils/check_json.sh diff --git a/utils/check_json.sh b/utils/check_json.sh new file mode 100755 index 00000000..4378a4f2 --- /dev/null +++ b/utils/check_json.sh @@ -0,0 +1,26 @@ +#!/bin/bash -e + +# check_json.sh +# +# Tool for comparing database JSON outputs from two database's. + +DIR_A=$1 +DIR_B=$2 + +for A_JSON_IN in $( ls ${DIR_A}/*.json ); do + A_JSON_OUT="$(mktemp)_a" + B_JSON_OUT="$(mktemp)_b" + + B_JSON_IN="${DIR_B}/$(basename ${A_JSON_IN})" + + if [ ! -f "${B_JSON_IN}" ]; then + echo "${B_JSON_IN} not found!" + continue + fi + + python3 -m utils.xjson ${A_JSON_IN} > ${A_JSON_OUT} + python3 -m utils.xjson ${B_JSON_IN} > ${B_JSON_OUT} + + echo "Comparing $(basename ${A_JSON_IN})" + diff -U 3 ${A_JSON_OUT} ${B_JSON_OUT} || true +done From 5817e5932ca25dcdef5ae5342b3bbbf1a5785a06 Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Fri, 18 Jan 2019 10:10:14 -0800 Subject: [PATCH 2/3] Rename script. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- utils/{check_json.sh => diff_db_json.sh} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename utils/{check_json.sh => diff_db_json.sh} (92%) diff --git a/utils/check_json.sh b/utils/diff_db_json.sh similarity index 92% rename from utils/check_json.sh rename to utils/diff_db_json.sh index 4378a4f2..717db6e7 100755 --- a/utils/check_json.sh +++ b/utils/diff_db_json.sh @@ -1,6 +1,6 @@ #!/bin/bash -e -# check_json.sh +# diff_db_json.sh # # Tool for comparing database JSON outputs from two database's. From 782d2c4ced761f097f2d8bb6d430bb8c685e9f4d Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Wed, 23 Jan 2019 13:09:19 -0800 Subject: [PATCH 3/3] Convert shell script to python script. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- utils/diff_db_json.py | 44 +++++++++++++++++++++++++++++++++++++++++++ utils/diff_db_json.sh | 26 ------------------------- 2 files changed, 44 insertions(+), 26 deletions(-) create mode 100755 utils/diff_db_json.py delete mode 100755 utils/diff_db_json.sh diff --git a/utils/diff_db_json.py b/utils/diff_db_json.py new file mode 100755 index 00000000..a4337979 --- /dev/null +++ b/utils/diff_db_json.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +import argparse +import subprocess +import tempfile +import os.path +import glob + + +def main(): + parser = argparse.ArgumentParser( + description= + "Tool for comparing database JSON outputs from two database's.") + parser.add_argument('a_db') + parser.add_argument('b_db') + + args = parser.parse_args() + + assert os.path.isdir(args.a_db) + assert os.path.isdir(args.b_db) + + for a_json_in in glob.glob('{}/*.json'.format(args.a_db)): + a_json_base = os.path.basename(a_json_in) + b_json_in = '{}/{}'.format(args.b_db, a_json_base) + + if not os.path.exists(b_json_in): + print('{} not found!'.format(b_json_in)) + continue + + with tempfile.NamedTemporaryFile(suffix="_a_{}".format( + a_json_base)) as a_json_out, tempfile.NamedTemporaryFile( + suffix="_b_{}".format(a_json_base)) as b_json_out: + subprocess.check_call( + "python3 -m utils.xjson {}".format(a_json_in), shell=True, stdout=a_json_out) + subprocess.check_call( + "python3 -m utils.xjson {}".format(b_json_in), shell=True, stdout=b_json_out) + + print("Comparing {}".format(a_json_base)) + subprocess.call( + "diff -U 3 {} {}".format(a_json_out.name, b_json_out.name), + shell=True) + + +if __name__ == "__main__": + main() diff --git a/utils/diff_db_json.sh b/utils/diff_db_json.sh deleted file mode 100755 index 717db6e7..00000000 --- a/utils/diff_db_json.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -e - -# diff_db_json.sh -# -# Tool for comparing database JSON outputs from two database's. - -DIR_A=$1 -DIR_B=$2 - -for A_JSON_IN in $( ls ${DIR_A}/*.json ); do - A_JSON_OUT="$(mktemp)_a" - B_JSON_OUT="$(mktemp)_b" - - B_JSON_IN="${DIR_B}/$(basename ${A_JSON_IN})" - - if [ ! -f "${B_JSON_IN}" ]; then - echo "${B_JSON_IN} not found!" - continue - fi - - python3 -m utils.xjson ${A_JSON_IN} > ${A_JSON_OUT} - python3 -m utils.xjson ${B_JSON_IN} > ${B_JSON_OUT} - - echo "Comparing $(basename ${A_JSON_IN})" - diff -U 3 ${A_JSON_OUT} ${B_JSON_OUT} || true -done