Ticket #9722: 0002-Add-support-for-pyinotify.patch

File 0002-Add-support-for-pyinotify.patch, 3.2 KB (added by Chris Lamb, 16 years ago)

Update 0002-Add-support-for-pyinotify.patch

  • AUTHORS

    From af5721c90929cac978001e191e7957465d5acb20 Mon Sep 17 00:00:00 2001
    From: Chris Lamb <lamby@debian.org>
    Date: Thu, 5 Feb 2009 12:00:55 +0000
    Subject: [PATCH] Add support for pyinotify.
    
    ---
     AUTHORS                    |    1 +
     django/utils/autoreload.py |   57 ++++++++++++++++++++++++++++++++++++++++++-
     2 files changed, 56 insertions(+), 2 deletions(-)
    
    diff --git a/AUTHORS b/AUTHORS
    index af45060..2cdfd82 100644
    a b answer newbie questions, and generally made Django that much better:  
    439439    Mykola Zamkovoi <nickzam@gmail.com>
    440440    Jarek Zgoda <jarek.zgoda@gmail.com>
    441441    Cheng Zhang
     442    Chris Lamb <lamby@debian.org>
    442443
    443444A big THANK YOU goes to:
    444445
  • django/utils/autoreload.py

    diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
    index 4dbc945..6afb58e 100644
    a b  
    2828# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    2929# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    3030
    31 import os, sys, time
     31import os, sys, time, datetime
    3232
    3333try:
    3434    import thread
    try:  
    4242except ImportError:
    4343    pass
    4444
     45USE_INOTIFY = False
     46try:
     47    # Test whether inotify is enabled and likely to work
     48    from pyinotify import WatchManager, Notifier, EventsCodes, inotify_init
     49
     50    fd = inotify_init()
     51    if fd != -1:
     52        USE_INOTIFY = True
     53        os.close(fd)
     54except ImportError:
     55    pass
    4556
    4657RUN_RELOADER = True
    4758
    4859_mtimes = {}
    4960_win = (sys.platform == "win32")
     61_last_update = None
    5062
    5163def gen_filenames():
    5264    for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
    def gen_filenames():  
    5668            continue # File might be in an egg, so it can't be reloaded.
    5769        yield filename
    5870
     71def inotify_code_changed():
     72    wm = WatchManager()
     73    notifier = Notifier(wm)
     74    min_update_interval = datetime.timedelta(seconds=1)
     75
     76    def update_watch(sender=None, **kwargs):
     77        global _last_update
     78
     79        if _last_update is not None and \
     80                datetime.datetime.now() - _last_update < min_update_interval:
     81            return
     82
     83        _last_update = datetime.datetime.now()
     84
     85        mask = EventsCodes.IN_MODIFY \
     86             | EventsCodes.IN_DELETE \
     87             | EventsCodes.IN_ATTRIB \
     88             | EventsCodes.IN_MOVED_FROM \
     89             | EventsCodes.IN_MOVED_TO \
     90             | EventsCodes.IN_CREATE
     91
     92        for path in gen_filenames():
     93            wm.add_watch(path, mask)
     94
     95    from django.core.signals import request_finished
     96    request_finished.connect(update_watch)
     97    update_watch()
     98
     99    # Block forever
     100    notifier.check_events(timeout=None)
     101    notifier.stop()
     102
     103    # If we are here the code must have changed.
     104    return True
     105
    59106def code_changed():
    60107    global _mtimes, _win
     108
    61109    for filename in gen_filenames():
    62110        stat = os.stat(filename)
    63111        mtime = stat.st_mtime
    def code_changed():  
    72120    return False
    73121
    74122def reloader_thread():
     123    if USE_INOTIFY:
     124        fn = inotify_code_changed
     125    else:
     126        fn = code_changed
     127
    75128    while RUN_RELOADER:
    76         if code_changed():
     129        if fn():
    77130            sys.exit(3) # force reload
    78131        time.sleep(1)
    79132
Back to Top