inotify


Nameinotify JSON
Version 0.2.10 PyPI version JSON
download
home_pagehttps://github.com/dsoprea/PyInotify
SummaryAn adapter to Linux kernel support for inotify directory-watching.
upload_time2018-07-07 06:46:46
maintainer
docs_urlNone
authorDustin Oprea
requires_python
licenseGPL 2
keywords inotify
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            |Build\_Status|
|Coverage\_Status|

========
Overview
========

*inotify* functionality is available from the Linux kernel and allows you to register one or more directories for watching, and to simply block and wait for notification events. This is obviously far more efficient than polling one or more directories to determine if anything has changed. This is available in the Linux kernel as of version 2.6 .

We've designed this library to act as a generator. All you have to do is loop, and you'll see one event at a time and block in-between. After each cycle (all notified events were processed, or no events were received), you'll get a *None*. You may use this as an opportunity to perform other tasks, if your application is being primarily driven by *inotify* events. By default, we'll only block for one-second on queries to the kernel. This may be set to something else by passing a seconds-value into the constructor as *block_duration_s*.

**This project is unrelated to the *PyInotify* project that existed prior to this one (this project began in 2015). That project is defunct and no longer available.**


==========
Installing
==========

Install via *pip*::

    $ sudo pip install inotify


=======
Example
=======

Code for monitoring a simple, flat path (see "Recursive Watching" for watching a hierarchical structure)::

    import inotify.adapters

    def _main():
        i = inotify.adapters.Inotify()

        i.add_watch('/tmp')

        with open('/tmp/test_file', 'w'):
            pass

        for event in i.event_gen(yield_nones=False):
            (_, type_names, path, filename) = event

            print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(
                  path, filename, type_names))

    if __name__ == '__main__':
        _main()

Output::

    PATH=[/tmp] FILENAME=[test_file] EVENT_TYPES=['IN_MODIFY']
    PATH=[/tmp] FILENAME=[test_file] EVENT_TYPES=['IN_OPEN']
    PATH=[/tmp] FILENAME=[test_file] EVENT_TYPES=['IN_CLOSE_WRITE']
    ^CTraceback (most recent call last):
      File "inotify_test.py", line 18, in <module>
        _main()
      File "inotify_test.py", line 11, in _main
        for event in i.event_gen(yield_nones=False):
      File "/home/dustin/development/python/pyinotify/inotify/adapters.py", line 202, in event_gen
        events = self.__epoll.poll(block_duration_s)
    KeyboardInterrupt

Note that this works quite nicely, but, in the event that you don't want to be driven by the loop, you can also provide a timeout and then even flatten the output of the generator directly to a list::

    import inotify.adapters

    def _main():
        i = inotify.adapters.Inotify()

        i.add_watch('/tmp')

        with open('/tmp/test_file', 'w'):
            pass

        events = i.event_gen(yield_nones=False, timeout_s=1)
        events = list(events)

        print(events)

    if __name__ == '__main__':
        _main()

This will return everything that's happened since the last time you ran it (artificially formatted here)::

    [
        (_INOTIFY_EVENT(wd=1, mask=2, cookie=0, len=16), ['IN_MODIFY'], '/tmp', u'test_file'),
        (_INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16), ['IN_OPEN'], '/tmp', u'test_file'),
        (_INOTIFY_EVENT(wd=1, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], '/tmp', u'test_file')
    ]

**Note that the event-loop will automatically register new directories to be watched, so, if you will create new directories and then potentially delete them, between calls, and are only retrieving the events in batches (like above) then you might experience issues. See the parameters for `event_gen()` for options to handle this scenario.**


==================
Recursive Watching
==================

There is also the ability to add a recursive watch on a path.

Example::

    i = inotify.adapters.InotifyTree('/tmp/watch_tree')

    for event in i.event_gen():
        # Do stuff...

        pass

This will immediately recurse through the directory tree and add watches on all subdirectories. New directories will automatically have watches added for them and deleted directories will be cleaned-up.

The other differences from the standard functionality:

- You can't remove a watch since watches are automatically managed.
- Even if you provide a very restrictive mask that doesn't allow for directory create/delete events, the *IN_ISDIR*, *IN_CREATE*, and *IN_DELETE* flags will still be seen.


=====
Notes
=====

