-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient_daemon.py
More file actions
1361 lines (1172 loc) · 53.1 KB
/
Copy pathclient_daemon.py
File metadata and controls
1361 lines (1172 loc) · 53.1 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# inotify observer unable to detect 'dragging file to trash' events
# from https://github.com/gorakhargosh/watchdog/issues/46 use polling solution
# from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver as Observer
from watchdog.events import PatternMatchingEventHandler
from requests.auth import HTTPBasicAuth
import ConfigParser
import requests
import argparse
import asyncore
import hashlib
import logging
import shutil
import time
import json
import os
from communication_system import CmdMessageServer
SERVER_URL = "localhost"
SERVER_PORT = "5000"
API_PREFIX = "API/v1"
CONFIG_DIR_PATH = None # RawBox root
FILE_CONFIG = "config.ini"
logger = logging.getLogger('RawBox')
logger.setLevel(logging.DEBUG)
def get_relpath(abs_path):
"""This function return a relative path from an absolute one.
@param abs_path An absolute path
@return A relative path
"""
if abs_path.startswith(CONFIG_DIR_PATH):
return abs_path[len(CONFIG_DIR_PATH) + 1:]
return abs_path
def get_abspath(rel_path):
"""This function return an absolute path from a relative one.
@param rel_path A relative path
@return An absolute path
"""
if not rel_path.startswith(CONFIG_DIR_PATH):
return "/".join([CONFIG_DIR_PATH, rel_path])
return rel_path
def initialize_directory():
"""This function initialize RawBox's directory."""
shares_dir = os.path.join(CONFIG_DIR_PATH, "shares")
try:
os.makedirs(shares_dir)
except OSError:
pass
class ServerCommunicator(object):
"""This class has the responsibility to send requests to the Server."""
def __init__(self, server_url, username, password, snapshot_manager):
"""The constructor.
@param server_url Server URL
@param username A RawBox user's name (his email)
@param password RawBox user's password
@param snapshot_manager An instance of DirSnapshotManager
"""
if username and password:
self.auth = HTTPBasicAuth(username, password)
else:
self.auth = None
self.server_url = server_url
self.snapshot_manager = snapshot_manager
self.msg = {
"result": "",
"details": []
}
def write_user_data(self, user=None, psw=None, activate=False):
"""This method writes user data in config.ini file.
@param user RawBox user's name. It is set to None as default
@param psw RawBox user's password. It is set to None as default
@param activate It indicates if the user is activated or not. It
is set to False as default
"""
config_ini = ConfigParser.ConfigParser()
config_ini.read(FILE_CONFIG)
if not activate:
config_ini.set("daemon_user_data", "username", user)
config_ini.set("daemon_user_data", "password", psw)
else:
config_ini.set("daemon_user_data", "active", True)
with open(FILE_CONFIG, "wb") as config_file:
config_ini.write(config_file)
def delete_user_data(self):
"""This method deletes user data from config.ini file."""
config_ini = ConfigParser.ConfigParser()
config_ini.read(FILE_CONFIG)
config_ini.set("daemon_user_data", "username", None)
config_ini.set("daemon_user_data", "password", None)
config_ini.set("daemon_user_data", "active", False)
username = None
password = None
self.auth = HTTPBasicAuth(username, password)
with open(FILE_CONFIG, "wb") as config_file:
config_ini.write(config_file)
def setExecuter(self, executer):
"""This method sets the command executor of the daemon.
@param executer An instance of CommandExecuter
"""
self.executer = executer
def _try_request(self, callback, success='', error='', retry_delay=2, *args, **kwargs):
"""This method try a request until it's success.
@param callback Request Type to do
@param success String in case of success
@param error String in case of error
@param retry_delay Delay in case of exception threw
@param args Arguments of request
@param kwargs Arguments of request
@return request_result Request result
"""
while True:
try:
request_result = callback(
auth=self.auth,
*args, **kwargs
)
if request_result.ok:
logger.info(success)
else:
logger.error(request_result.reason)
return request_result
except requests.exceptions.RequestException:
time.sleep(retry_delay)
logger.warning(error)
def synchronize(self, operation_handler):
"""This method synchronize client and server folders.
@param operation_handler Operation occurred
"""
server_url = "{}/files/".format(self.server_url)
request = {"url": server_url}
sync = self._try_request(
requests.get, "getFile success", "getFile fail", **request)
if sync.status_code != requests.codes.unauthorized:
server_snapshot = sync.json()['snapshot']
server_timestamp = float(sync.json()['timestamp'])
logger.debug(
"".format("SERVER SAY: ", server_snapshot, server_timestamp, "\n"))
command_list = self.snapshot_manager.synchronize_dispatcher(
server_timestamp, server_snapshot)
self.executer.synchronize_executer(command_list)
self.snapshot_manager.save_timestamp(server_timestamp)
def get_url_relpath(self, abs_path):
"""This method from absolute path returns the relative path.
@param abs_path Absolute path
@return Relative Path
"""
return get_relpath(abs_path).replace(os.path.sep, '/')
def download_file(self, dst_path):
"""This method sends a message to the server for the downloading of a file.
@param dst_path File path
@return Local Path with request body in case of success
@return False in case of error
"""
error_log = "ERROR on download request " + dst_path
success_log = "file downloaded! " + dst_path
server_url = "{}/files/{}".format(
self.server_url,
self.get_url_relpath(dst_path))
request = {"url": server_url}
r = self._try_request(requests.get, success_log, error_log, **request)
local_path = get_abspath(dst_path)
if r.status_code == requests.codes.ok:
return local_path, r.content
else:
return False, False
def upload_file(self, dst_path, put_file=False):
"""This method sends a message to the server fot the uploading of a file.
@param dst_path File Path
@param put_file Tells if the file already exists on the server or not
"""
file_object = ''
try:
file_object = open(get_abspath(dst_path), 'rb')
file_content = open(get_abspath(dst_path), 'rb').read()
except IOError:
return False # Atomic create and delete error!
server_url = "{}/files/{}".format(
self.server_url,
self.get_url_relpath(dst_path))
error_log = "ERROR upload request " + dst_path
success_log = "file uploaded! " + dst_path
request = {
"url": server_url,
"files": {'file_content': file_object},
"data": {'file_md5': hashlib.md5(file_content).hexdigest()}
}
if put_file:
r = self._try_request(
requests.put, success_log, error_log, **request)
else:
r = self._try_request(
requests.post, success_log, error_log, **request)
if r.status_code == requests.codes.conflict:
logger.error("file {} already exists on server".format(dst_path))
elif r.status_code == requests.codes.created:
if put_file:
self.snapshot_manager.update_snapshot_update(
{"src_path": dst_path})
else:
self.snapshot_manager.update_snapshot_upload(
{"src_path": dst_path})
self.snapshot_manager.save_snapshot(float(r.text))
def delete_file(self, dst_path):
"""This method sends a message to the server for the deleting of a file.
@param dst_path File Path
"""
error_log = "ERROR delete request " + dst_path
success_log = "file deleted! " + dst_path
server_url = "{}/actions/delete".format(self.server_url)
request = {
"url": server_url,
"data": {"path": self.get_url_relpath(dst_path)}
}
r = self._try_request(requests.post, success_log, error_log, **request)
if r.status_code == requests.codes.not_found:
logger.error("DELETE REQUEST file {} not found on server".format(dst_path))
elif r.status_code == requests.codes.ok:
self.snapshot_manager.update_snapshot_delete({"src_path": dst_path})
self.snapshot_manager.save_snapshot(float(r.text))
def move_file(self, src_path, dst_path):
"""This method sends a message to server for moving a file.
@param src_path Source Path of the file
@param dst_path Destination Path of the file
"""
error_log = "ERROR move request " + dst_path
success_log = "file moved! " + dst_path
server_url = "{}/actions/move".format(self.server_url)
request = {
"url": server_url,
"data": {
"file_src": self.get_url_relpath(src_path),
"file_dest": self.get_url_relpath(dst_path),
}
}
r = self._try_request(requests.post, success_log, error_log, **request)
if r.status_code == requests.codes.not_found:
logger.error("MOVE REQUEST file {} not found on server".format(src_path))
elif r.status_code == requests.codes.created:
self.snapshot_manager.update_snapshot_move({"src_path": src_path, "dst_path": dst_path})
self.snapshot_manager.save_snapshot(float(r.text))
def copy_file(self, src_path, dst_path):
"""This method sends a message to the server for copying a file.
@param Source Path of the file
@param Destination Path of the file
"""
error_log = "ERROR copy request " + dst_path
success_log = "file copied! " + dst_path
server_url = "{}/actions/copy".format(self.server_url)
request = {
"url": server_url,
"data": {
"file_src": self.get_url_relpath(src_path),
"file_dest": self.get_url_relpath(dst_path),
}
}
r = self._try_request(requests.post, success_log, error_log, **request)
if r.status_code == requests.codes.not_found:
logger.error("COPY REQUEST file {} not found on server".format(src_path))
elif r.status_code == requests.codes.created:
self.snapshot_manager.update_snapshot_copy({"src_path": src_path, "dst_path": dst_path})
self.snapshot_manager.save_snapshot(float(r.text))
def create_user(self, param):
"""This method sends a message to the server for creating a user.
@param param This parameter includes user email and password
@return msg A message that includes details of request
"""
self.msg["details"] = []
error_log = "User creation error"
success_log = "user created!"
username = param["user"]
password = param["psw"]
server_url = "{}/Users/{}".format(self.server_url, param["user"])
request = {
"url": server_url,
"data": {
"psw": param["psw"],
"reset": param["reset"]
}
}
response = self._try_request(
requests.post, success_log, error_log, **request
)
self.msg["result"] = response.status_code
if response.status_code == requests.codes.created:
self.msg["details"].append(
"Check your email for the activation code")
logger.info("user: {} created!".format(username))
self.write_user_data(param["user"], param["psw"], activate=False)
elif response.status_code == requests.codes.not_acceptable:
logger.warning("{}".format(response.text))
self.msg["details"].append("{}".format(response.text))
elif response.status_code == requests.codes.conflict:
logger.warning("user: {} already exists!".format(username))
self.msg["details"].append("User already exists")
else:
error = ("on create user:\t email: {}\n\nsend message:\t"
"{}\nresponse is:\t{}").format(username, request, response.text)
logger.critical("\nbad request on user creation,"
" report this crash to RawBox_team@gmail.com\n {}\n\n".format(error))
self.msg["details"].append("Bad request")
return self.msg
def get_user(self, param):
"""This method returns user data if user exists
@param param This parameter includes user email, password and reset flag.
@return msg A message that includes details of request
"""
self.msg["details"] = []
error_log = "cannot get user data"
success_log = "user data retrived"
server_url = "{}/user".format(self.server_url)
request = {
"url": server_url,
"data": {
"user": param["user"],
"psw": param["psw"],
"reset": param["reset"]
}
}
response = self._try_request(
requests.get, success_log, error_log, **request)
self.msg["result"] = response.status_code
self.msg["details"].append(eval(response.text))
if response.status_code == 200:
self.msg["details"].append("User data retrieved")
elif response.status_code == 404:
self.msg["details"].append("User not found")
else:
self.msg["details"].append("Bad request")
return self.msg
def delete_user(self, param):
"""This method sends a message to the server for deleting user.
@param param Parameters are not used
@return msg A message that includes details of request
"""
self.msg["details"] = []
error_log = "Cannot delete user"
success_log = "User deleted"
server_url = "{}/Users/".format(self.server_url)
request = {
"url": server_url,
"data": {}
}
response = self._try_request(
requests.delete, success_log, error_log, **request)
self.msg["result"] = response.status_code
if response.status_code == requests.codes.ok:
self.delete_user_data()
self.msg["details"].append("User deleted")
elif response.status_code == requests.codes.unauthorized:
self.msg["details"].append("Access denied")
else:
self.msg["details"].append("Bad request")
return self.msg
def activate_user(self, param):
"""This method sends a message to the server for activating user.
@param param This parameter includes reset flag and activation code
@return msg A message that includes details of request
"""
self.msg["details"] = []
error_log = "Cannot activate user"
success_log = "User activated"
server_url = "{}/Users/{}".format(self.server_url, param["user"])
request = {
"url": server_url,
"data": {
"reset": param["reset"],
"code": param["code"]
}
}
response = self._try_request(
requests.put, success_log, error_log, **request)
self.msg["result"] = response.status_code
if response.status_code == requests.codes.created:
self.write_user_data(activate=True)
initialize_directory()
config, _ = load_config()
username = config['username']
password = config['password']
self.auth = HTTPBasicAuth(username, password)
self.msg["details"].append("You have now entered RawBox")
elif response.status_code == requests.codes.not_found:
self.msg["details"].append("User not found")
else:
self.msg["details"].append("Bad request")
return self.msg
def reset_password(self, param):
"""This method sends a message to the server for resetting user password.
@param param This parameter includes reset flag.(In this case must be set to "True")
@return msg A message that includes details of request
"""
self.msg["details"] = []
success_log = "Password resetted"
server_url = "{}/Users/{}/reset".format(self.server_url, param["user"])
request = {
"url": server_url,
"data": {
"reset": param["reset"]
}
}
response = self._try_request(requests.post, success_log, **request)
self.msg["result"] = response.status_code
if response.status_code == 202:
self.msg["details"].append("Check your email for the resetting code")
return self.msg
def set_password(self, param):
"""This method sends a message to the server for setting a new password
after resetting request.
@param param This parameter includes reset flag (In this case must be set to "True"),
activation code and new psw
@return msg A message that includes details of request
"""
self.msg["details"] = []
success_log = "Password resetted"
server_url = "{}/Users/{}/reset".format(self.server_url, param["user"])
request = {
"url": server_url,
"data": {
"reset": param["reset"],
"code": param["code"],
"psw": param["psw"]
}
}
response = self._try_request(requests.put, success_log, **request)
self.msg["result"] = response.status_code
if response.status_code == 202:
self.write_user_data(param["user"], param["psw"])
self.msg["details"].append("If email exists, your password will be resetted. Login needed!")
elif response.status_code == 404:
self.msg["details"].append("Wrong code or reset request not found")
return self.msg
def add_share(self, param):
"""This method sends a message to the server for sharing a resource
with a beneficiary.
@param param This parameter includes path to share and the beneficiary
@return msg A message that includes details of request
"""
self.msg["details"] = []
request = {
"url": "{}/shares/{}/{}".format(
self.server_url, param["path"], param["beneficiary"]
)
}
success_log = "share added with {}!".format(param["beneficiary"])
error_log = "ERROR in adding a share with {}".format(param["beneficiary"])
r = self._try_request(requests.post, success_log, error_log, **request)
self.msg["result"] = r.status_code
if r.status_code == 400:
self.msg["details"].append("Bad request")
elif r.status_code == 201:
self.msg["details"].append("Added share!")
return self.msg
def remove_share(self, param):
"""This method sends a message to the server for removing of shares.
@param param This parameter includes path of shares
@return msg A message that includes details of request
"""
self.msg["details"] = []
request = {
"url": "{}/shares/{}".format(self.server_url, param["path"]),
"data": {}
}
success_log = "The resource is no more shared"
error_log = "ERROR on removing all the shares"
response = self._try_request(
requests.delete, success_log, error_log, **request
)
self.msg["result"] = response.status_code
if response.status_code == 200:
self.msg["details"].append("Shares removed")
elif response.status_code == 400:
self.msg["details"].append("Error, shares not removed")
return self.msg
def remove_beneficiary(self, param):
"""This method sends a message to the server for removing of a
beneficiary from shares.
@param param This parameter includes path of shares and beneficiary to remove
@return msg A message that includes details of request
"""
self.msg["details"] = []
request = {
"url": "{}/shares/{}/{}".format(
self.server_url,
param["path"], param["beneficiary"]
),
"data": {}
}
success_log = "Removed user {} from shares".format(param["beneficiary"])
error_log = "ERROR on removing user {} from shares".format(param["beneficiary"])
response = self._try_request(
requests.delete, success_log, error_log, **request
)
self.msg["result"] = response.status_code
if response.status_code == 200:
self.msg["details"].append("User removed from shares")
elif response.status_code == 400:
self.msg["details"].append("Cannot remove user from shares")
return self.msg
def get_shares_list(self, param=None):
"""This method sends a message to the server for getting the list of shares.
@param param Parameters are not used
@return msg A message that includes details of request
"""
self.msg["details"] = []
error_log = "List shares error"
success_log = "List shares downloaded!"
request = {"url": "{}/shares/".format(self.server_url)}
response = self._try_request(
requests.get, success_log, error_log, **request
)
self.msg["result"] = response.status_code
if response.status_code != 401:
try:
shares = response.json()
self.msg["details"].append("Shares list downloaded")
sub_res = []
if shares["my_shares"]:
sub_res.append("My shares:\n")
sub_res.append(str(shares["my_shares"]))
if shares["other_shares"]:
sub_res.append("Other shares:\n")
sub_res.append(str(shares["other_shares"]))
if sub_res:
res = "".join(["\nList of shares\n"] + sub_res)
else:
res = "No shares found"
self.msg["details"].append(res)
except ValueError:
self.msg["details"].append("Shares not found")
else:
self.msg["details"].append("Unauthorized access")
return self.msg
class FileSystemOperator(object):
"""This class has the responsibility to do operations on the Client File System."""
def __init__(self, event_handler, server_com, snapshot_manager):
"""The constructor.
@param event_handler DirectoryEventHandler instance
@param server_com ServerCommunicator instance
@param snapshot_manager DirSnapshotManager instance
"""
self.snapshot_manager = snapshot_manager
self.event_handler = event_handler
self.server_com = server_com
def add_event_to_ignore(self, path):
"""This method allows to ignore an event.
@param path File Path to ignore
"""
self.event_handler.paths_ignored.append(path)
def write_a_file(self, path):
"""This method writes a file after its download (if it exists).
1: It sends a path to ignore to watchdog
2: It downloads the file from server
3: It creates directory chain
4: it creates the file
So, when watchdog see the first event on this path, ignore it.
@param path File Path of download
"""
abs_path, content = self.server_com.download_file(path)
if abs_path and content:
self.add_event_to_ignore(get_abspath(path))
try:
os.makedirs(os.path.split(abs_path)[0], 0755)
except OSError:
pass
with open(abs_path, 'wb') as f:
f.write(content)
self.snapshot_manager.update_snapshot_upload(
{"src_path": get_abspath(abs_path)})
else:
logger.error("DOWNLOAD REQUEST for file {} "
", not found on server".format(path))
def move_a_file(self, origin_path, dst_path):
"""This method moves a file from an origin path to a destination path.
1: It sends a path to ignore to watchdog (origin and dst path)
2: It creates directory chain for dst_path
3: It moves the file from origin_path to dst_path
So, when watchdog see the first event on this path, ignore it.
@param origin_path Path of the file to move
@param dst_path Destination path for the file to move
"""
self.add_event_to_ignore(get_abspath(origin_path))
self.add_event_to_ignore(get_abspath(dst_path))
try:
os.makedirs(os.path.split(dst_path)[0], 0755)
except OSError:
pass
shutil.move(origin_path, dst_path)
self.snapshot_manager.update_snapshot_move(
{"src_path": get_abspath(origin_path), "dst_path": get_abspath(dst_path)})
def copy_a_file(self, origin_path, dst_path):
"""This method copies a file in a specified path.
1: It sends a path to ignore to watchdog (dst path)
(because copy is a creation event)
2: It creates directory chain for dst_path
3: It copies the file of an origin_path in a dst_path
So, when watchdog see the first event on this path, ignore it.
@param origin_path Path of the file to copy
@param dst_path Path in which copy the file
"""
self.add_event_to_ignore(get_abspath(dst_path))
origin_path = get_abspath(origin_path)
dst_path = get_abspath(dst_path)
try:
os.makedirs(os.path.split(dst_path)[0], 0755)
except OSError:
pass
shutil.copyfile(origin_path, dst_path)
self.snapshot_manager.update_snapshot_copy(
{"src_path": get_abspath(origin_path), "dst_path": get_abspath(dst_path)})
def delete_a_file(self, dst_path):
"""This method deletes a file in a specified path.
1: It sends a path to ignore to watchdog (dst path)
2: It deletes the file
So, when watchdog see the first event on this path, ignore it.
@param dst_path Path of the file to delete
"""
self.add_event_to_ignore(get_abspath(dst_path))
dst_path = get_abspath(dst_path)
if os.path.isdir(dst_path):
shutil.rmtree(dst_path)
else:
try:
os.remove(dst_path)
except OSError:
pass
self.snapshot_manager.update_snapshot_delete(
{"src_path": get_abspath(dst_path)})
def load_config():
"""This method loads configuration data (Client Daemon data and Daemon User data)
from a .ini file.
@return config A dictionary containing all configuration data loaded
@return user_exists True if the user is activated and False if he isn't
"""
abs_path = os.path.dirname(os.path.abspath(__file__))
crash_log_path = os.path.join(abs_path, "RawBox_crash_report.log")
config_ini = ConfigParser.ConfigParser()
config_ini.read(FILE_CONFIG)
dir_path = os.path.join(os.path.expanduser("~"), "RawBox")
user_exists = True
config = {}
default_daemon_config = {
"server_url": "http://{}:{}/{}".format(SERVER_URL, SERVER_PORT, API_PREFIX),
"crash_repo_path": crash_log_path,
"stdout_log_level": "DEBUG",
"file_log_level": "ERROR",
"dir_path": dir_path,
"snapshot_file_path": "snapshot_file.json"
}
try:
config = {
"host": config_ini.get('cmd', 'host'),
"port": config_ini.get('cmd', 'port'),
"server_url": config_ini.get('daemon_communication', 'server_url'),
"crash_repo_path": config_ini.get('daemon_communication', 'crash_repo_path'),
"stdout_log_level": config_ini.get('daemon_communication', 'stdout_log_level'),
"file_log_level": config_ini.get('daemon_communication', 'file_log_level'),
"dir_path": config_ini.get('daemon_communication', 'dir_path'),
"snapshot_file_path": config_ini.get('daemon_communication', 'snapshot_file_path')
}
except ConfigParser.NoSectionError:
config_ini.add_section("daemon_communication")
for option, value in default_daemon_config.iteritems():
config_ini.set("daemon_communication", option, value)
for section in config_ini.sections():
for option, value in config_ini.items(section):
config[option] = value
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
with open(config["snapshot_file_path"], 'w') as snapshot:
json.dump({"timestamp": 0, "snapshot": ""}, snapshot)
try:
config["username"] = config_ini.get('daemon_user_data', 'username')
config["password"] = config_ini.get('daemon_user_data', 'password')
config["active"] = config_ini.get('daemon_user_data', 'active')
user_exists = config["active"]
except ConfigParser.NoSectionError:
user_exists = False
config_ini.add_section('daemon_user_data')
config_ini.set('daemon_user_data', 'username')
config_ini.set('daemon_user_data', 'password')
except ConfigParser.NoOptionError:
user_exists = False # in this case the user is created but not activated
with open(FILE_CONFIG, 'wb') as config_file:
config_ini.write(config_file)
return config, user_exists
class DirectoryEventHandler(PatternMatchingEventHandler):
"""This class handles changes in the client file system."""
def __init__(self, cmd, snap):
"""The constructor.
@param cmd ServerCommunicator instance
@param snap DirSnapshotManager instance
"""
super(DirectoryEventHandler, self).__init__(
ignore_patterns=[os.path.join(CONFIG_DIR_PATH, "shares/*")]
)
self.cmd = cmd
self.snap = snap
self.paths_ignored = []
def _is_copy(self, abs_path):
"""This method checks if a file_md5 already exists in my local snapshot
1: IF IS A COPY, return the first path of already exists file
2: ELSE, return False
@param abs_path Absolute Path of the file
"""
file_md5 = self.snap.file_snapMd5(abs_path)
if not file_md5:
return False
if file_md5 in self.snap.local_full_snapshot:
return self.snap.local_full_snapshot[file_md5][0]
return False
def on_moved(self, event):
"""This method is called when a file or a directory is moved or renamed.
@param event Event representing file/directory movement, of type
"DirMovedEvent" or "FileMovedEvent"
"""
if event.src_path not in self.paths_ignored:
if not event.is_directory:
self.cmd.move_file(event.src_path, event.dest_path)
else:
logger.debug("".format("ignored move on ", event.src_path))
self.paths_ignored.remove(event.src_path)
self.paths_ignored.remove(event.dest_path)
def on_created(self, event):
"""This method is called when a file or directory is created.
@param event Event representing file/directory creation, of type
"DirCreatedEvent" or "FileCreatedEvent"
"""
copy = False
if event.src_path not in self.paths_ignored:
if not event.is_directory:
copy = self._is_copy(event.src_path)
if copy:
self.cmd.copy_file(copy, event.src_path)
else:
self.cmd.upload_file(event.src_path)
else:
if copy:
logger.debug("".format("ignored copy on ", event.src_path))
else:
logger.debug("".format("ignored creation on ", event.src_path))
self.paths_ignored.remove(event.src_path)
def on_deleted(self, event):
"""This method is called when a file or directory is deleted.
@param event Event representing file/directory deletion, of type
"DirDeletedEvent" or "FileDeletedEvent"
"""
if event.src_path not in self.paths_ignored:
if not event.is_directory:
self.cmd.delete_file(event.src_path)
else:
logger.debug("".format("ignored deletion on ", event.src_path))
self.paths_ignored.remove(event.src_path)
def on_modified(self, event):
"""This method is called when a file or directory is modified.
@param event Event representing file/directory modification, of type
"DirModifiedEvent" or "FileModifiedEvent"
"""
if event.src_path not in self.paths_ignored:
if not event.is_directory:
self.cmd.upload_file(event.src_path, put_file=True)
else:
logger.debug("".format("ignored modified on ", event.src_path))
self.paths_ignored.remove(event.src_path)
class DirSnapshotManager(object):
"""This class has the responsibility to synchronize client and server"""
def __init__(self, snapshot_file_path, share_path):
"""The constructor.
It loads the last global snapshot and creates an instant snapshot
of local directory.
@param snapshot_file_path Path of the snapshot
"""
self.snapshot_file_path = snapshot_file_path
self.share_path = share_path
self.last_status = self._load_status()
self.local_full_snapshot = self.instant_snapshot()
def local_check(self):
"""This method checks if daemon is synchronized with local directory.
@return True if the local global snapshot is equal to the last global
snapshot, False if not
"""
local_global_snapshot = self.global_md5()
if self.last_status['snapshot'] == "":
return True
last_global_snapthot = self.last_status['snapshot']
return local_global_snapshot == last_global_snapthot
def is_syncro(self, server_timestamp):
"""This method checks if daemon timestamp is synchronized with server timestamp.
@param server_timestamp
@return True if the server timestamp is equal to the client timestamp,
False if not
"""
server_timestamp = server_timestamp
client_timestamp = self.last_status['timestamp']
return server_timestamp == client_timestamp
def _load_status(self):
"""This method loads the last snapshot from a file.
@return The last snapshot
"""
with open(self.snapshot_file_path) as f:
return json.load(f)
def file_snapMd5(self, file_path):
"""This method calculates the md5 of a file.
@param file_path File with which calculate the md5
@return MD5 of the file
"""
file_path = get_abspath(file_path)
file_md5 = hashlib.md5()
if os.path.isdir(file_path):
return False
with open(file_path, 'rb') as afile:
buf = afile.read(2048)
while len(buf) > 0:
file_md5.update(buf)
buf = afile.read(2048)
return file_md5.hexdigest()
def global_md5(self):
"""This method calculates the global md5 of local_full_snapshot.
@return Global Md5"""
for k, v in self.local_full_snapshot.items():
v.sort()
snap_list = sorted(list(self.local_full_snapshot.items()))
return hashlib.md5(str(snap_list)).hexdigest()
def instant_snapshot(self):
"""This method creates a snapshot of directory.
@return The snapshot of the directory
"""
dir_snapshot = {}
for root, dirs, files in os.walk(self.share_path):
for f in files:
full_path = os.path.join(root, f)
file_md5 = self.file_snapMd5(full_path)
rel_path = get_relpath(full_path)
if file_md5 in dir_snapshot:
dir_snapshot[file_md5].append(rel_path)
else:
dir_snapshot[file_md5] = [rel_path]
return dir_snapshot
def save_snapshot(self, timestamp):
"""This method saves snapshot to file"""
self.last_status['timestamp'] = float(timestamp)
self.last_status['snapshot'] = self.global_md5()
with open(self.snapshot_file_path, 'w') as f:
f.write(
json.dumps({"timestamp": float(timestamp), "snapshot": self.last_status['snapshot']}))
def update_snapshot_upload(self, body):
"""This method updates the local full snapshot in case of upload request.
@param body
"""
self.local_full_snapshot[self.file_snapMd5(
body['src_path'])] = [get_relpath(body["src_path"])]
def update_snapshot_update(self, body):
"""This method updates the local full snapshot in case of update request.
@param body
"""