-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbMonitorLogged.py
180 lines (125 loc) · 5.33 KB
/
thumbMonitorLogged.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
"""
06/06/13
This file provides methods to monitor a file system to report the removed, added, and modified filepaths of the file system.
That information is then used to generate thumbnails of pictures(as recognized by PIL-python image library) that have been added to the filesystem
Note: No thumbnail is deleted, that is taken care of by another module during periodical checks.
First let's test the pyinotify...
PyInotify Events:
"""
import sys
import argparse
import os
import threading
import Image
import Queue
import smtplib
import time
import pyinotify
from pyinotify import WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
import pyinotify
from thumbnailer import Thumber
from datetime import datetime
class EventHandler(pyinotify.ProcessEvent):
def __init__(self, queue, log_rate, log_file):
self.queue = queue
self.log_rate = log_rate
self.log_file = log_file
self.log_count = 0
def process_IN_CREATE(self, event):
if os.path.isfile(event.pathname):
self.queue.put_nowait([event.pathname])
if self.log_count % self.log_rate == 0:
log(self.log_file, ("added %s to queue to add: " % [event.pathname, str(datetime.now())]))
elif os.path.isdir(event.pathname):
log(self.log_file, ("the path %s added to fs is not file" % [event.pathname, str(datetime.now())]))
else:
pass
self.log_count += 1
def process_IN_MOVED_TO(self, event):
if os.path.isfile(event.pathname):
try:
im = Image.open(event.pathname)
self.queue.put_nowait(event.pathname)
if self.log_count % self.log_rate == 0:
log(self.log_file, ("added %s to queue to add IN_MOVED_TO: " % [event.pathname, str(datetime.now())]))
except:
pass
elif os.path.isdir(event.pathname):
self.queue.put_nowait(event.pathname, True)
else:
pass
def process_IN_Q_OVERFLOW(self, event):
# The event queue overflowed:
# currently emailing karthik@silanano if an event queue overflow occurs
sender = '[email protected]'
receiver = '[email protected]'
message = " The pyinotify queue overflowed, will miss some paths!"
#credentials:
username = 'kartuppuluri'
password = 'nofearkfart'
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username, password)
server.sendmail(sender, receiver, message)
server.quit()
print "event queue overflowed and email sent to [email protected] ", datetime.now()
class MonitorThread(threading.Thread):
def __init__(self, queue, watch_directory, log_rate, log_file):
self.queue = queue
wm = pyinotify.WatchManager()
wm.add_watch(watch_directory, pyinotify.ALL_EVENTS, rec=True, auto_add=True)
event_handler = EventHandler(queue, log_rate, log_file)
self.notifier = pyinotify.Notifier(wm, event_handler)
threading.Thread.__init__(self) # needed for thread to instantiated
def run(self):
self.notifier.loop()
class ThumberThread(threading.Thread):
def __init__(self, queue, thumbnail_dir, log_rate, log_file, thumb_size, extension):
self.queue = queue
self.thumber = Thumber(thumb_size[0], thumb_size[1], thumbnail_dir, extension)
self.log_count = 0
self.log_rate = log_rate
self.log_file = log_file
threading.Thread.__init__(self) # needed for thread to be instantiated
def run(self):
while True:
while not self.queue.empty():
job = self.queue.get_nowait()
print "job pulled: ", job
if len(job) == 1: # It is a file
try:
self.thumber.thumb(job[0])
print "THUMBED THIS JOB!: ", job
except: # either pic deleted from or hasn't completely transfered to the file system.
pass
else:
print "this job is not a file, but a dir: ", [job[0]]
self.thumber.thumb_dir(job[0])
self.log(job)
self.log_count += 1
def log(self, path):
if self.log_count % self.log_rate == 0:
log(self.log_file, ("time taken to thumb path: ", [path, str(datetime.now())]))
"""
runs the monitoring through the monitor and the indexer threads
"""
def run_monitor(thumbnail_dir, monitor_dir):
queue = Queue.Queue()
monitor_thread = MonitorThread(queue, monitor_dir, 1)
thumber_thread = ThumberThread(queue, thumbnail_dir, 1)
monitor_thread.start()
thumber_thread.start()
def log(log_file, data):
if os.path.exists(log_file):
with open(log_file, 'a') as f:
f.write(str(data))
else:
print "there is no log file at the specified location: ", log_file
def main():
queue = Queue.Queue()
thumber_thread = ThumberThread(queue, '/home/labuser/thumb_dir', 1, '/home/labuser/thumb_log.txt', (500, 500), '.jpg')
monitor_thread = MonitorThread(queue, '/home/labuser/thumb_monitor', 1, '/home/labuser/monitor_log.txt') #ok
thumber_thread.start()
monitor_thread.start()
if __name__ == '__main__':
main()