- **IMPORTANT:** Recursively monitoring paths is **not** a functionality provided by the kernel. Rather, we artificially implement it. As directory-created events are received, we create watches for the child directories on-the-fly. This means that there is potential for a race condition: if a directory is created and a file or directory is created inside before you (using the `event_gen()` loop) have a chance to observe it, then you are going to have a problem: If it is a file, then you will miss the events related to its creation, but, if it is a directory, then not only will you miss those creation events but this library will also miss them and not be able to add a watch for them. If you are dealing with a **large number of hierarchical directory creations** and have the ability to be aware new directories via a secondary channel with some lead time before any files are populated *into* them, you can take advantage of this and call `add_watch()` manually. In this case there is limited value in using `InotifyTree()`/`InotifyTree()` instead of just `Inotify()` but this choice is left to you.

- *epoll* is used to audit for *inotify* kernel events.

- **The earlier versions of this project had only partial Python 3 compatibility (string related). This required doing the string<->bytes conversions outside of this project. As of the current version, this has been fixed. However, this means that Python 3 users may experience breakages until this is compensated-for on their end. It will obviously be trivial for this project to detect the type of the arguments that are passed but there'd be no concrete way of knowing which type to return. Better to just fix it completely now and move forward.**

- You may also choose to pass the list of directories to watch via the *paths* parameter of the constructor. This would work best in situations where your list of paths is static.

- Calling `remove_watch()` is not strictly necessary. The *inotify* resources is automatically cleaned-up, which would clean-up all watch resources as well.


=======
Testing
=======

Call "test.sh" to run the tests::

    $ ./test.sh
    test__cycle (tests.test_inotify.TestInotify) ... ok
    test__get_event_names (tests.test_inotify.TestInotify) ... ok
    test__international_naming_python2 (tests.test_inotify.TestInotify) ... SKIP: Not in Python 2
    test__international_naming_python3 (tests.test_inotify.TestInotify) ... ok
    test__automatic_new_watches_on_existing_paths (tests.test_inotify.TestInotifyTree) ... ok
    test__automatic_new_watches_on_new_paths (tests.test_inotify.TestInotifyTree) ... ok
    test__cycle (tests.test_inotify.TestInotifyTree) ... ok
    test__renames (tests.test_inotify.TestInotifyTree) ... ok
    test__cycle (tests.test_inotify.TestInotifyTrees) ... ok

    ----------------------------------------------------------------------
    Ran 9 tests in 12.039s

    OK (SKIP=1)

.. |Build_Status| image:: https://travis-ci.org/dsoprea/PyInotify.svg?branch=master
   :target: https://travis-ci.org/dsoprea/PyInotify
