-
Notifications
You must be signed in to change notification settings - Fork 5
/
inotify_thread.py
159 lines (134 loc) · 5.25 KB
/
inotify_thread.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
# -*- coding: utf-8 -*-
# Copyright (c) 2012 Wu Tangsheng(lanbaba) <[email protected]>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os, sys, threading, logging
from Queue import *
from ossync.lib import queue_model
from ossync.lib import helper
try:
import pyinotify
except ImportError as e:
print e.message
exit(0)
class EventHandler(pyinotify.ProcessEvent):
"""事件处理"""
def __init__(self, monitered_dir, queue, bucket):
self.monitered_dir = monitered_dir
self.queue = queue
self.bucket = bucket
self.logger = logging.getLogger('app')
dbpath = 'db/ossync.db'
self.qm = queue_model.QueueModel(dbpath)
def process_IN_CREATE(self, event):
self.process_event(event, 'CREATE')
def process_IN_DELETE(self, event):
self.process_event(event, 'DELETE')
def process_IN_MODIFY(self, event):
self.process_event(event, 'MODIFY')
def process_IN_MOVED_FROM(self, event):
self.logger.info("Moved from file: %s " % os.path.join(event.path, event.name))
self.process_event(event, 'DELETE')
def process_IN_MOVED_TO(self, event):
self.logger.info("Moved to file: %s " % os.path.join(event.path, event.name))
realpath = os.path.join(event.path, event.name)
if event.dir:
self.queue_dir(realpath)
self.process_event(event, 'CREATE')
def process_event(self, event, action):
if len(action) == 0:
return False
realpath = os.path.join(event.path, event.name)
relpath = os.path.relpath(realpath, self.monitered_dir)
if action == 'DELETE':
if event.dir:
relpath += '/'
self.logger.info(action.title() + " file: %s " % realpath)
#print "Modify file: %s " % os.path.join(event.path, event.name)
el = self.bucket + '::' + self.monitered_dir + '::' + relpath + '::' + action[0]
self.save_el(self.monitered_dir, relpath, self.bucket, action[0])
self.queue_el(el)
def queue_dir(self, queue_path):
files = list(helper.walk_files(queue_path, yield_folders = True))
if len(files) > 0:
for path in files:
relpath = os.path.relpath(path, self.monitered_dir)
self.save_el(self.monitered_dir, relpath, self.bucket,'C')
el = self.bucket + '::' + self.monitered_dir + '::' + relpath + '::' + 'C'
self.queue_el(el)
def save_el(self, root, relpath, bucket, action):
hashcode = helper.calc_el_md5(root, relpath, bucket)
self.qm.open()
if self.is_el_existed(hashcode):
self.qm.update_action(hashcode, action)
self.qm.update_status(hashcode, 0)
else:
data={"root": root, "relpath": relpath, "bucket": bucket, "action": action, "status": 0, "retries": 0}
self.qm.save(data)
self.qm.close()
def is_el_existed(self, hashcode):
row = self.qm.get(hashcode)
if row:
return True
return False
def queue_el(self, el):
'''el: element of queue , formated as "bucket::root::path::C|M|D"
C means CREATE, M means MODIFY, D means DELETE
'''
try:
self.queue.put(el, block = True, timeout = 1)
msg = 'queue element:' + el
#print msg
self.logger.info(msg)
except Full as e:
#print e
self.logger.error(e.message)
class InotifyThread(threading.Thread):
def __init__(self, bucket, root, queue, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.bucket = bucket
self.queue = queue
self.root = root
self.logger = logging.getLogger('app')
self._terminate = False
def terminate(self):
self._terminate = True
self.notify.stop()
def start_notify(self, monitered_dir):
wm = pyinotify.WatchManager()
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
self.notifier = pyinotify.Notifier(wm, EventHandler(monitered_dir, self.queue, self.bucket), timeout = 10)
wm.add_watch(monitered_dir, mask, rec = True, auto_add = True)
self.logger.info('now starting monitor %s'%(monitered_dir))
# self.notifier.loop()
while True:
if self._terminate:
break
self.notifier.process_events()
if self.notifier.check_events():
self.notifier.read_events()
def run(self):
self.start_notify(self.root)
return
if __name__ == '__main__':
queue = Queue()
root = '.'
bucket = 'dzdata'
logger = logging.getLogger('app')
logger.setLevel(logging.INFO)
logger.addHandler(logging.FileHandler('logs/app.log'))
inotifythd = InotifyThread(bucket, root, queue)
inotifythd.start()