-
-
Notifications
You must be signed in to change notification settings - Fork 704
/
fsevents.py
339 lines (267 loc) · 13.3 KB
/
fsevents.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
# coding: utf-8
#
# Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
# Copyright 2012 Google, Inc & contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
:module: watchdog.observers.fsevents
:synopsis: FSEvents based emitter implementation.
:author: yesudeep@google.com (Yesudeep Mangalapilly)
:author: contact@tiger-222.fr (Mickaël Schoentgen)
:platforms: Mac OS X
"""
import time
import logging
import os
import threading
import unicodedata
import _watchdog_fsevents as _fsevents
from watchdog.events import (
FileDeletedEvent,
FileModifiedEvent,
FileCreatedEvent,
FileMovedEvent,
DirDeletedEvent,
DirModifiedEvent,
DirCreatedEvent,
DirMovedEvent,
generate_sub_created_events,
generate_sub_moved_events
)
from watchdog.observers.api import (
BaseObserver,
EventEmitter,
DEFAULT_EMITTER_TIMEOUT,
DEFAULT_OBSERVER_TIMEOUT
)
from watchdog.utils.dirsnapshot import DirectorySnapshot
logger = logging.getLogger('fsevents')
class FSEventsEmitter(EventEmitter):
"""
Mac OS X FSEvents Emitter class.
:param event_queue:
The event queue to fill with events.
:param watch:
A watch object representing the directory to monitor.
:type watch:
:class:`watchdog.observers.api.ObservedWatch`
:param timeout:
Read events blocking timeout (in seconds).
:param suppress_history:
The FSEvents API may emit historic events up to 30 sec before the watch was
started. When ``suppress_history`` is ``True``, those events will be suppressed
by creating a directory snapshot of the watched path before starting the stream
as a reference to suppress old events. Warning: This may result in significant
memory usage in case of a large number of items in the watched path.
:type timeout:
``float``
"""
def __init__(self, event_queue, watch, timeout=DEFAULT_EMITTER_TIMEOUT, suppress_history=False):
EventEmitter.__init__(self, event_queue, watch, timeout)
self._fs_view = set()
self.suppress_history = suppress_history
self._start_time = 0.0
self._starting_state = None
self._lock = threading.Lock()
def on_thread_stop(self):
if self.watch:
_fsevents.remove_watch(self.watch)
_fsevents.stop(self)
self._watch = None
def queue_event(self, event):
# fsevents defaults to be recursive, so if the watch was meant to be non-recursive then we need to drop
# all the events here which do not have a src_path / dest_path that matches the watched path
if self._watch.is_recursive:
logger.debug("queue_event %s", event)
EventEmitter.queue_event(self, event)
else:
if not self._is_recursive_event(event):
logger.debug("queue_event %s", event)
EventEmitter.queue_event(self, event)
else:
logger.debug("drop event %s", event)
def _is_recursive_event(self, event):
src_path = event.src_path if event.is_directory else os.path.dirname(event.src_path)
if src_path == self._watch.path:
return False
if isinstance(event, (FileMovedEvent, DirMovedEvent)):
# when moving something into the watch path we must always take the dirname,
# otherwise we miss out on `DirMovedEvent`s
dest_path = os.path.dirname(event.dest_path)
if dest_path == self._watch.path:
return False
return True
def _queue_created_event(self, event, src_path, dirname):
cls = DirCreatedEvent if event.is_directory else FileCreatedEvent
self.queue_event(cls(src_path))
self.queue_event(DirModifiedEvent(dirname))
def _queue_deleted_event(self, event, src_path, dirname):
cls = DirDeletedEvent if event.is_directory else FileDeletedEvent
self.queue_event(cls(src_path))
self.queue_event(DirModifiedEvent(dirname))
def _queue_modified_event(self, event, src_path, dirname):
cls = DirModifiedEvent if event.is_directory else FileModifiedEvent
self.queue_event(cls(src_path))
def _queue_renamed_event(self, src_event, src_path, dst_path, src_dirname, dst_dirname):
cls = DirMovedEvent if src_event.is_directory else FileMovedEvent
dst_path = self._encode_path(dst_path)
self.queue_event(cls(src_path, dst_path))
self.queue_event(DirModifiedEvent(src_dirname))
self.queue_event(DirModifiedEvent(dst_dirname))
def _is_historic_created_event(self, event):
# We only queue a created event if the item was created after we
# started the FSEventsStream.
in_history = event.inode in self._fs_view
if self._starting_state:
try:
old_inode = self._starting_state.inode(event.path)[0]
before_start = old_inode == event.inode
except KeyError:
before_start = False
else:
before_start = False
return in_history or before_start
def queue_events(self, timeout, events):
if logger.getEffectiveLevel() <= logging.DEBUG:
for event in events:
flags = ", ".join(attr for attr in dir(event) if getattr(event, attr) is True)
logger.debug(f"{event}: {flags}")
if time.monotonic() - self._start_time > 60:
# Event history is no longer needed, let's free some memory.
self._starting_state = None
while events:
event = events.pop(0)
src_path = self._encode_path(event.path)
src_dirname = os.path.dirname(src_path)
try:
stat = os.stat(src_path)
except OSError:
stat = None
exists = stat and stat.st_ino == event.inode
# FSevents may coalesce multiple events for the same item + path into a
# single event. However, events are never coalesced for different items at
# the same path or for the same item at different paths. Therefore, the
# event chains "removed -> created" and "created -> renamed -> removed" will
# never emit a single native event and a deleted event *always* means that
# the item no longer existed at the end of the event chain.
# Some events will have a spurious `is_created` flag set, coalesced from an
# already emitted and processed CreatedEvent. To filter those, we keep track
# of all inodes which we know to be already created. This is safer than
# keeping track of paths since paths are more likely to be reused than
# inodes.
# Likewise, some events will have a spurious `is_modified`,
# `is_inode_meta_mod` or `is_xattr_mod` flag set. We currently do not
# suppress those but could do so if the item still exists by caching the
# stat result and verifying that it did change.
if event.is_created and event.is_removed:
# Events will only be coalesced for the same item / inode.
# The sequence deleted -> created therefore cannot occur.
# Any combination with renamed cannot occur either.
if not self._is_historic_created_event(event):
self._queue_created_event(event, src_path, src_dirname)
self._fs_view.add(event.inode)
if event.is_modified or event.is_inode_meta_mod or event.is_xattr_mod:
self._queue_modified_event(event, src_path, src_dirname)
self._queue_deleted_event(event, src_path, src_dirname)
self._fs_view.discard(event.inode)
else:
if event.is_created and not self._is_historic_created_event(event):
self._queue_created_event(event, src_path, src_dirname)
self._fs_view.add(event.inode)
if event.is_modified or event.is_inode_meta_mod or event.is_xattr_mod:
self._queue_modified_event(event, src_path, src_dirname)
if event.is_renamed:
# Check if we have a corresponding destination event in the watched path.
dst_event = next(iter(e for e in events if e.is_renamed and e.inode == event.inode), None)
if dst_event:
# Item was moved within the watched folder.
logger.debug("Destination event for rename is %s", dst_event)
dst_path = self._encode_path(dst_event.path)
dst_dirname = os.path.dirname(dst_path)
self._queue_renamed_event(event, src_path, dst_path, src_dirname, dst_dirname)
self._fs_view.add(event.inode)
for sub_event in generate_sub_moved_events(src_path, dst_path):
self.queue_event(sub_event)
# Process any coalesced flags for the dst_event.
events.remove(dst_event)
if dst_event.is_modified or dst_event.is_inode_meta_mod or dst_event.is_xattr_mod:
self._queue_modified_event(dst_event, dst_path, dst_dirname)
if dst_event.is_removed:
self._queue_deleted_event(dst_event, dst_path, dst_dirname)
self._fs_view.discard(dst_event.inode)
elif exists:
# This is the destination event, item was moved into the watched
# folder.
self._queue_created_event(event, src_path, src_dirname)
self._fs_view.add(event.inode)
for sub_event in generate_sub_created_events(src_path):
self.queue_event(sub_event)
else:
# This is the source event, item was moved out of the watched
# folder.
self._queue_deleted_event(event, src_path, src_dirname)
self._fs_view.discard(event.inode)
# Skip further coalesced processing.
continue
if event.is_removed:
# Won't occur together with renamed.
self._queue_deleted_event(event, src_path, src_dirname)
self._fs_view.discard(event.inode)
if event.is_root_changed:
# This will be set if root or any of its parents is renamed or deleted.
# TODO: find out new path and generate DirMovedEvent?
self.queue_event(DirDeletedEvent(self.watch.path))
logger.debug("Stopping because root path was changed")
self.stop()
self._fs_view.clear()
def run(self):
try:
def callback(paths, inodes, flags, ids, emitter=self):
try:
with emitter._lock:
events = [
_fsevents.NativeEvent(path, inode, event_flags, event_id)
for path, inode, event_flags, event_id in zip(paths, inodes, flags, ids)
]
emitter.queue_events(emitter.timeout, events)
except Exception:
logger.exception("Unhandled exception in fsevents callback")
self.pathnames = [self.watch.path]
self._start_time = time.monotonic()
_fsevents.add_watch(self, self.watch, callback, self.pathnames)
_fsevents.read_events(self)
except Exception:
logger.exception("Unhandled exception in FSEventsEmitter")
def on_thread_start(self):
if self.suppress_history:
if isinstance(self.watch.path, bytes):
watch_path = os.fsdecode(self.watch.path)
else:
watch_path = self.watch.path
self._starting_state = DirectorySnapshot(watch_path)
def _encode_path(self, path):
"""Encode path only if bytes were passed to this emitter. """
if isinstance(self.watch.path, bytes):
return os.fsencode(path)
return path
class FSEventsObserver(BaseObserver):
def __init__(self, timeout=DEFAULT_OBSERVER_TIMEOUT):
BaseObserver.__init__(self, emitter_class=FSEventsEmitter,
timeout=timeout)
def schedule(self, event_handler, path, recursive=False):
# Fix for issue #26: Trace/BPT error when given a unicode path
# string. https://github.com/gorakhargosh/watchdog/issues#issue/26
if isinstance(path, str):
path = unicodedata.normalize('NFC', path)
return BaseObserver.schedule(self, event_handler, path, recursive)