.. |Coverage_Status| image:: https://coveralls.io/repos/github/dsoprea/PyInotify/badge.svg?branch=master
   :target: https://coveralls.io/github/dsoprea/PyInotify?branch=master



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dsoprea/PyInotify",
    "name": "inotify",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "inotify",
    "author": "Dustin Oprea",
    "author_email": "myselfasunder@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/35/cb/6d564f8a3f25d9516298dce151670d01e43a4b3b769c1c15f40453179cd5/inotify-0.2.10.tar.gz",
    "platform": "",
    "description": "|Build\\_Status|\n|Coverage\\_Status|\n\n========\nOverview\n========\n\n*inotify* functionality is available from the Linux kernel and allows you to register one or more directories for watching, and to simply block and wait for notification events. This is obviously far more efficient than polling one or more directories to determine if anything has changed. This is available in the Linux kernel as of version 2.6 .\n\nWe've designed this library to act as a generator. All you have to do is loop, and you'll see one event at a time and block in-between. After each cycle (all notified events were processed, or no events were received), you'll get a *None*. You may use this as an opportunity to perform other tasks, if your application is being primarily driven by *inotify* events. By default, we'll only block for one-second on queries to the kernel. This may be set to something else by passing a seconds-value into the constructor as *block_duration_s*.\n\n**This project is unrelated to the *PyInotify* project that existed prior to this one (this project began in 2015). That project is defunct and no longer available.**\n\n\n==========\nInstalling\n==========\n\nInstall via *pip*::\n\n    $ sudo pip install inotify\n\n\n=======\nExample\n=======\n\nCode for monitoring a simple, flat path (see \"Recursive Watching\" for watching a hierarchical structure)::\n\n    import inotify.adapters\n\n    def _main():\n        i = inotify.adapters.Inotify()\n\n        i.add_watch('/tmp')\n\n        with open('/tmp/test_file', 'w'):\n            pass\n\n        for event in i.event_gen(yield_nones=False):\n            (_, type_names, path, filename) = event\n\n            print(\"PATH=[{}] FILENAME=[{}] EVENT_TYPES={}\".format(\n                  path, filename, type_names))\n\n    if __name__ == '__main__':\n        _main()\n\nOutput::\n\n    PATH=[/tmp] FILENAME=[test_file] EVENT_TYPES=['IN_MODIFY']\n    PATH=[/tmp] FILENAME=[test_file] EVENT_TYPES=['IN_OPEN']\n    PATH=[/tmp] FILENAME=[test_file] EVENT_TYPES=['IN_CLOSE_WRITE']\n    ^CTraceback (most recent call last):\n      File \"inotify_test.py\", line 18, in <module>\n        _main()\n      File \"inotify_test.py\", line 11, in _main\n        for event in i.event_gen(yield_nones=False):\n      File \"/home/dustin/development/python/pyinotify/inotify/adapters.py\", line 202, in event_gen\n        events = self.__epoll.poll(block_duration_s)\n    KeyboardInterrupt\n\nNote that this works quite nicely, but, in the event that you don't want to be driven by the loop, you can also provide a timeout and then even flatten the output of the generator directly to a list::\n\n    import inotify.adapters\n\n    def _main():\n        i = inotify.adapters.Inotify()\n\n        i.add_watch('/tmp')\n\n        with open('/tmp/test_file', 'w'):\n            pass\n\n        events = i.event_gen(yield_nones=False, timeout_s=1)\n        events = list(events)\n\n        print(events)\n\n    if __name__ == '__main__':\n        _main()\n\nThis will return everything that's happened since the last time you ran it (artificially formatted here)::\n\n    [\n        (_INOTIFY_EVENT(wd=1, mask=2, cookie=0, len=16), ['IN_MODIFY'], '/tmp', u'test_file'),\n        (_INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16), ['IN_OPEN'], '/tmp', u'test_file'),\n        (_INOTIFY_EVENT(wd=1, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], '/tmp', u'test_file')\n    ]\n\n**Note that the event-loop will automatically register new directories to be watched, so, if you will create new directories and then potentially delete them, between calls, and are only retrieving the events in batches (like above) then you might experience issues. See the parameters for `event_gen()` for options to handle this scenario.**\n\n\n==================\nRecursive Watching\n==================\n\nThere is also the ability to add a recursive watch on a path.\n\nExample::\n\n    i = inotify.adapters.InotifyTree('/tmp/watch_tree')\n\n    for event in i.event_gen():\n        # Do stuff...\n\n        pass\n\nThis will immediately recurse through the directory tree and add watches on all subdirectories. New directories will automatically have watches added for them and deleted directories will be cleaned-up.\n\nThe other differences from the standard functionality:\n\n- You can't remove a watch since watches are automatically managed.\n- Even if you provide a very restrictive mask that doesn't allow for directory create/delete events, the *IN_ISDIR*, *IN_CREATE*, and *IN_DELETE* flags will still be seen.\n\n\n=====\nNotes\n=====\n\n- **IMPORTANT:** Recursively monitoring paths is **not** a functionality provided by the kernel. Rather, we artificially implement it. As directory-created events are received, we create watches for the child directories on-the-fly. This means that there is potential for a race condition: if a directory is created and a file or directory is created inside before you (using the `event_gen()` loop) have a chance to observe it, then you are going to have a problem: If it is a file, then you will miss the events related to its creation, but, if it is a directory, then not only will you miss those creation events but this library will also miss them and not be able to add a watch for them. If you are dealing with a **large number of hierarchical directory creations** and have the ability to be aware new directories via a secondary channel with some lead time before any files are populated *into* them, you can take advantage of this and call `add_watch()` manually. In this case there is limited value in using `InotifyTree()`/`InotifyTree()` instead of just `Inotify()` but this choice is left to you.\n\n- *epoll* is used to audit for *inotify* kernel events.\n\n- **The earlier versions of this project had only partial Python 3 compatibility (string related). This required doing the string<->bytes conversions outside of this project. As of the current version, this has been fixed. However, this means that Python 3 users may experience breakages until this is compensated-for on their end. It will obviously be trivial for this project to detect the type of the arguments that are passed but there'd be no concrete way of knowing which type to return. Better to just fix it completely now and move forward.**\n\n- You may also choose to pass the list of directories to watch via the *paths* parameter of the constructor. This would work best in situations where your list of paths is static.\n\n- Calling `remove_watch()` is not strictly necessary. The *inotify* resources is automatically cleaned-up, which would clean-up all watch resources as well.\n\n\n=======\nTesting\n=======\n\nCall \"test.sh\" to run the tests::\n\n    $ ./test.sh\n    test__cycle (tests.test_inotify.TestInotify) ... ok\n    test__get_event_names (tests.test_inotify.TestInotify) ... ok\n    test__international_naming_python2 (tests.test_inotify.TestInotify) ... SKIP: Not in Python 2\n    test__international_naming_python3 (tests.test_inotify.TestInotify) ... ok\n    test__automatic_new_watches_on_existing_paths (tests.test_inotify.TestInotifyTree) ... ok\n    test__automatic_new_watches_on_new_paths (tests.test_inotify.TestInotifyTree) ... ok\n    test__cycle (tests.test_inotify.TestInotifyTree) ... ok\n    test__renames (tests.test_inotify.TestInotifyTree) ... ok\n    test__cycle (tests.test_inotify.TestInotifyTrees) ... ok\n\n    ----------------------------------------------------------------------\n    Ran 9 tests in 12.039s\n\n    OK (SKIP=1)\n\n.. |Build_Status| image:: https://travis-ci.org/dsoprea/PyInotify.svg?branch=master\n   :target: https://travis-ci.org/dsoprea/PyInotify\n.. |Coverage_Status| image:: https://coveralls.io/repos/github/dsoprea/PyInotify/badge.svg?branch=master\n   :target: https://coveralls.io/github/dsoprea/PyInotify?branch=master\n\n\n",
    "bugtrack_url": null,
    "license": "GPL 2",
    "summary": "An adapter to Linux kernel support for inotify directory-watching.",
    "version": "0.2.10",
    "project_urls": {
        "Homepage": "https://github.com/dsoprea/PyInotify"
    },
    "split_keywords": [
        "inotify"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c7fc9728f1f708ecd5981007abe133d44fdcddf40915f8d13e12a140b77376ae",
                "md5": "2c088ccb0ee31be9ee26e6aeb78e88d1",
                "sha256": "397f8785450e41f606fe4eb6f5e8e0a1c70b354b56495225fc6c6fe7e07db0c9"
            },
            "downloads": -1,
            "filename": "inotify-0.2.10-py2-none-any.whl",
            "has_sig": false,
            "md5_digest": "2c088ccb0ee31be9ee26e6aeb78e88d1",
            "packagetype": "bdist_wheel",
            "python_version": "py2",
            "requires_python": null,
            "size": 17175,
            "upload_time": "2018-07-07T06:46:44",
            "upload_time_iso_8601": "2018-07-07T06:46:44.767989Z",
            "url": "https://files.pythonhosted.org/packages/c7/fc/9728f1f708ecd5981007abe133d44fdcddf40915f8d13e12a140b77376ae/inotify-0.2.10-py2-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "35cb6d564f8a3f25d9516298dce151670d01e43a4b3b769c1c15f40453179cd5",
                "md5": "33c7ee4a7cde60036a2d2a1a55c7c7c8",
                "sha256": "974a623a338482b62e16d4eb705fb863ed33ec178680fc3e96ccdf0df6c02a07"
            },
            "downloads": -1,
            "filename": "inotify-0.2.10.tar.gz",
            "has_sig": false,
            "md5_digest": "33c7ee4a7cde60036a2d2a1a55c7c7c8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9905,
            "upload_time": "2018-07-07T06:46:46",
            "upload_time_iso_8601": "2018-07-07T06:46:46.084659Z",
            "url": "https://files.pythonhosted.org/packages/35/cb/6d564f8a3f25d9516298dce151670d01e43a4b3b769c1c15f40453179cd5/inotify-0.2.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2018-07-07 06:46:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dsoprea",
    "github_project": "PyInotify",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "tox": true,
    "lcname": "inotify"
}
        
Elapsed time: 3.70486s