File size: 1,938 Bytes
0a2c21d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import yaml
def compare_yaml(file1_path, file2_path):
with open(file1_path, "r") as f1:
yaml1 = yaml.safe_load(f1)
with open(file2_path, "r") as f2:
yaml2 = yaml.safe_load(f2)
missing_keys = []
different_values = []
compare_dict(yaml1, yaml2, "", missing_keys, different_values)
print("=" * 60)
print("Missing Keys")
print("=" * 60)
if missing_keys:
for diff in missing_keys:
print(diff)
else:
print("None")
print("\n" + "=" * 60)
print("Different Values")
print("=" * 60)
if different_values:
for diff in different_values:
print(diff)
else:
print("None")
print("\n" + "=" * 60)
print(f"Total: {len(missing_keys)} missing keys, {len(different_values)} different values")
print("=" * 60)
def compare_dict(dict1, dict2, path, missing_keys, different_values):
all_keys = set(dict1.keys()) | set(dict2.keys())
for key in all_keys:
current_path = f"{path}.{key}" if path else key
if key not in dict2:
missing_keys.append(f"[{current_path}] Only in file1: {dict1[key]}")
elif key not in dict1:
missing_keys.append(f"[{current_path}] Only in file2: {dict2[key]}")
else:
val1, val2 = dict1[key], dict2[key]
if isinstance(val1, dict) and isinstance(val2, dict):
compare_dict(val1, val2, current_path, missing_keys, different_values)
elif isinstance(val1, list) and isinstance(val2, list):
if val1 != val2:
different_values.append(f"[{current_path}]\n File1: {val1}\n File2: {val2}")
elif val1 != val2:
different_values.append(f"[{current_path}]\n File1: {val1}\n File2: {val2}")
if __name__ == "__main__":
compare_yaml(
"configs/stage_1_init.yaml",
"configs/stage_1_post.yaml",
)
|