reformated code

master
kirbylife 2019-11-27 17:24:04 -06:00
parent 0e6dde68c6
commit fa8590e197
2 changed files with 31 additions and 33 deletions

View File

@ -5,42 +5,38 @@ import json
from dir_to_json import get_json from dir_to_json import get_json
def compare_json(json1, json2): def compare_json(json1, json2):
bckup1, bckup2 = json1[:], json2[:]
items1 = list(enumerate(json1)) items1 = list(enumerate(json1))
items2 = list(enumerate(json2)) items2 = list(enumerate(json2))
for i, item1 in items1: for i, item1 in items1:
for j, item2 in items2: for j, item2 in items2:
if item1["name"] == item2["name"]: if item1["name"] == item2["name"]:
if item1["is_file"] == True == item2["is_file"]: if all([item1["is_file"], item2["is_file"]]):
if item1["checksum"] == item2["checksum"]: if item1["checksum"] == item2["checksum"]:
json1[i] = None json1[i] = None
json2[j] = None json2[j] = None
''' elif all([not item1["is_file"], not item2["is_file"]]):
else: new_json1, new_json2 = compare_json(
json1[i]["tag"] = "update" item1["content"], item2["content"])
json2[j] = None if len(new_json1) == 0:
''' json1[i] = None
elif item1["is_file"] == False == item2["is_file"]: else:
new_json1, new_json2 = compare_json(item1["content"], item2["content"]) json1[i]["content"] = new_json1
if len(new_json1) == 0: if len(new_json2) == 0:
json1[i] = None json2[j] = None
else: else:
json1[i]["content"] = new_json1 json2[j]["content"] = new_json2
if len(new_json2) == 0:
json2[j] = None
else:
json2[j]["content"] = new_json2
elif item1["is_file"] != item2["is_file"]:##### Caso hipotetico imposible #####
json1[i]["tag"] == "delete"
json1 = list(filter(None, json1)) json1 = list(filter(None, json1))
json2 = list(filter(None, json2)) json2 = list(filter(None, json2))
return json1, json2 return json1, json2
if __name__ == "__main__": if __name__ == "__main__":
try: try:
json1 = get_json("/home/kirbylife/Proyectos/munyal_test/original") json1 = get_json("/home/kirbylife/Proyectos/munyal_test/original")
json2 = get_json("/home/kirbylife/Proyectos/munyal_test/copy") json2 = get_json("/home/kirbylife/Proyectos/munyal_test/copy")
except: except Exception:
print("error outside") print("error outside")
json1, json2 = compare_json(json1, json2) json1, json2 = compare_json(json1, json2)
#print(len(json1), len(json2)) #print(len(json1), len(json2))

View File

@ -1,10 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from hashlib import md5
import os
import json import json
import os
from hashlib import md5
def md5sum(filename): def md5sum(filename):
@ -14,18 +13,17 @@ def md5sum(filename):
for chunk in iter(lambda: f.read(128 * hash.block_size), b""): for chunk in iter(lambda: f.read(128 * hash.block_size), b""):
hash.update(chunk) hash.update(chunk)
return hash.hexdigest() return hash.hexdigest()
except: except Exception:
return None return None
def get_json(path): def get_json(path):
out = [] out = []
items = os.listdir(path) items = os.listdir(path)
try: try:
for item in items: for item in items:
if item[0] != ".": if item[0] != ".":
item_json = { item_json = {"name": item}
"name": item
}
route = os.path.join(path, item) route = os.path.join(path, item)
if os.path.isdir(route): if os.path.isdir(route):
item_json["is_file"] = False item_json["is_file"] = False
@ -40,11 +38,15 @@ def get_json(path):
item_json["checksum"] = checksum item_json["checksum"] = checksum
else: else:
item = None item = None
else:
continue
out.append(item_json) out.append(item_json)
except: except Exception:
return get_json(path) return get_json(path)
return out return out
if __name__ == "__main__": if __name__ == "__main__":
output = get_json("/media/kirbylife/DATOS/Proyectos/PyCharmProjects/Munyal/folder_test") output = get_json(
"/media/kirbylife/DATOS/Proyectos/PyCharmProjects/Munyal/folder_test")
print(json.dumps(output, indent=4)) print(json.dumps(output, indent=4))