-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
376 lines (333 loc) · 12.4 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/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
from requests import get
from shutil import rmtree
from threading import Thread
from tcping import Ping
from tkinter import Tk
from tkinter import StringVar
from tkinter import Entry
from tkinter import Button
from tkinter import Label
import rethinkdb as r
import os
import socket
import json
import pathlib
from random import randint
import sys
import ftplib
#ORIGINAL = "/home/kirbylife/Proyectos/munyal_test/another"
ORIGINAL = "/home/kirbylife/Proyectos/munyal_test/"
IP = "localhost"
USERNAME = "munyal"
PASSWORD = "123"
RDB = ""
FTP = ""
HTTP = ""
HOSTNAME = socket.gethostname() + str(randint(1, 100000000))
SKIP_UPLOAD = []
FLAG_DOWNLOAD = False
pending_routes = []
split_address = lambda x: (x.split(":")[0], int(x.split(":")[1]))
def alert(title, message):
window = Tk()
window.title = title
Label(window, text=message).pack()
Button(window, text="Aceptar", command=window.destroy)
def check_network(port):
ping = Ping(IP, port, 20)
ping.ping(1)
print(SKIP_UPLOAD)
return ping.result.rows[0].successed == 1
def get_addresses(name, first=False):
global RDB
global FTP
global HTTP
while True:
response = get("http://localhost:5000/directory", params={"name": name})
response_content = json.loads(response.text)
if response.status_code != 200:
if first:
alert("Error", "Revisa tu cone")
return False
elif response_content['status'] != 'ok':
if first:
alert(response['error'], response['message'])
return false
else:
RDB = response['rdb_address'] if response['rdb_address'] != RDB else RDB
FTP = response['ftp_address'] if response['ftp_address'] != FTP else FTP
HTTP = response['http_address'] if response['http_address'] != HTTP else HTTP
if first:
return True
sleep(60)
def watch_dir():
global pending_routes
folder = os.path.join(os.getenv("HOME"), ".munyal")
if not os.path.exists(folder):
pathlib.Path(folder).mkdir(parents=True)
actual_file = os.path.join(folder, "actual.json")
if not os.path.exists(actual_file):
with open(actual_file, "w") as f:
f.write(json.dumps([]))
pending_file = os.path.join(folder, "pending_routes.json")
if not os.path.exists(pending_file):
with open(pending_file, "w") as f:
f.write(json.dumps([]))
with open(actual_file, "r") as f:
actual = json.loads(f.read())
actual = get_json(ORIGINAL)
new = deepcopy(actual)
with open(pending_file, "r") as f:
pending_routes = json.loads(f.read())
new = get_json(ORIGINAL)
while True:
sleep(0.2)
while True:
try:
jsons = compare_json(deepcopy(actual), deepcopy(new))
except:
new = get_json(ORIGINAL)
else:
break
changes = get_changes(jsons)
pending_routes = pending_routes + changes
with open(pending_file, "w") as f:
f.write(json.dumps(pending_routes, indent=4))
actual = deepcopy(new)
with open(actual_file, "w") as f:
f.write(json.dumps(actual, indent=4))
while True:
try:
new = get_json(ORIGINAL)
except:
pass
else:
break
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
def _is_ftp_dir(ftp_handle, name):
original_cwd = ftp_handle.pwd()
try:
ftp_handle.cwd(name)
ftp_handle.cwd(original_cwd)
return True
except:
return False
def _make_parent_dir(fpath):
#dirname = os.path.dirname(fpath)
dirname = os.path.join(ORIGINAL, fpath)
while not os.path.exists(dirname):
try:
os.makedirs(dirname)
print("created {0}".format(dirname))
except:
_make_parent_dir(dirname)
def _download_ftp_file(ftp_handle, name, dest, overwrite):
if not os.path.exists(dest) or overwrite is True:
try:
with open(dest, 'wb') as f:
ftp_handle.retrbinary("RETR {0}".format(name), f.write)
print("downloaded: {0}".format(dest))
except FileNotFoundError:
print("FAILED: {0}".format(dest))
else:
print("already exists: {0}".format(dest))
def _mirror_ftp_dir(ftp_handle, name, overwrite):
for item in ftp_handle.nlst(name):
SKIP_UPLOAD.append(item)
if _is_ftp_dir(ftp_handle, item):
_make_parent_dir(item.lstrip("/"))
_mirror_ftp_dir(ftp_handle, os.path.join(name, item), overwrite)
else:
_download_ftp_file(ftp_handle, item, os.path.join(ORIGINAL, item), overwrite)
def download_ftp_tree(overwrite=False):
FLAG_DOWNLOAD = True
ftp_handle = ftplib.FTP(IP, USERNAME, PASSWORD)
path = ""
original_directory = os.getcwd()
os.chdir(ORIGINAL)
_mirror_ftp_dir(ftp_handle, path, overwrite)
os.chdir(original_directory)
ftp_handle.close()
FLAG_DOWNLOAD = False
def upload(*args):
global SKIP_UPLOAD
global pending_routes
global HTTP
global FTP
print("Modulo de subida listo")
while True:
sleep(0.1)
if check_network(FTP) and check_network(HTTP) and pending_routes:
change = pending_routes.pop(0)
while FLAG_DOWNLOAD:
print("Wait")
if change['route'] not in SKIP_UPLOAD:
ftp = ftplib.FTP(IP, USERNAME, PASSWORD)
route = os.path.join(ORIGINAL, change['route'])
success = False
while not success:
try:
if change['action'] == 'add':
print("Agregar archivo")
with open(route, "rb") as f:
ftp.storbinary("STOR /" + change['route'], f)
elif change['action'] == 'add_folder':
print("Agregar carpeta")
ftp.mkd(change['route'])
elif change['action'] == 'delete':
print("Borrar archivo")
ftp.delete(change['route'])
elif change['action'] == 'delete_folder':
print("Borrar carpeta")
ftp.rmd(change['route'])
else:
print("Unexpected action")
except:
print("Error uploading\n")
r = post("http://"+HTTP+'/upload', data={
'host': HOSTNAME,
'action': change['action'],
'route': change['route']
}
)
r = json.loads(r.text)
print(json.dumps(r, indent=4))
success = r['status'] == 'ok'
ftp.close()
else:
SKIP_UPLOAD.pop()
return 0
def download(*args):
global SKIP_UPLOAD
global RDB
global FTP
while True:
sleep(1)
if check_network(RDB) and check_network(RDB):
try:
download_ftp_tree(overwrite=False)
print("Modulo de descarga listo")
print("Carpeta " + ORIGINAL)
r.connect(IP, 28015).repl()
cursor = r.table("changes").changes().run()
for document in cursor:
change = document['new_val']
#print(change)
if change['host'] != HOSTNAME:
FLAG_DOWNLOAD = True
route = os.path.join(ORIGINAL, change['route'])
SKIP_UPLOAD.append(change['route'])
try:
if change['action'] == 'add':
print("Agregar archivo")
ftp = ftplib.FTP(IP, USERNAME, PASSWORD)
with open(route, "wb") as f:
ftp.retrbinary("RETR /" + change['route'], f.write)
ftp.close()
elif change['action'] == 'add_folder':
print("Agregar carpeta")
pathlib.Path(route).mkdir(parents=True)
elif change['action'] == 'delete':
print("Borrar archivo")
pathlib.Path(route).unlink()
elif change['action'] == 'delete_folder':
print("Borrar carpeta")
rmtree(route)
else:
print("Unexpected action")
except OSError as e:
print("Error en el sistema operativo")
except:
print("Error en el servidor FTP")
FLAG_DOWNLOAD = False
except rethinkdb.errors.ReqlDriverError as e:
print("Conection refused with rethinkdb")
return 0
def run_client(window, password, username, server_name, folder):
global PASSWORD
global IP
global USERNAME
global ORIGINAL
PASSWORD = password.get()
USERNAME = username.get()
NAME = server_name.get()
ORIGINAL = folder.get()
if not os.path.exists(ORIGINAL):
pathlib.Path(ORIGINAL).mkdir(parents=True)
download_thread = Thread(target=download, args=[window])
download_thread.setDaemon(True)
download_thread.start()
upload_thread = Thread(target=upload, args=[window])
upload_thread.setDaemon(True)
upload_thread.start()
watch_dir_thread = Thread(target=watch_dir)
watch_dir_thread.setDaemon(True)
watch_dir_thread.start()
def main(args):
root = Tk()
root.geometry("200x300")
root.title("MUNYAL")
server_name = StringVar()
server_name.set("munyal")
server_name_field = Entry(root, textvariable=server_name)
user = StringVar()
user.set("munyal")
user_field = Entry(root, textvariable=user)
passwd = StringVar()
passwd.set("123")
passwd_field = Entry(root, textvariable=passwd, show="*")
folder = StringVar()
folder.set(os.path.join(os.getenv("HOME"), "Munyal"))
folder_field = Entry(root, textvariable=folder)
connect = Button(root, text="Conectar", command = lambda: run_client(root, passwd, user, server_name, folder))
Label(root, text="MUNYAL").pack()
Label(root, text="").pack()
Label(root, text="Ruta del servidor").pack()
host_field.pack()
Label(root, text="").pack()
Label(root, text="Nombre de usuario").pack()
user_field.pack()
Label(root, text="").pack()
Label(root, text="Contraseña").pack()
passwd_field.pack()
Label(root, text="").pack()
Label(root, text="Carpeta a sincronizar").pack()
folder_field.pack()
Label(root, text="").pack()
connect.pack()
root.mainloop()
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))