/usr/lib/python2.7/dist-packages/hupper/watchdog.py is in python-hupper 1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # check ``hupper.compat.is_watchdog_supported`` before using this module
from __future__ import absolute_import
import os.path
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from .interfaces import IFileMonitor
class WatchdogFileMonitor(FileSystemEventHandler, Observer, IFileMonitor):
"""
An :class:`hupper.interfaces.IFileMonitor` that uses ``watchdog``
to watch for file changes uses inotify.
``callback`` is a callable that accepts a path to a changed file.
"""
def __init__(self, callback):
super(WatchdogFileMonitor, self).__init__()
self.callback = callback
self.paths = set()
self.dirpaths = set()
self.lock = threading.Lock()
def add_path(self, path):
with self.lock:
dirpath = os.path.dirname(path)
if dirpath not in self.dirpaths:
try:
self.schedule(self, dirpath)
except (OSError, IOError): # pragma: no cover
# ideally we would handle this better but if the
# directory is missing watchdog raises an error
pass
else:
self.dirpaths.add(dirpath)
if path not in self.paths:
self.paths.add(path)
def on_any_event(self, event):
with self.lock:
path = event.src_path
if path in self.paths:
self.callback(path)
|