forked from ome/openmicroscopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsWin-XP-Monitor.py
211 lines (169 loc) · 8.3 KB
/
fsWin-XP-Monitor.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
OMERO.fs Monitor module for Window XP.
Copyright 2009 University of Dundee. All rights reserved.
Use is subject to license terms supplied in LICENSE.txt
"""
import logging
import threading, Queue
import os, sys, traceback
import time
# Third party path package. It provides much of the
# functionality of os.path but without the complexity.
# Imported as pathModule to avoid clashes.
import path as pathModule
import omero.all
import omero.grid.monitors as monitors
from fsAbstractPlatformMonitor import AbstractPlatformMonitor
import win32file
import win32con
class PlatformMonitor(AbstractPlatformMonitor):
"""
A Thread to monitor a path.
:group Constructor: __init__
:group Other methods: run, stop
"""
def __init__(self, eventTypes, pathMode, pathString, whitelist, blacklist, ignoreSysFiles, ignoreDirEvents, proxy):
"""
Set-up Monitor thread.
After initialising the superclass and some instance variables
try to create an FSEventStream. Throw an exeption if this fails.
:Parameters:
eventTypes :
A list of the event types to be monitored.
pathMode :
The mode of directory monitoring: flat, recursive or following.
pathString : string
A string representing a path to be monitored.
whitelist : list<string>
A list of files and extensions of interest.
blacklist : list<string>
A list of subdirectories to be excluded.
ignoreSysFiles :
If true platform dependent sys files should be ignored.
monitorId :
Unique id for the monitor included in callbacks.
proxy :
A proxy to be informed of events
"""
AbstractPlatformMonitor.__init__(self, eventTypes, pathMode, pathString, whitelist, blacklist, ignoreSysFiles, ignoreDirEvents, proxy)
self.log = logging.getLogger("fsserver."+__name__)
self.actions = {
1 : monitors.EventType.Create, # Created
2 : monitors.EventType.Delete, # Deleted
3 : monitors.EventType.Modify, # Updated
4 : monitors.EventType.Modify, # Renamed to something
5 : monitors.EventType.Modify # Renamed from something
}
self.recurse = not (self.pathMode == "Flat")
self.event = threading.Event()
self.log.info('Monitor set-up on %s', str(self.pathsToMonitor))
self.log.info('Monitoring %s events', str(self.eTypes))
def run(self):
"""
Start monitoring.
:return: No explicit return value.
"""
# Blocks
self.watch() # for now
def stop(self):
"""
Stop monitoring
:return: No explicit return value.
"""
self.event.set()
def watch(self):
"""
Create a monitor on created files.
Callback on file events.
"""
FILE_LIST_DIRECTORY = 0x0001
hDir = win32file.CreateFile (
self.pathsToMonitor,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None)
while not self.event.isSet():
results = win32file.ReadDirectoryChangesW (
hDir,
4096,
self.recurse, # recurse
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None,
None)
eventList = []
if results:
for action, file in results:
filename = os.path.join(self.pathsToMonitor, file)
self.log.info("Event : %s %s", str(self.actions[action]), filename)
if self.ignoreDirEvents and pathModule.path(filename).isdir():
self.log.info('Directory event, not propagated.')
else:
if action == 1:
if "Creation" in self.eTypes:
# Ignore default name for GUI created folders.
if self.ignoreSysFiles and filename.find('New Folder') >= 0:
self.log.info('Created "New Folder" ignored.')
else:
eventType = self.actions[action]
# Should have richer filename matching here.
if (len(self.whitelist) == 0) or (pathModule.path(filename).ext in self.whitelist):
eventList.append((filename.replace('\\\\','\\').replace('\\','/'), eventType))
else:
self.log.info('Not propagated.')
elif action == 2:
if "Deletion" in self.eTypes:
# Ignore default name for GUI created folders.
if self.ignoreSysFiles and filename.find('New Folder') >= 0:
self.log.info('Deleted "New Folder" ignored.')
else:
eventType = self.actions[action]
# Should have richer filename matching here.
if (len(self.whitelist) == 0) or (pathModule.path(filename).ext in self.whitelist):
eventList.append((filename.replace('\\\\','\\').replace('\\','/'), eventType))
else:
self.log.info('Not propagated.')
elif action in (3,4,5):
if "Modification" in self.eTypes:
# Ignore default name for GUI created folders.
if self.ignoreSysFiles and filename.find('New Folder') >= 0:
self.log.info('Modified "New Folder" ignored.')
else:
eventType = self.actions[action]
# Should have richer filename matching here.
if (len(self.whitelist) == 0) or (pathModule.path(filename).ext in self.whitelist):
eventList.append((filename.replace('\\\\','\\').replace('\\','/'), eventType))
else:
self.log.info('Not propagated.')
else:
self.log.error('Unknown event type.')
self.propagateEvents(eventList)
if __name__ == "__main__":
class Proxy(object):
def callback(self, eventList):
for event in eventList:
#pass
log.info("EVENT_RECORD::%s::%s::%s" % (time.time(), event[1], event[0]))
import logging
from logging import handlers
log = logging.getLogger("fstestserver")
file_handler = logging.FileHandler("/TEST/logs/fstestserver.out")
file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s: %(name)s - %(message)s"))
log.addHandler(file_handler)
log.setLevel(logging.INFO)
log = logging.getLogger("fstestserver."+__name__)
p = Proxy()
m = PlatformMonitor([monitors.WatchEventType.Creation,monitors.WatchEventType.Modification], monitors.PathMode.Follow, "C:\OMERO\DropBox", [], [], True, True, p)
try:
m.start()
except:
m.stop()