munyal-client/compare_json.py

43 lines
1.4 KiB
Python
Raw Normal View History

2019-07-28 16:18:49 +00:00
import json
from dir_to_json import get_json
2019-11-27 23:24:04 +00:00
2019-07-28 16:18:49 +00:00
def compare_json(json1, json2):
items1 = list(enumerate(json1))
items2 = list(enumerate(json2))
for i, item1 in items1:
for j, item2 in items2:
2019-11-27 23:24:04 +00:00
if item1["name"] == item2["name"]:
if all([item1["is_file"], item2["is_file"]]):
if item1["checksum"] == item2["checksum"]:
json1[i] = None
json2[j] = None
elif all([not item1["is_file"], not item2["is_file"]]):
new_json1, new_json2 = compare_json(
item1["content"], item2["content"])
if len(new_json1) == 0:
json1[i] = None
else:
json1[i]["content"] = new_json1
if len(new_json2) == 0:
json2[j] = None
else:
json2[j]["content"] = new_json2
2019-07-28 16:18:49 +00:00
json1 = list(filter(None, json1))
json2 = list(filter(None, json2))
return json1, json2
2019-11-27 23:24:04 +00:00
2019-07-28 16:18:49 +00:00
if __name__ == "__main__":
try:
2019-11-29 23:19:25 +00:00
json1 = get_json("/home/kirbylife/Munyal/test_1")
json2 = get_json("/home/kirbylife/Munyal/test_2")
2019-11-27 23:24:04 +00:00
except Exception:
2019-07-28 16:18:49 +00:00
print("error outside")
json1, json2 = compare_json(json1, json2)
#print(len(json1), len(json2))
print(json.dumps(json1, indent=4))
print("\n============\n")
print(json.dumps(json2, indent=4))