-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_sync.py
456 lines (329 loc) · 18.5 KB
/
_sync.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
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
'''
This controls the sync and its options
Also implements every action related to a sync
'''
import _file
import _defaults
import _validations
import _about
import os
import ast
import json
import random
import collections
from Slea import slea
class Sync:
def __init__(self, path):
self.path = path # stores the path of the sync file (that is also its name in the schedule file)
def load(self, log): # loads the sync from its path and validates its values
try:
log.report("ok_sync_running", detail=self.path)
self.properties = json.loads(open(self.path, "r", encoding="utf-8").read(), object_pairs_hook=collections.OrderedDict)
except:
log.report("error_sync_opening", detail=self.path)
return False
if not self.validate_enable(self.properties, log):
return False
if not ast.literal_eval(self.properties["enable"]):
# will skip validating the file if it is disabled
return True
if not self.validate_source_path(self.properties, log):
return False
if not self.validate_source_selection_condition(self.properties, log):
return False
if not self.validate_source_subfolder_search(self.properties, log):
return False
if not self.validate_source_filelist_shuffle(self.properties, log):
return False
if not self.validate_destination_path(self.properties, log):
return False
if not self.validate_destination_selection_condition(self.properties, log):
return False
if not self.validate_destination_subfolder_search(self.properties, log):
return False
if not self.validate_destination_filelist_shuffle(self.properties, log):
return False
if not self.validate_unnacurate_comparison(self.properties, log):
return False
if not self.validate_hierarchy_maintenance(self.properties, log):
return False
if not self.validate_left_files_deletion(self.properties, log):
return False
if not self.validate_file_override(self.properties, log):
return False
if not self.validate_size_limit(self.properties, log):
return False
if not self.validate_sync_cooldown(self.properties, log):
return False
log.report("ok_sync_json_load")
return True
def run(self, control, log): # runs this sync if needed
if (self.path not in control.properties) or (control.its_time(self.path)): # will run if this sync is not scheduled or if its cooldown has ended
if not ast.literal_eval(self.properties["enable"]): # checks if it is disabled before start running
log.report("ok_sync_run_disabled")
return True
if self.properties["source_path"].startswith(self.properties["destination_path"]):
log.report("error_sync_source_subfolder_of_destination")
return False
if self.properties["destination_path"].startswith(self.properties["source_path"]):
log.report("error_sync_destination_subfolder_of_source")
return False
# creates a list of files inside the source folder
source = File_List(
self.properties["source_path"],
self.properties["source_selection_condition"],
ast.literal_eval(self.properties["source_subfolder_search"]),
ast.literal_eval(self.properties["source_filelist_shuffle"])
)
# creates a list of files inside the destination folder
destination = File_List(
self.properties["destination_path"],
self.properties["destination_selection_condition"],
ast.literal_eval(self.properties["destination_subfolder_search"]),
ast.literal_eval(self.properties["destination_filelist_shuffle"])
)
if not source.fill(int(self.properties["size_limit"]), log): # fills the source file list with the specified size limit
return False
if not destination.fill(0, log): # fills the destination file list with no size limit
return False
if ast.literal_eval(self.properties["left_files_deletion"]):
if not destination.mark_files_to_delete_except_by(source, ast.literal_eval(self.properties["hierarchy_maintenance"]), ast.literal_eval(self.properties["inaccurate_comparison"]), log): # marks files in the file list to be removed
return False
if not destination.remove_marked_files(log): # removes the previously marked files
return False
if not destination.remove_empty_folders(log): # removes empty folders in the destination
return False
if not ast.literal_eval(self.properties["file_override"]):
if not source.mark_files_to_copy_except_by(destination, ast.literal_eval(self.properties["hierarchy_maintenance"]), ast.literal_eval(self.properties["inaccurate_comparison"]), log): # unmarks redundant files to prevent overwriting
return False
if not source.copy_marked_files(self.properties["destination_path"], ast.literal_eval(self.properties["hierarchy_maintenance"]), log): # copies the marked files
return False
control.schedule(self.path, int(self.properties["sync_cooldown"])) # schedules the next time to run this sync again
log.sync_occurred = True
log.report("ok_sync_finished")
return True
else:
log.report("ok_sync_still_in_cooldown")
return True
def disable(self, log): # disables this sync (useful if an error occurred)
try:
self.properties["enable"] = "False"
with open(self.path, "w") as file_to_write:
json.dump(self.properties, file_to_write, indent=4, ensure_ascii=False)
log.report("ok_sync_disable")
return True
except:
log.report("error_sync_disable", critical=True)
return False
def validate_enable(self, json, log):
if not "enable" in json:
log.report("error_sync_enable_missing") # has to be specified
return False
if not _validations.validate_boolean_value(json["enable"]):
log.report("error_sync_enable", detail=json["enable"])
return False
return True
def validate_source_path(self, json, log):
if not "source_path" in json:
log.report("error_sync_source_path_missing") # has to be specified
return False
if not _validations.validate_path(json["source_path"]):
log.report("error_sync_source_path", detail=json["source_path"])
return False
return True
def validate_source_selection_condition(self, json, log):
if not "source_selection_condition" in json:
json["source_selection_condition"] = _defaults.default_source_selection_condition # if wasn't found in the json, use the default value
if not _validations.validate_selection_condition(json["source_selection_condition"]):
log.report("error_sync_source_selection_condition", detail=json["source_selection_condition"])
return False
return True
def validate_source_subfolder_search(self, json, log):
if not "source_subfolder_search" in json:
json["source_subfolder_search"] = _defaults.default_source_subfolder_search # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["source_subfolder_search"]):
log.report("error_sync_source_subfolder_search", detail=json["source_subfolder_search"])
return False
return True
def validate_source_filelist_shuffle(self, json, log):
if not "source_filelist_shuffle" in json:
json["source_filelist_shuffle"] = _defaults.default_source_filelist_shuffle # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["source_filelist_shuffle"]):
log.report("error_sync_source_filelist_shuffle", detail=json["source_filelist_shuffle"])
return False
return True
def validate_destination_path(self, json, log):
if not "destination_path" in json:
log.report("error_sync_destination_path_missing") # has to be specified
return False
if not _validations.validate_path(json["destination_path"]):
log.report("error_sync_destination_path", detail=json["destination_path"])
return False
return True
def validate_destination_selection_condition(self, json, log):
if not "destination_selection_condition" in json:
json["destination_selection_condition"] = _defaults.default_destination_selection_condition # if wasn't found in the json, use the default value
if not _validations.validate_selection_condition(json["destination_selection_condition"]):
log.report("error_sync_destination_selection_condition", detail=json["destination_selection_condition"])
return False
return True
def validate_destination_subfolder_search(self, json, log):
if not "destination_subfolder_search" in json:
json["destination_subfolder_search"] = _defaults.default_destination_subfolder_search # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["destination_subfolder_search"]):
log.report("error_sync_destination_subfolder_search", detail=json["destination_subfolder_search"])
return False
return True
def validate_destination_filelist_shuffle(self, json, log):
if not "destination_filelist_shuffle" in json:
json["destination_filelist_shuffle"] = _defaults.default_destination_filelist_shuffle # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["destination_filelist_shuffle"]):
log.report("error_sync_destination_filelist_shuffle", detail=json["destination_filelist_shuffle"])
return False
return True
def validate_unnacurate_comparison(self, json, log):
if not "inaccurate_comparison" in json:
json["inaccurate_comparison"] = _defaults.default_inaccurate_comparison # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["inaccurate_comparison"]):
log.report("error_sync_inaccurate_comparison", detail=json["inaccurate_comparison"])
return False
return True
def validate_hierarchy_maintenance(self, json, log):
if not "hierarchy_maintenance" in json:
json["hierarchy_maintenance"] = _defaults.default_hierarchy_maintenance # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["hierarchy_maintenance"]):
log.report("error_sync_hierarchy_maintenance", detail=json["hierarchy_maintenance"])
return False
return True
def validate_left_files_deletion(self, json, log):
if not "left_files_deletion" in json:
json["left_files_deletion"] = _defaults.default_left_files_deletion # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["left_files_deletion"]):
log.report("error_sync_left_files_deletion", detail=json["left_files_deletion"])
return False
return True
def validate_file_override(self, json, log):
if not "file_override" in json:
json["file_override"] = _defaults.default_file_override # if wasn't found in the json, use the default value
if not _validations.validate_boolean_value(json["file_override"]):
log.report("error_sync_file_override", detail=json["file_override"])
return False
return True
def validate_size_limit(self, json, log):
if not "size_limit" in json:
json["size_limit"] = _defaults.default_size_limit # if wasn't found in the json, use the default value
if not _validations.validate_integer_greater_than_or_equal_to_zero(json["size_limit"]):
log.report("error_sync_size_limit", detail=json["size_limit"])
return False
return True
def validate_sync_cooldown(self, json, log):
if not "sync_cooldown" in json:
json["sync_cooldown"] = _defaults.default_sync_cooldown # if wasn't found in the json, use the default value
if not _validations.validate_integer_greater_than_zero(json["sync_cooldown"]):
log.report("error_sync_sync_cooldown", detail=json["sync_cooldown"])
return False
return True
class File_List: # creates a list of files
def __init__(self, path, selection_condition, subfolder_search, filelist_shuffle):
self.filelist = []
self.path = path
self.selection_condition = selection_condition
self.subfolder_search = subfolder_search
self.filelist_shuffle = filelist_shuffle
def fill(self, size_limit, log): # fills the file list
# mounts the syntax tree out of the selection confition string
# as it is the same condition to all files in this File_List, it can be created once and then reused
syntax_tree = slea.get_syntax_tree(self.selection_condition)
for (dirpath, dirnames, filenames) in os.walk(self.path):
for f in filenames:
file = _file.File(os.path.join(dirpath, f))
if slea.evaluate_syntax_tree(syntax_tree, file.evaluate_condition, log):
self.filelist.append(file)
if not self.subfolder_search:
break
if self.filelist_shuffle: # will shuffle the file list if needed
random.shuffle(self.filelist)
if size_limit > 0: # will trim the file list to meet the specified limit
total_size = 0.0
for f in self.filelist:
if total_size + f.size > size_limit:
self.filelist = self.filelist[:self.filelist.index(f)]
break
else:
total_size += f.size
log.report("ok_file_list_load")
return True
def mark_files_to_delete_except_by(self, other, hierarchy_maintenance, inaccurate_comparison, log): # marks the leftover files to be removed
for f_this in self.filelist:
if not f_this.path == "":
f_this.to_delete = True
for f_other in other.filelist:
if not f_other.path == "":
if hierarchy_maintenance:
if f_this.path.split(self.path, 1)[1] == f_other.path.split(other.path, 1)[1]:
if inaccurate_comparison:
f_this.to_delete = False
elif f_this.is_the_same_file(f_other):
f_this.to_delete = False
break
elif f_this.path == os.path.join(self.path, f_other.basename):
if inaccurate_comparison:
f_this.to_delete = False
elif f_this.is_the_same_file(f_other):
f_this.to_delete = False
break
log.report("ok_file_list_mark_files_to_delete")
return True
def mark_files_to_copy_except_by(self, other, hierarchy_maintenance, inaccurate_comparison, log): # unmarks redundant files to prevent being overwrited unnecessarily
for f_this in self.filelist:
f_this.to_copy = True
if not f_this.path == "":
for f_other in other.filelist:
if not f_other.path == "":
if hierarchy_maintenance:
if f_this.path.split(self.path, 1)[1] == f_other.path.split(other.path, 1)[1]:
if inaccurate_comparison:
f_this.to_copy = False
elif f_this.is_the_same_file(f_other):
f_this.to_copy = False
break
elif os.path.join(other.path, f_this.basename) == f_other.path:
if inaccurate_comparison:
f_this.to_copy = False
elif f_this.is_the_same_file(f_other):
f_this.to_copy = False
break
log.report("ok_file_list_unmark_redundant_files")
return True
def remove_marked_files(self, log): # removes all marked files
for f_this in self.filelist:
if f_this.to_delete:
if not f_this.delete(log):
log.report("error_file_list_remove_marked_files")
return False
log.report("ok_file_list_remove_marked_files")
return True
def copy_marked_files(self, destination, hierarchy_maintenance, log): # copies all marked files to the destination folder
for f_this in self.filelist:
if f_this.to_copy:
target_path = os.path.split(f_this.path.replace(self.path, destination))[0] if hierarchy_maintenance else destination
if not f_this.copy(target_path, log):
log.report("error_file_list_copying_files")
return False
log.report("ok_file_list_copying_files")
return True
def remove_empty_folders(self, log): # removes all empty folders under the path of this file list
for (dirpath, dirnames, filenames) in os.walk(self.path, topdown=False):
if dirpath == self.path:
break
else:
if os.listdir(dirpath) == []:
try:
os.rmdir(dirpath)
log.report("ok_file_list_remove_empty_folder", detail=dirpath)
except:
log.report("error_file_list_remove_empty_folder", detail=dirpath)
log.report("error_file_list_remove_empty_folders")
return False
log.report("ok_file_list_remove_empty_folders")
return True