Ticket #9722: 0001-add-support-for-pyinotify.patch

File 0001-add-support-for-pyinotify.patch, 3.2 KB (added by ch0wn, 14 years ago)

Update for django 1.2.1 and pyinotify 0.7

  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index 4921f7c..67d6a47 100644
    a b answer newbie questions, and generally made Django that much better:  
    510510    Cheng Zhang
    511511    Glenn Maynard <glenn@zewt.org>
    512512    bthomas
     513    Chris Lamb <lamby@debian.org>
     514    Pascal Hartig <phartig@rdrei.net>
    513515
    514516A big THANK YOU goes to:
    515517
  • django/utils/autoreload.py

    diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
    index 8d9d6f2..3eb02b1 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    import pyinotify
     49
     50    fd = pyinotify.LIBC.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
    51 def code_changed():
    52     global _mtimes, _win
     63
     64def gen_filenames():
     65    """
     66    Yields a generator over filenames  referenced in
     67    sys.modules.
     68    """
    5369    for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):
    5470        if filename.endswith(".pyc") or filename.endswith(".pyo"):
    5571            filename = filename[:-1]
    5672        if not os.path.exists(filename):
    5773            continue # File might be in an egg, so it can't be reloaded.
     74
     75        yield filename
     76
     77
     78def inotify_code_changed():
     79    """
     80    Checks for changed code using inotify.  After being called
     81    it blocks until a change event has been fired.
     82    """
     83    wm = pyinotify.WatchManager()
     84    notifier = pyinotify.Notifier(wm)
     85    min_update_interval = datetime.timedelta(seconds=1)
     86
     87    def update_watch(sender=None, **kwargs):
     88        global _last_update
     89
     90        if _last_update is not None and \
     91                datetime.datetime.now() - _last_update < min_update_interval:
     92            return
     93
     94        _last_update = datetime.datetime.now()
     95
     96        mask = pyinotify.IN_MODIFY \
     97             | pyinotify.IN_DELETE \
     98             | pyinotify.IN_ATTRIB \
     99             | pyinotify.IN_MOVED_FROM \
     100             | pyinotify.IN_MOVED_TO \
     101             | pyinotify.IN_CREATE
     102
     103        for path in gen_filenames():
     104            wm.add_watch(path, mask)
     105
     106    from django.core.signals import request_finished
     107    request_finished.connect(update_watch)
     108    update_watch()
     109
     110    # Block forever
     111    notifier.check_events(timeout=None)
     112    notifier.stop()
     113
     114    # If we are here the code must have changed.
     115    return True
     116
     117def code_changed():
     118    global _mtimes, _win
     119    for filename in gen_filenames():
    58120        stat = os.stat(filename)
    59121        mtime = stat.st_mtime
    60122        if _win:
    def code_changed():  
    68130    return False
    69131
    70132def reloader_thread():
     133    if USE_INOTIFY:
     134        fn = inotify_code_changed
     135    else:
     136        fn = code_changed
     137
    71138    while RUN_RELOADER:
    72         if code_changed():
     139        if fn():
    73140            sys.exit(3) # force reload
    74141        time.sleep(1)
    75142
Back to Top