Repository navigation
Expand file tree
/
Copy pathget_modification.py
More file actions
79 lines (68 loc) · 2.9 KB
/
Copy pathget_modification.py
File metadata and controls
79 lines (68 loc) · 2.9 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
import os
import datetime
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class SyncEventHandler(FileSystemEventHandler):
"""Logs all the events captured."""
def __init__(self, q, path, ignore, f):
self.timestamps = dict()
self.base_path = path
self.q = q
self.ignore = ignore
self.f = f
print self.f.file_syncing
def on_moved(self, event):
super(SyncEventHandler, self).on_moved(event)
print "moved"
what = 'directory' if event.is_directory else 'file'
#logging.info("Moved %s: from %s to %s", what, event.src_path,
# event.dest_path)
if (self.valid_path(event.src_path) and self.valid_path(event.dest_path)):
self.ignore.append(self.clean_file_string(self.get_local_path(event.src_path)))
object = {"command":"move", "src": self.get_local_path(event.src_path), "dest": self.get_local_path(event.dest_path) }
self.q.append(object)
def on_created(self, event):
super(SyncEventHandler, self).on_created(event)
print "created"
what = 'directory' if event.is_directory else 'file'
#logging.info("Created %s: %s", what, event.src_path)
if (self.valid_path(event.src_path) and what=='directory'):
object = {"command": "create", "file": self.get_local_path(event.src_path), "what": what}
self.q.append(object)
def on_deleted(self, event):
super(SyncEventHandler, self).on_deleted(event)
time.sleep(1)
what = 'directory' if event.is_directory else 'file'
#logging.info("Deleted %s: %s", what, event.src_path)
if self.valid_path(event.src_path):
object = {"command": "delete", "file": self.get_local_path(event.src_path), "what": what}
self.q.append(object)
def on_modified(self, event):
super(SyncEventHandler, self).on_modified(event)
what = 'directory' if event.is_directory else 'file'
#logging.info("Modified %s: %s", what, event.src_path)
if self.valid_path(event.src_path):
self.create_object("put", event.src_path)
def create_object(self, command, path):
if (self.valid_path(path)):
o = {"command" : command, "file" : path}
self.q.append(o)
#o = {"command": "put", "files": self.timestamps.items()}
def valid_path(self, path):
if path == None:
return False
if path.find(".goutputstream") != -1:
return False
if path == self.f.file_syncing:
return False
return True
def clean_file_string(self, string):
if (len(string) > 0):
if string[0] == '/':
return string[1:]
return string
def get_local_path(self, full_path):
return full_path.replace(self.base_path, '')