-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
223 lines (178 loc) · 7.5 KB
/
utils.py
File metadata and controls
223 lines (178 loc) · 7.5 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
import os
chunk = 1000000
# send all the back-up folder to the socket
def send_all(s, key_folder_name):
with s:
s.send(os.sep.encode() + b'\n')
for path, dirs, files in os.walk(key_folder_name):
for d in dirs:
send_folder(s, os.path.join(path, d), key_folder_name)
for file1 in files:
sendfile(s, key_folder_name, os.path.join(path, file1))
# receive all the changes in the back-up folder from the socket
def receive_changes(s, folder_name, size, map_key_of_map_client_and_changes=None, computer_id=0):
while size > 0:
receive_event(readline(s).strip(), s, folder_name, map_key_of_map_client_and_changes, computer_id)
size = size - 1
# receive all the back-up folder from the socket
def receive_folders(s, folder_name):
separator = readline(s)
while True:
if receive_create(s, folder_name, separator) == -1:
break
# read a line from the socket
def readline(s):
rec = s.recv(1)
while '\n'.encode() not in rec and rec:
rec += s.recv(1)
if rec:
return rec[:-1].decode()
return ''
# receive a report about a new file in the client's folder
def receive_create(s, key_folder_name, separator, map_key_of_map_client_and_changes=None, computer_id=0):
rec = readline(s)
if not rec:
return -1
# get the name, the length and the path of the file
filename = rec[1:].strip()
length = int(readline(s))
path = key_folder_name
for dirname in filename.split(separator):
path = os.path.join(path, dirname)
# send all the computers of the client the changes and to the event list before changes
send_client_computers(map_key_of_map_client_and_changes, key_folder_name, computer_id, path, '', 'created')
# create the directory or the new file
if rec[0] == 'd':
os.makedirs(path, exist_ok=True)
return 1
os.makedirs(os.path.dirname(path), exist_ok=True)
return write_file(s, path, length)
# send a folder
def send_folder(s, path, key_folder_name):
rel_path_dir = os.path.relpath(path, key_folder_name)
dir_size = 0
s.send(b'd' + rel_path_dir.encode() + b'\n')
s.send(str(dir_size).encode() + b'\n')
# send a file
def sendfile(s, key_folder_name, filename):
rel_path = os.path.relpath(filename, key_folder_name)
filesize = os.path.getsize(filename)
with open(filename, 'rb') as f:
s.send(b'f' + rel_path.encode() + b'\n')
s.send(str(filesize).encode() + b'\n')
# Send the file in chunks so large files can be handled.
data = f.read(chunk)
while data:
s.send(data)
data = f.read(chunk)
# send all the computers the change in the back-up folder
def send_client_computers(map_key_of_map_client_and_changes, key_folder_name, computer_id, src, dst, report):
if map_key_of_map_client_and_changes is not None:
for cid in map_key_of_map_client_and_changes[key_folder_name].keys():
if cid != computer_id:
if map_key_of_map_client_and_changes[key_folder_name][cid] is not None:
map_key_of_map_client_and_changes[key_folder_name][cid].append((src, dst, report))
else:
map_key_of_map_client_and_changes[key_folder_name][cid] = [(src, dst, report)]
# write a file from the socket in the path
def write_file(s, path, length):
# Read the data in chunks, so it can handle large files.
with open(path, 'wb') as f:
while length:
chunk_data = min(length, chunk)
data = s.recv(chunk_data)
if not data:
break
f.write(data)
length -= len(data)
else: # only runs if while doesn't break and length==0
return 1
return -1
# remove directory recursively
def remove_directory(directory):
for path, dirs, files in os.walk(directory):
for file1 in files:
os.remove(os.path.join(path, file1))
for d in dirs:
remove_directory(os.path.join(path, d))
os.rmdir(directory)
# receive a report about deleting in the client's folder
def receive_delete(s, key_folder_name, separator, map_key_of_map_client_and_changes=None, computer_id=0):
path = readline(s)
full_path = key_folder_name
for name in path.split(separator):
if name != '..' and name != '.':
full_path = os.path.join(full_path, name)
# send all the computers of the client the changes and to the event list before changes
send_client_computers(map_key_of_map_client_and_changes, key_folder_name, computer_id, full_path, '', 'deleted')
try:
# delete the directory or the file
if os.path.isdir(full_path):
remove_directory(full_path)
else:
os.remove(full_path)
except:
pass
# receive a report about a move in the client's folder
def receive_move(s, key_folder_name, separator, map_key_of_map_client_and_changes=None, computer_id=0):
# define the path of the source
src = readline(s)
for d in src.split(separator)[1:]:
key_folder_name = os.path.join(key_folder_name, d)
src = key_folder_name
key_folder_name = key_folder_name.split(separator)[0]
# define the path of the destination
dst = readline(s)
for d in dst.split(separator)[1:]:
key_folder_name = os.path.join(key_folder_name, d)
dst = key_folder_name
key_folder_name = key_folder_name.split(separator)[0]
# send all the computers of the client the changes and to the event list before changes
send_client_computers(map_key_of_map_client_and_changes, key_folder_name, computer_id, src, dst, 'moved')
try:
# move the file by rename it
os.rename(src, dst)
except:
pass
# send about a file deleted
def send_delete(s, src):
s.send('delete'.encode() + b'\n')
s.send(os.sep.encode() + b'\n')
s.send(src.encode() + b'\n')
# send about a file moved
def send_move(s, src, dst):
s.send('move'.encode() + b'\n')
s.send(os.sep.encode() + b'\n')
s.send(src.encode() + b'\n')
s.send(dst.encode() + b'\n')
# send about a new file created
def send_create(s, key_folder_name, src):
s.send('create'.encode() + b'\n')
s.send(os.sep.encode() + b'\n')
if os.path.isdir(src):
send_folder(s, src, key_folder_name)
else:
sendfile(s, key_folder_name, src)
# call the function according to the report we want to send
def send_event(option, s, directory, src, dst):
if option == 'created':
return send_create(s, directory, src)
if option == 'deleted':
return send_delete(s, os.path.relpath(src, directory))
if option == 'moved':
return send_move(s, src, dst)
if option == 'modified':
s.send('modify'.encode() + b'\n')
s.send(os.sep.encode() + b'\n')
return sendfile(s, directory, src)
# call the function according to the report we got
def receive_event(option, s, folder_name, map_key_of_map_client_and_changes=None, computer_id=0):
separator = readline(s)
if option == 'create':
return receive_create(s, folder_name, separator, map_key_of_map_client_and_changes, computer_id)
if option == 'delete':
return receive_delete(s, folder_name, separator, map_key_of_map_client_and_changes, computer_id)
if option == 'move':
return receive_move(s, folder_name, separator, map_key_of_map_client_and_changes, computer_id)
if option == 'modify':
return receive_create(s, folder_name, separator, map_key_of_map_client_and_changes, computer_id)