fsevents.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>
  5. # Copyright 2012 Google, Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. """
  19. :module: watchdog.observers.fsevents
  20. :synopsis: FSEvents based emitter implementation.
  21. :author: yesudeep@google.com (Yesudeep Mangalapilly)
  22. :platforms: Mac OS X
  23. """
  24. from __future__ import with_statement
  25. import sys
  26. import threading
  27. import unicodedata
  28. import _watchdog_fsevents as _fsevents
  29. from watchdog.events import (
  30. FileDeletedEvent,
  31. FileModifiedEvent,
  32. FileCreatedEvent,
  33. FileMovedEvent,
  34. DirDeletedEvent,
  35. DirModifiedEvent,
  36. DirCreatedEvent,
  37. DirMovedEvent
  38. )
  39. from watchdog.utils.dirsnapshot import DirectorySnapshot
  40. from watchdog.observers.api import (
  41. BaseObserver,
  42. EventEmitter,
  43. DEFAULT_EMITTER_TIMEOUT,
  44. DEFAULT_OBSERVER_TIMEOUT
  45. )
  46. class FSEventsEmitter(EventEmitter):
  47. """
  48. Mac OS X FSEvents Emitter class.
  49. :param event_queue:
  50. The event queue to fill with events.
  51. :param watch:
  52. A watch object representing the directory to monitor.
  53. :type watch:
  54. :class:`watchdog.observers.api.ObservedWatch`
  55. :param timeout:
  56. Read events blocking timeout (in seconds).
  57. :type timeout:
  58. ``float``
  59. """
  60. def __init__(self, event_queue, watch, timeout=DEFAULT_EMITTER_TIMEOUT):
  61. EventEmitter.__init__(self, event_queue, watch, timeout)
  62. self._lock = threading.Lock()
  63. self.snapshot = DirectorySnapshot(watch.path, watch.is_recursive)
  64. def on_thread_stop(self):
  65. if self.watch:
  66. _fsevents.remove_watch(self.watch)
  67. _fsevents.stop(self)
  68. self._watch = None
  69. def queue_events(self, timeout):
  70. with self._lock:
  71. if (not self.watch.is_recursive
  72. and self.watch.path not in self.pathnames):
  73. return
  74. new_snapshot = DirectorySnapshot(self.watch.path,
  75. self.watch.is_recursive)
  76. events = new_snapshot - self.snapshot
  77. self.snapshot = new_snapshot
  78. # Files.
  79. for src_path in events.files_deleted:
  80. self.queue_event(FileDeletedEvent(src_path))
  81. for src_path in events.files_modified:
  82. self.queue_event(FileModifiedEvent(src_path))
  83. for src_path in events.files_created:
  84. self.queue_event(FileCreatedEvent(src_path))
  85. for src_path, dest_path in events.files_moved:
  86. self.queue_event(FileMovedEvent(src_path, dest_path))
  87. # Directories.
  88. for src_path in events.dirs_deleted:
  89. self.queue_event(DirDeletedEvent(src_path))
  90. for src_path in events.dirs_modified:
  91. self.queue_event(DirModifiedEvent(src_path))
  92. for src_path in events.dirs_created:
  93. self.queue_event(DirCreatedEvent(src_path))
  94. for src_path, dest_path in events.dirs_moved:
  95. self.queue_event(DirMovedEvent(src_path, dest_path))
  96. def run(self):
  97. try:
  98. def callback(pathnames, flags, emitter=self):
  99. emitter.queue_events(emitter.timeout)
  100. # for pathname, flag in zip(pathnames, flags):
  101. # if emitter.watch.is_recursive: # and pathname != emitter.watch.path:
  102. # new_sub_snapshot = DirectorySnapshot(pathname, True)
  103. # old_sub_snapshot = self.snapshot.copy(pathname)
  104. # diff = new_sub_snapshot - old_sub_snapshot
  105. # self.snapshot += new_subsnapshot
  106. # else:
  107. # new_snapshot = DirectorySnapshot(emitter.watch.path, False)
  108. # diff = new_snapshot - emitter.snapshot
  109. # emitter.snapshot = new_snapshot
  110. # INFO: FSEvents reports directory notifications recursively
  111. # by default, so we do not need to add subdirectory paths.
  112. # pathnames = set([self.watch.path])
  113. # if self.watch.is_recursive:
  114. # for root, directory_names, _ in os.walk(self.watch.path):
  115. # for directory_name in directory_names:
  116. # full_path = absolute_path(
  117. # os.path.join(root, directory_name))
  118. # pathnames.add(full_path)
  119. self.pathnames = [self.watch.path]
  120. _fsevents.add_watch(self,
  121. self.watch,
  122. callback,
  123. self.pathnames)
  124. _fsevents.read_events(self)
  125. except Exception:
  126. pass
  127. class FSEventsObserver(BaseObserver):
  128. def __init__(self, timeout=DEFAULT_OBSERVER_TIMEOUT):
  129. BaseObserver.__init__(self, emitter_class=FSEventsEmitter,
  130. timeout=timeout)
  131. def schedule(self, event_handler, path, recursive=False):
  132. # Python 2/3 compat
  133. try:
  134. str_class = unicode
  135. except NameError:
  136. str_class = str
  137. # Fix for issue #26: Trace/BPT error when given a unicode path
  138. # string. https://github.com/gorakhargosh/watchdog/issues#issue/26
  139. if isinstance(path, str_class):
  140. # path = unicode(path, 'utf-8')
  141. path = unicodedata.normalize('NFC', path)
  142. # We only encode the path in Python 2 for backwards compatibility.
  143. # On Python 3 we want the path to stay as unicode if possible for
  144. # the sake of path matching not having to be rewritten to use the
  145. # bytes API instead of strings. The _watchdog_fsevent.so code for
  146. # Python 3 can handle both str and bytes paths, which is why we
  147. # do not HAVE to encode it with Python 3. The Python 2 code in
  148. # _watchdog_fsevents.so was not changed for the sake of backwards
  149. # compatibility.
  150. if sys.version_info < (3,):
  151. path = path.encode('utf-8')
  152. return BaseObserver.schedule(self, event_handler, path, recursive)