90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
import ruamel.yaml
|
|
import os
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--file1', default='development.yml')
|
|
parser.add_argument('--file2', default='test.yml')
|
|
|
|
|
|
def findDiff(d1, d2, path=""):
|
|
for k in d1.keys():
|
|
if not (k in d2):
|
|
print(path, ":")
|
|
print(k + " as key not in d2", "\n")
|
|
else:
|
|
if type(d1[k]) is dict:
|
|
if path == "":
|
|
path = k
|
|
else:
|
|
path = path + "->" + k
|
|
findDiff(d1[k], d2[k], path)
|
|
else:
|
|
if d1[k] != d2[k]:
|
|
print(path, ":")
|
|
print(" - ", k, " : ", d1[k])
|
|
print(" + ", k, " : ", d2[k])
|
|
|
|
|
|
def compare_dictionaries(dict_1, dict_2, dict_1_name, dict_2_name, path=""):
|
|
"""Compare two dictionaries recursively to find non mathcing elements
|
|
|
|
Args:
|
|
dict_1: dictionary 1
|
|
dict_2: dictionary 2
|
|
|
|
Returns:
|
|
|
|
"""
|
|
err = ''
|
|
key_err = ''
|
|
value_err = ''
|
|
old_path = path
|
|
for k in dict_1.keys():
|
|
path = old_path + "[%s]" % k
|
|
if not (k in dict_2):
|
|
key_err += "Key %s%s not in %s\n" % (
|
|
dict_2_name, path, dict_2_name)
|
|
else:
|
|
if isinstance(dict_1[k], dict) and isinstance(dict_2[k], dict):
|
|
err += compare_dictionaries(
|
|
dict_1[k], dict_2[k], 'd1', 'd2', path)
|
|
else:
|
|
if dict_1[k] != dict_2[k]:
|
|
value_err += "Value of %s%s (%s) not same as %s%s (%s)\n"\
|
|
% (dict_1_name,
|
|
path, dict_1[k], dict_2_name, path, dict_2[k])
|
|
|
|
for k in dict_2.keys():
|
|
path = old_path + "[%s]" % k
|
|
if not (k in dict_1):
|
|
key_err += "Key %s%s not in %s\n" % (
|
|
dict_2_name, path, dict_1_name)
|
|
|
|
return key_err + value_err + err
|
|
|
|
|
|
def create_path(path_str, delimiter="/"):
|
|
path = ''
|
|
|
|
for d in path_str.split(delimiter):
|
|
path = os.path.join(path, d)
|
|
|
|
return path
|
|
|
|
|
|
def main():
|
|
path_meta = create_path("../../../meta/project")
|
|
|
|
args = parser.parse_args()
|
|
|
|
path_main = os.path.join(path_meta, args.file1)
|
|
path_test = os.path.join(path_meta, args.file2)
|
|
|
|
yaml_main = ruamel.yaml.load(file(path_main, 'r'))
|
|
yaml_test = ruamel.yaml.load(file(path_test, 'r'))
|
|
|
|
a = compare_dictionaries(yaml_main, yaml_test, 'main', 'test')
|
|
print(a)
|
|
|
|
main()
|