munyal-client/munyal/compare_json.py

77 lines
2.3 KiB
Python
Raw Normal View History

2019-07-28 16:18:49 +00:00
import json
2020-12-19 22:48:55 +00:00
from munyal.dir_to_json import get_json
from munyal.flatten_dirs import flatten_dirs
2019-07-28 16:18:49 +00:00
2019-11-27 23:24:04 +00:00
def compare_json(json_1, json_2):
json_1 = flatten_dirs(json_1)
json_2 = flatten_dirs(json_2)
items_1 = set(json_1)
items_2 = set(json_2)
delete = items_1 - items_2
add = items_2 - items_1
update = items_1 & items_2
add_files = []
for key, value in json_2.items():
if key in add:
add_files.append(value)
delete_files = []
for key, value in json_1.items():
if key in delete:
delete_files.append(value)
for name in update:
if not json_1[name]["is_file"] or not json_2[name]["is_file"]:
continue
if json_1[name]["checksum"] != json_2[name]["checksum"]:
delete_files.append(json_2[name])
add_files.append(json_1[name])
return list(delete_files), list(add_files)
def compare_json_old(json1, json2):
2019-07-28 16:18:49 +00:00
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__":
2020-12-08 20:45:42 +00:00
import sys
folder_1 = sys.argv[1]
folder_2 = sys.argv[2]
2019-07-28 16:18:49 +00:00
try:
2020-12-08 20:45:42 +00:00
json1 = get_json(folder_1)
json2 = get_json(folder_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))