118 lines
4.1 KiB
Python
118 lines
4.1 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
from compare_json import compare_json
|
||
|
from dir_to_json import get_json
|
||
|
|
||
|
from copy import deepcopy
|
||
|
from time import sleep
|
||
|
from requests import post
|
||
|
|
||
|
import os
|
||
|
import socket
|
||
|
import json
|
||
|
import ftplib
|
||
|
|
||
|
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))
|