2019-07-28 16:18:49 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-12-08 20:45:42 +00:00
|
|
|
import ftplib
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import socket
|
2019-07-28 16:18:49 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from time import sleep
|
2020-12-08 20:45:42 +00:00
|
|
|
|
2019-07-28 16:18:49 +00:00
|
|
|
from requests import post
|
|
|
|
|
2020-12-08 20:45:42 +00:00
|
|
|
from compare_json import compare_json
|
|
|
|
from dir_to_json import get_json
|
|
|
|
|
2019-07-28 16:18:49 +00:00
|
|
|
|
|
|
|
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))
|