now remove files from subdirectories works correctly and removed test files

master
kirbylife 2020-12-14 20:24:46 -06:00
parent 911652fa2b
commit 3f0ee4af7e
8 changed files with 68 additions and 222 deletions

View File

@ -158,14 +158,12 @@ class MunyalClient(Icon):
if delete:
print("Cosas eliminadas:")
print(delete)
delete = flatten_dirs(delete, action="delete")
for f in delete:
f["action"] = "delete"
self.stack.extend(delete)
if add:
print("Cosas agregadas:")
print(add)
add = flatten_dirs(add, action="add")
for f in add:
f["action"] = "add"
self.stack.extend(add)

View File

@ -1,9 +1,40 @@
import json
from dir_to_json import get_json
from misc import flatten_dirs
def compare_json(json1, json2):
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:

View File

@ -1,33 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from copy import deepcopy
from time import sleep
from compare_json import compare_json
from dir_to_json import get_json
ORIGINAL = "/home/kirbylife/Proyectos/munyal_test/original"
def main(args):
actual = get_json(ORIGINAL)
new = deepcopy(actual)
while True:
delete, add = compare_json(deepcopy(actual), deepcopy(new))
for item in delete:
if item.get("tag"):
if item.get("tag") == "update":
print("Actualizado el archivo {}".format(item.get("name")))
else:
print("borrado el archivo {}".format(item.get("name")))
for item in add:
print("Agregado el archivo {}".format(item.get("name")))
actual = deepcopy(new)
new = get_json(ORIGINAL)
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))

View File

@ -1,67 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
from hashlib import md5
from random import randint
from time import time
import rethinkdb as r
from flask import Flask, jsonify, request
app = Flask(__name__)
def md5sum(filename):
try:
hash = md5()
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(128 * hash.block_size), b""):
hash.update(chunk)
return hash.hexdigest()
except:
return None
@app.route("/", methods=["GET"])
def index():
return ('''
<html>
<head>
<title>Munyal API</title>
</head>
<body>
<h1>Munyal private API</h1>
</body>
</html>
''')
@app.route("/upload", methods=["POST"])
def upload():
try:
r.connect("localhost", 28015).repl()
cursor = r.table("changes")
host = request.form.get("host")
action = request.form.get("action")
route = request.form.get("route")
obj = {
'id': str(time()).split('.')[0] + str(randint(1, 1000000)),
'action': action,
'route': route,
'host': host
}
status = 'ok'
try:
cursor.insert(obj).run()
except:
status = 'error'
except:
status = 'error'
obj['status'] = status
return jsonify(obj)
if __name__ == '__main__':
app.run(debug=True)

33
misc.py
View File

@ -36,7 +36,25 @@ def alert(window, message, title="Munyal"):
button.pack()
def flatten_dirs(content, path="", action=""):
def _flatten_dirs(content, path=""):
for item in content:
item["name"] = path_join(path, item["name"])
if item["is_file"]:
yield item
else:
yield from _flatten_dirs(item["content"], item["name"])
del item["content"]
yield item
def flatten_dirs(content, path=""):
dirs = _flatten_dirs(content, path=path)
output = {}
for item in dirs:
output[item["name"]] = item
return output
def flatten_dirs_old(content, path="", action=""):
output = []
for item in content:
if item["is_file"]:
@ -56,3 +74,16 @@ def flatten_dirs(content, path="", action=""):
def path_join(*items):
return os.path.join(*items).replace("\\", "/")
if __name__ == "__main__":
from dir_to_json import get_json
import json
folder = sys.argv[1]
action = sys.argv[2]
try:
json_1 = get_json(folder)
except Exception as e:
print(e)
print("error outside")
sys.exit(1)
print(json.dumps(list(flatten_dirs(json_1)), indent=2))

4
requirements.txt 100644
View File

@ -0,0 +1,4 @@
websocket-client="*"
tcping="*"
requests="*"
pystray="*"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 65 KiB

View File

@ -1,118 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import ftplib
import json
import os
import socket
from copy import deepcopy
from time import sleep
from requests import post
from compare_json import compare_json
from dir_to_json import get_json
def need_deleted(items, route):
out = []
for item in items:
if item.get("is_file"):
out.append({"action": "delete", "route": os.path.join(route, item.get('name'))})
else:
if item.get('content'):
out = out + need_deleted(item.get("content"), os.path.join(route, item.get('name')))
else:
out.append({"action": "delete_folder", "route": os.path.join(route, item.get('name'))})
return out
def need_added(items, route):
out = []
for item in items:
if item.get("is_file"):
out.append({"action": "add", "route": os.path.join(route, item.get('name'))})
else:
if item.get('content'):
out = out + need_added(item.get("content"), os.path.join(route, item.get('name')))
else:
out.append({"action": "add_folder", "route": os.path.join(route, item.get('name'))})
return out
def get_changes(jsons, route=''):
delete, add = jsons
out = need_deleted(delete, route) + need_added(add, route)
return out
'''
out = []
delete, add = jsons
for item in delete:
if item.get("is_file"):
if item.get("tag"):
if item.get("tag") == "update":
out.append({"action": "update", "file": os.path.join(route, item.get('name'))})
elif item.get("tag") == "delete":##### Caso hipotetico imposible #####
if item.get("is_file"):
out.append({"action": "delete", "file": os.path.join(route, item.get('name'))})
else:
out.append({"action": "delete_folder", "file": os.path.join(route, item.get('name'))})
else:
out.append({"action": "delete", "file": os.path.join(route, item.get('name'))})
out.append({"action": "delete", "file": os.path.join(route, item.get('name'))})
else:
return out
'''
ORIGINAL = "/home/kirbylife/Proyectos/munyal_test/original"
def main(args):
ftp = ftplib.FTP('localhost', 'munyal', '123')
actual = get_json(ORIGINAL)
new = deepcopy(actual)
switch = lambda x,o,d=None: o.get(x) if o.get(x) else d if d else lambda *args: None
while True:
sleep(1)
jsons = compare_json(deepcopy(actual), deepcopy(new))
changes = get_changes(jsons)
for change in changes:
route = os.path.join(ORIGINAL, change['route'])
success = False
while not success:
# ~ try:
x = change['route']
if change['action'] == 'add':
print("Agregar archivo")
with open(route, "rb") as f:
ftp.storbinary("STOR /" + x, f)
elif change['action'] == 'add_folder':
print("Agregar carpeta")
ftp.mkd(x)
elif change['action'] == 'delete':
print("Borrar archivo")
ftp.delete(x)
elif change['action'] == 'delete_folder':
print("Borrar carpeta")
ftp.rmd(x)
else:
print("Unexpected action")
r = post('http://127.0.0.1:5000/upload', data={
'host': socket.gethostname(),
'action': change['action'],
'route': change['route']
}
)
r = json.loads(r.text)
print(json.dumps(r, indent=4))
success = r['status'] == 'ok'
# ~ except:
# ~ print("Error uploading, retrying again\n")
actual = deepcopy(new)
new = get_json(ORIGINAL)
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))