77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import json
|
|
|
|
from dir_to_json import get_json
|
|
from flatten_dirs import flatten_dirs
|
|
|
|
|
|
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):
|
|
items1 = list(enumerate(json1))
|
|
items2 = list(enumerate(json2))
|
|
for i, item1 in items1:
|
|
for j, item2 in items2:
|
|
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
|
|
json1 = list(filter(None, json1))
|
|
json2 = list(filter(None, json2))
|
|
return json1, json2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
folder_1 = sys.argv[1]
|
|
folder_2 = sys.argv[2]
|
|
try:
|
|
json1 = get_json(folder_1)
|
|
json2 = get_json(folder_2)
|
|
except Exception:
|
|
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))
|