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