X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=csit%2Flibraries%2Fbackuprestore%2FJsonDiffTool.py;h=93f921e0fdafd100b93ffa265aa5694cdb34515b;hb=1c106006ffb33c1c60e2f8abe61e88a7bd95a7bd;hp=b60126ac8e532371ab77a3de41b18eb85c7ffc30;hpb=e235045c2ad12d41416e2583609a0178c8bd9287;p=integration%2Ftest.git diff --git a/csit/libraries/backuprestore/JsonDiffTool.py b/csit/libraries/backuprestore/JsonDiffTool.py index b60126ac8e..93f921e0fd 100644 --- a/csit/libraries/backuprestore/JsonDiffTool.py +++ b/csit/libraries/backuprestore/JsonDiffTool.py @@ -32,32 +32,32 @@ def from_path_to_jsonpatch(matchedpath): :return: the corresponding json patch for removing the fragment """ - logging.info('starting. filter path: %s', matchedpath) + logging.info("starting. filter path: %s", matchedpath) # First step: path format change # typical input: $['ietf-yang-library:modules-state']['module'][57] # desired output: /ietf-yang-library:modules-state/module/57 - matchedpath = matchedpath.replace('$.', '/') - matchedpath = matchedpath.replace('$[\'', '/') - matchedpath = matchedpath.replace('\'][\'', '/') - matchedpath = matchedpath.replace('\']', '/') + matchedpath = matchedpath.replace("$.", "/") + matchedpath = matchedpath.replace("$['", "/") + matchedpath = matchedpath.replace("']['", "/") + matchedpath = matchedpath.replace("']", "/") # this one is for the $[2] pattern - if '$[' in matchedpath and ']' in matchedpath: - matchedpath = matchedpath.replace('$[', '/') - matchedpath = matchedpath.replace(']', '') + if "$[" in matchedpath and "]" in matchedpath: + matchedpath = matchedpath.replace("$[", "/") + matchedpath = matchedpath.replace("]", "") - matchedpath = matchedpath.replace('[', '') - matchedpath = matchedpath.replace(']', '') - matchedpath = matchedpath.rstrip('/') + matchedpath = matchedpath.replace("[", "") + matchedpath = matchedpath.replace("]", "") + matchedpath = matchedpath.rstrip("/") # Now, for input: /ietf-yang-library:modules-state/module/57 # desired output: [{"op":"remove","path":"/ietf-yang-library:modules-state/module/57"}] - logging.info('final filter path: %s', matchedpath) - as_patch = '[{{"op\":\"remove\",\"path\":\"{0}\"}}]'.format(matchedpath) - logging.info('generated patch line: %s', as_patch) + logging.info("final filter path: %s", matchedpath) + as_patch = '[{{"op":"remove","path":"{0}"}}]'.format(matchedpath) + logging.info("generated patch line: %s", as_patch) return as_patch @@ -70,20 +70,23 @@ def apply_filter(json_arg, filtering_line): :return: the filtered document """ - logging.info('apply_filter:starting. jsonPath filter=[%s]', filtering_line) + logging.info("apply_filter:starting. jsonPath filter=[%s]", filtering_line) - res = jsonpath(json_arg, filtering_line, result_type='PATH') + res = jsonpath(json_arg, filtering_line, result_type="PATH") if isinstance(res, types.BooleanType) or len(res) == 0: - logging.info('apply_filter: The prefilter [%s] matched nothing', filtering_line) + logging.info("apply_filter: The prefilter [%s] matched nothing", filtering_line) return json_arg if len(res) > 1: - raise AssertionError('Bad pre-filter [%s] (returned [%d] entries, should return one at most', - filtering_line, len(res)) + raise AssertionError( + "Bad pre-filter [%s] (returned [%d] entries, should return one at most", + filtering_line, + len(res), + ) as_json_patch = from_path_to_jsonpatch(res[0]) - logging.info('apply_filter: applying patch! resolved patch =%s', as_json_patch) + logging.info("apply_filter: applying patch! resolved patch =%s", as_json_patch) patched_json = jsonpatch.apply_patch(json_arg, as_json_patch) - logging.info('apply_filter: json after patching: %s', patched_json) + logging.info("apply_filter: json after patching: %s", patched_json) return patched_json @@ -97,15 +100,15 @@ def prefilter(json_arg, initial_prefilter): """ if not initial_prefilter: - logging.info('prefilter not found!') + logging.info("prefilter not found!") # whether it is filtered or not, return as json so it can be handled uniformly from now on return json.loads(json_arg) with open(initial_prefilter) as f: lines = f.read().splitlines() - logging.info('prefilter:lines in prefilter file: %d ', len(lines)) - lines = filter(lambda k: not k.startswith('#'), lines) - logging.info('prefilter:lines after removing comments: %d ', len(lines)) + logging.info("prefilter:lines in prefilter file: %d ", len(lines)) + lines = filter(lambda k: not k.startswith("#"), lines) + logging.info("prefilter:lines after removing comments: %d ", len(lines)) json_args_as_json = json.loads(json_arg) for filtering_line in lines: json_args_as_json = apply_filter(json_args_as_json, filtering_line) @@ -123,21 +126,29 @@ def prefilter_json_files_then_compare(args): requested) """ - logging.info('prefilter_json_files_then_compare: starting!') + logging.info("prefilter_json_files_then_compare: starting!") with open(args.initialFile) as f: json_initial = file.read(f) with open(args.finalFile) as f2: json_final = file.read(f2) patch = jsonpatch.JsonPatch.from_diff(json_initial, json_final) - logging.info('prefilter_json_files_then_compare:differences before patching: %d', len(list(patch))) + logging.info( + "prefilter_json_files_then_compare:differences before patching: %d", + len(list(patch)), + ) json_initial_filtered = prefilter(json_initial, args.initial_prefilter) json_final_filtered = prefilter(json_final, args.finalPreFilter) - patch_after_filtering = jsonpatch.JsonPatch.from_diff(json_initial_filtered, json_final_filtered) + patch_after_filtering = jsonpatch.JsonPatch.from_diff( + json_initial_filtered, json_final_filtered + ) differences_after_patching = list(patch_after_filtering) - logging.info('prefilter_json_files_then_compare: differences after patching: %d', len(differences_after_patching)) + logging.info( + "prefilter_json_files_then_compare: differences after patching: %d", + len(differences_after_patching), + ) if args.printDifferences: for patchline in differences_after_patching: @@ -148,46 +159,89 @@ def prefilter_json_files_then_compare(args): def Json_Diff_Check_Keyword(json_before, json_after, filter_before, filter_after): - input_argv = ['-i', json_before, '-f', json_after, '-ipf', filter_before, '-fpf', filter_after, '-pd'] + input_argv = [ + "-i", + json_before, + "-f", + json_after, + "-ipf", + filter_before, + "-fpf", + filter_after, + "-pd", + ] sys.argv[1:] = input_argv - logging.info('starting. constructed command line: %s', sys.argv) + logging.info("starting. constructed command line: %s", sys.argv) return Json_Diff_Check() def parse_args(args): - parser = argparse.ArgumentParser(description='both initial and final json files are compared for differences. ' - 'The program returns 0 when the json contents are the same, or the ' - 'number of' - ' differences otherwise. Both json files can be prefiltered for ' - 'certain patterns' - ' before checking the differences') - - parser.add_argument('-i', '--initialFile', required='true', dest='initialFile', action='store', - help='initial json file') - parser.add_argument('-f', '--finalFile', required='true', dest='finalFile', action='store', help='final json file') - parser.add_argument('-ipf', '--initial_prefilter', dest='initial_prefilter', - help='File with pre-filtering patterns to apply to the initial json file before comparing') - parser.add_argument('-fpf', '--finalPreFilter', dest='finalPreFilter', - help='File with pre-filtering patterns to apply to the final json file before comparing') - parser.add_argument('-pd', '--printDifferences', action='store_true', - help='on differences found, prints the list of paths for the found differences before exitting') - parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='generate log information') + parser = argparse.ArgumentParser( + description="both initial and final json files are compared for differences. " + "The program returns 0 when the json contents are the same, or the " + "number of" + " differences otherwise. Both json files can be prefiltered for " + "certain patterns" + " before checking the differences" + ) + + parser.add_argument( + "-i", + "--initialFile", + required="true", + dest="initialFile", + action="store", + help="initial json file", + ) + parser.add_argument( + "-f", + "--finalFile", + required="true", + dest="finalFile", + action="store", + help="final json file", + ) + parser.add_argument( + "-ipf", + "--initial_prefilter", + dest="initial_prefilter", + help="File with pre-filtering patterns to apply to the initial json file before comparing", + ) + parser.add_argument( + "-fpf", + "--finalPreFilter", + dest="finalPreFilter", + help="File with pre-filtering patterns to apply to the final json file before comparing", + ) + parser.add_argument( + "-pd", + "--printDifferences", + action="store_true", + help="on differences found, prints the list of paths for the found differences before exitting", + ) + parser.add_argument( + "-v", + "--verbose", + dest="verbose", + action="store_true", + help="generate log information", + ) return parser.parse_args(args) def Json_Diff_Check(): args = parse_args(sys.argv[1:]) - if hasattr(args, 'verbose'): + if hasattr(args, "verbose"): if args.verbose: logging.basicConfig(level=logging.DEBUG) if args.printDifferences: - logging.info('(will print differences)') + logging.info("(will print differences)") result = prefilter_json_files_then_compare(args) return result -if __name__ == '__main__': +if __name__ == "__main__": Json_Diff_Check()