WATCHDOG(3) | watchdog | WATCHDOG(3) |
watchdog - watchdog Documentation
Python API library and shell utilities to monitor file system events.
Get started quickly with a simple example in quickstart.
You can use pip to install watchdog quickly and easily:
$ pip install watchdog
Need more help with installing? See installation.
watchdog requires Python 2.6 or above to work. If you are using a Linux/FreeBSD/Mac OS X system, you already have Python installed. However, you may wish to upgrade your system to Python 2.7 at least, because this version comes with updates that can reduce compatibility problems. See a list of Dependencies.
$ pip install watchdog
$ wget -c http://pypi.python.org/packages/source/w/watchdog/watchdog-0.9.0.tar.gz $ tar zxvf watchdog-0.9.0.tar.gz $ cd watchdog-0.9.0 $ python setup.py install
$ git clone --recursive git://github.com/gorakhargosh/watchdog.git $ cd watchdog $ python setup.py install
watchdog depends on many libraries to do its job. The following is a list of dependencies you need based on the operating system you are using.
Operating system Dependency (row) | Windows | Linux 2.6 | 0.0 Mac OS X/ Darwin 168u | BSD |
XCode | Yes | |||
PyYAML | Yes | Yes | Yes | Yes |
argh | Yes | Yes | Yes | Yes |
argparse | Yes | Yes | Yes | Yes |
select_backport (Python 2.6) | Yes | Yes | ||
pathtools | Yes | Yes | Yes | Yes |
The watchmedo script depends on PyYAML which links with LibYAML. On Mac OS X, you can use homebrew to install LibYAML:
brew install libyaml
On Linux, use your favorite package manager to install LibYAML. Here's how you do it on Ubuntu:
sudo aptitude install libyaml-dev
On Windows, please install PyYAML using the binaries they provide.
watchdog uses native APIs as much as possible falling back to polling the disk periodically to compare directory snapshots only when it cannot use an API natively-provided by the underlying operating system. The following operating systems are currently supported:
WARNING:
NOTE:
fs.inotify.max_user_watches=16384
watchdog can use whichever one is available, preferring FSEvents over kqueue(2). kqueue(2) uses open file descriptors for monitoring and the current implementation uses Mac OS X File System Monitoring Performance Guidelines to open these file descriptors only to monitor events, thus allowing OS X to unmount volumes that are being watched without locking them.
NOTE:
watchdog will automatically open file descriptors for all new files/directories created and close those for which are deleted.
NOTE:
You should ensure this limit is set to at least 1024 (or a value suitable to your usage). The following command appended to your ~/.profile configuration file does this for you:
ulimit -n 1024
NOTE:
NOTE:
Below we present a simple example that monitors the current directory recursively (which means, it will traverse any sub-directories) to detect changes. Here is what we will do with the API:
By default, an watchdog.observers.Observer instance will not monitor sub-directories. By passing recursive=True in the call to watchdog.observers.Observer.schedule() monitoring entire directory trees is ensured.
The following example program will monitor the current directory recursively for file system changes and simply log them to the console:
import sys import logging from watchdog.observers import Observer from watchdog.events import LoggingEventHandler if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while observer.isAlive():
observer.join(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
To stop the program, press Control-C.
Immutable type that represents a file system event that is triggered when a change occurs on the monitored file system.
All FileSystemEvent objects are required to be immutable and hence can be used as keys in dictionaries or be added to sets.
File system event representing any kind of file system movement.
File system event representing file movement on the file system.
File system event representing directory movement on the file system.
File system event representing file modification on the file system.
File system event representing directory modification on the file system.
File system event representing file creation on the file system.
File system event representing directory creation on the file system.
File system event representing file deletion on the file system.
File system event representing directory deletion on the file system.
Base file system event handler that you can override methods from.
Matches given patterns with file paths associated with occurring events.
Matches given regexes with file paths associated with occurring events.
Logs all the events captured.
An scheduled watch.
Thread-safe event queue based on a special queue that skips adding the same event (FileSystemEvent) multiple times consecutively. Thus avoiding dispatching multiple event handling calls when multiple identical events are produced quicker than an observer can consume them.
Producer thread base class subclassed by event emitters that generate events and populate a queue with them.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
Consumer thread base class subclassed by event observer threads that dispatch events from an event queue to appropriate event handlers.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
Base observer.
This method is called immediately after the thread is signaled to stop.
It must be called at most once per thread object. It arranges for the object's run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.
Observer thread that schedules watching directories and dispatches calls to event handlers.
You can also import platform specific classes directly and use it instead of Observer. Here is a list of implemented observer classes.:
Class | Platforms | Note |
inotify.InotifyObserver | Linux 2.6.13+ | inotify(7) based observer |
fsevents.FSEventsObserver | Mac OS X | FSEvents based observer |
kqueue.KqueueObserver | Mac OS X and BSD with kqueue(2) | kqueue(2) based observer |
read_directory_changes.WindowsApiObserver | MS Windows | Windows API-based observer |
polling.PollingObserver | Any | fallback implementation |
Platform-independent observer that polls a directory to detect file system changes.
File system independent observer that polls a directory to detect changes.
Convenience class for creating stoppable threads.
This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when no alive non-daemon threads are left.
This is a nonzero integer. See the get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.
This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.
This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads.
This blocks the calling thread until the thread whose join() method is called terminates -- either normally or through an unhandled exception or until the optional timeout occurs.
When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). As join() always returns None, you must call isAlive() after join() to decide whether a timeout happened -- if the thread is still alive, the join() call timed out.
When the timeout argument is not present or None, the operation will block until the thread terminates.
A thread can be join()ed many times.
join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.
It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.
This method is called right before this thread is started and this object’s run() method is invoked.
This method is called immediately after the thread is signaled to stop.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
It must be called at most once per thread object. It arranges for the object's run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.
This implementation does not take partition boundaries into consideration. It will only work when the directory tree is entirely on the same file system. More specifically, any part of the code that depends on inode numbers can break if partition boundaries are crossed. In these cases, the snapshot diff will represent file/directory movement as created and deleted events.
A snapshot of stat information of files in a directory.
Deprecated since version 0.7.2.
Use custom stat function that returns a stat structure for path. Currently only st_dev, st_ino, st_mode and st_mtime are needed.
A function with the signature walker_callback(path, stat_info) which will be called for every entry in the directory tree.
Attached information is subject to change. Do not use unless you specify stat in constructor. Use inode(), mtime(), isdir() instead.
Compares two directory snapshots and creates an object that represents the difference between the two snapshots.
Each event is a two-tuple the first item of which is the path that has been renamed to the second item in the tuple.
Each event is a two-tuple the first item of which is the path that has been renamed to the second item in the tuple.
Welcome hacker! So you have got something you would like to see in watchdog? Whee. This document will help you get started.
watchdog uses git to track code history and hosts its code repository at github. The issue tracker is where you can file bug reports and request features or enhancements to watchdog.
Ensure your system has the following programs and libraries installed before beginning to hack:
watchdog makes extensive use of zc.buildout to set up its work environment. You should get familiar with it.
Steps to setting up a clean environment:
$ git clone --recursive git@github.com:hackeratti/watchdog.git $ cd watchdog $ python tools/bootstrap.py --distribute $ bin/buildout
IMPORTANT:
That's it with the setup. Now you're ready to hack on watchdog.
The repository checkout contains a script called autobuild.sh which you must run prior to making changes. It will detect changes to Python source code or restructuredText documentation files anywhere in the directory tree and rebuild sphinx documentation, run all tests using nose, and generate coverage reports.
Start it by issuing this command in the watchdog directory checked out earlier:
$ tools/autobuild.sh ...
Happy hacking!
Found a bug in or want a feature added to watchdog? You can fork the official code repository or file an issue ticket at the issue tracker. You can also ask questions at the official mailing list. You may also want to refer to hacking for information about contributing code or documentation to watchdog.
Yesudeep Mangalapilly
2018, Yesudeep Mangalapilly
December 1, 2018 | 0.9.0 |