README.rst
1Watchdog
2========
3
4|Build Status|
5|CirrusCI Status|
6
7Python API and shell utilities to monitor file system events.
8
9Works on 3.9+.
10
11Example API Usage
12-----------------
13
14A simple program that uses watchdog to monitor directories specified
15as command-line arguments and logs events generated:
16
17.. code-block:: python
18
19 import time
20
21 from watchdog.events import FileSystemEvent, FileSystemEventHandler
22 from watchdog.observers import Observer
23
24
25 class MyEventHandler(FileSystemEventHandler):
26 def on_any_event(self, event: FileSystemEvent) -> None:
27 print(event)
28
29
30 event_handler = MyEventHandler()
31 observer = Observer()
32 observer.schedule(event_handler, ".", recursive=True)
33 observer.start()
34 try:
35 while True:
36 time.sleep(1)
37 finally:
38 observer.stop()
39 observer.join()
40
41
42Shell Utilities
43---------------
44
45Watchdog comes with an *optional* utility script called ``watchmedo``.
46Please type ``watchmedo --help`` at the shell prompt to
47know more about this tool.
48
49Here is how you can log the current directory recursively
50for events related only to ``*.py`` and ``*.txt`` files while
51ignoring all directory events:
52
53.. code-block:: bash
54
55 watchmedo log \
56 --patterns='*.py;*.txt' \
57 --ignore-directories \
58 --recursive \
59 --verbose \
60 .
61
62You can use the ``shell-command`` subcommand to execute shell commands in
63response to events:
64
65.. code-block:: bash
66
67 watchmedo shell-command \
68 --patterns='*.py;*.txt' \
69 --recursive \
70 --command='echo "${watch_src_path}"' \
71 .
72
73Please see the help information for these commands by typing:
74
75.. code-block:: bash
76
77 watchmedo [command] --help
78
79
80About ``watchmedo`` Tricks
81~~~~~~~~~~~~~~~~~~~~~~~~~~
82
83``watchmedo`` can read ``tricks.yaml`` files and execute tricks within them in
84response to file system events. Tricks are actually event handlers that
85subclass ``watchdog.tricks.Trick`` and are written by plugin authors. Trick
86classes are augmented with a few additional features that regular event handlers
87don't need.
88
89An example ``tricks.yaml`` file:
90
91.. code-block:: yaml
92
93 tricks:
94 - watchdog.tricks.LoggerTrick:
95 patterns: ["*.py", "*.js"]
96 - watchmedo_webtricks.GoogleClosureTrick:
97 patterns: ['*.js']
98 hash_names: true
99 mappings_format: json # json|yaml|python
100 mappings_module: app/javascript_mappings
101 suffix: .min.js
102 compilation_level: advanced # simple|advanced
103 source_directory: app/static/js/
104 destination_directory: app/public/js/
105 files:
106 index-page:
107 - app/static/js/vendor/jquery*.js
108 - app/static/js/base.js
109 - app/static/js/index-page.js
110 about-page:
111 - app/static/js/vendor/jquery*.js
112 - app/static/js/base.js
113 - app/static/js/about-page/**/*.js
114
115The directory containing the ``tricks.yaml`` file will be monitored. Each trick
116class is initialized with its corresponding keys in the ``tricks.yaml`` file as
117arguments and events are fed to an instance of this class as they arrive.
118
119Installation
120------------
121Install from PyPI using ``pip``:
122
123.. code-block:: bash
124
125 $ python -m pip install -U watchdog
126
127 # or to install the watchmedo utility:
128 $ python -m pip install -U "watchdog[watchmedo]"
129
130Install from source:
131
132.. code-block:: bash
133
134 $ python -m pip install -e .
135
136 # or to install the watchmedo utility:
137 $ python -m pip install -e '.[watchmedo]'
138
139
140Documentation
141-------------
142
143You can browse the latest release documentation_ online.
144
145Contribute
146----------
147
148Fork the `repository`_ on GitHub and send a pull request, or file an issue
149ticket at the `issue tracker`_. For general help and questions use
150`stackoverflow`_ with tag `python-watchdog`.
151
152Create and activate your virtual environment, then::
153
154 python -m pip install tox
155 python -m tox [-q] [-e ENV]
156
157If you are making a substantial change, add an entry to the "Unreleased" section
158of the `changelog`_.
159
160Supported Platforms
161-------------------
162
163* Linux 2.6 (inotify)
164* macOS (FSEvents, kqueue)
165* FreeBSD/BSD (kqueue)
166* Windows (ReadDirectoryChangesW with I/O completion ports;
167 ReadDirectoryChangesW worker threads)
168* OS-independent (polling the disk for directory snapshots and comparing them
169 periodically; slow and not recommended)
170
171Note that when using watchdog with kqueue, you need the
172number of file descriptors allowed to be opened by programs
173running on your system to be increased to more than the
174number of files that you will be monitoring. The easiest way
175to do that is to edit your ``~/.profile`` file and add
176a line similar to::
177
178 ulimit -n 1024
179
180This is an inherent problem with kqueue because it uses
181file descriptors to monitor files. That plus the enormous
182amount of bookkeeping that watchdog needs to do in order
183to monitor file descriptors just makes this a painful way
184to monitor files and directories. In essence, kqueue is
185not a very scalable way to monitor a deeply nested
186directory of files and directories with a large number of
187files.
188
189About using watchdog with editors like Vim
190------------------------------------------
191
192Vim does not modify files unless directed to do so.
193It creates backup files and then swaps them in to replace
194the files you are editing on the disk. This means that
195if you use Vim to edit your files, the on-modified events
196for those files will not be triggered by watchdog.
197You may need to configure Vim appropriately to disable
198this feature.
199
200
201About using watchdog with CIFS
202------------------------------
203
204When you want to watch changes in CIFS, you need to explicitly tell watchdog to
205use ``PollingObserver``, that is, instead of letting watchdog decide an
206appropriate observer like in the example above, do::
207
208 from watchdog.observers.polling import PollingObserver as Observer
209
210
211Dependencies
212------------
213
2141. Python 3.9 or above.
2152. XCode_ (only on macOS when installing from sources)
2163. PyYAML_ (only for ``watchmedo``)
217
218Licensing
219---------
220
221Watchdog is licensed under the terms of the `Apache License, version 2.0`_.
222
223- Copyright 2018-2024 Mickaël Schoentgen & contributors
224- Copyright 2014-2018 Thomas Amland & contributors
225- Copyright 2012-2014 Google, Inc.
226- Copyright 2011-2012 Yesudeep Mangalapilly
227
228Project `source code`_ is available at Github. Please report bugs and file
229enhancement requests at the `issue tracker`_.
230
231Why Watchdog?
232-------------
233
234Too many people tried to do the same thing and none did what I needed Python
235to do:
236
237* pnotify_
238* `unison fsmonitor`_
239* fsmonitor_
240* guard_
241* pyinotify_
242* `inotify-tools`_
243* jnotify_
244* treewatcher_
245* `file.monitor`_
246* pyfilesystem_
247
248.. links:
249.. _Yesudeep Mangalapilly: [email protected]
250.. _source code: https://github.com/gorakhargosh/watchdog
251.. _issue tracker: https://github.com/gorakhargosh/watchdog/issues
252.. _Apache License, version 2.0: https://www.apache.org/licenses/LICENSE-2.0
253.. _documentation: https://python-watchdog.readthedocs.io/
254.. _stackoverflow: https://stackoverflow.com/questions/tagged/python-watchdog
255.. _repository: https://github.com/gorakhargosh/watchdog
256.. _issue tracker: https://github.com/gorakhargosh/watchdog/issues
257.. _changelog: https://github.com/gorakhargosh/watchdog/blob/master/changelog.rst
258
259.. _PyYAML: https://www.pyyaml.org/
260.. _XCode: https://developer.apple.com/technologies/tools/xcode.html
261
262.. _pnotify: http://mark.heily.com/pnotify
263.. _unison fsmonitor: https://webdav.seas.upenn.edu/viewvc/unison/trunk/src/fsmonitor.py?view=markup&pathrev=471
264.. _fsmonitor: https://github.com/shaurz/fsmonitor
265.. _guard: https://github.com/guard/guard
266.. _pyinotify: https://github.com/seb-m/pyinotify
267.. _inotify-tools: https://github.com/rvoicilas/inotify-tools
268.. _jnotify: http://jnotify.sourceforge.net/
269.. _treewatcher: https://github.com/jbd/treewatcher
270.. _file.monitor: https://github.com/pke/file.monitor
271.. _pyfilesystem: https://github.com/PyFilesystem/pyfilesystem
272
273.. |Build Status| image:: https://github.com/gorakhargosh/watchdog/workflows/Tests/badge.svg
274 :target: https://github.com/gorakhargosh/watchdog/actions?query=workflow%3ATests
275.. |CirrusCI Status| image:: https://api.cirrus-ci.com/github/gorakhargosh/watchdog.svg
276 :target: https://cirrus-ci.com/github/gorakhargosh/watchdog/
277