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:
|
| 439 | 439 | Mykola Zamkovoi <nickzam@gmail.com> |
| 440 | 440 | Jarek Zgoda <jarek.zgoda@gmail.com> |
| 441 | 441 | Cheng Zhang |
| | 442 | Chris Lamb <lamby@debian.org> |
| 442 | 443 | |
| 443 | 444 | A big THANK YOU goes to: |
| 444 | 445 | |
diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
index 4dbc945..6afb58e 100644
|
a
|
b
|
|
| 28 | 28 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 | |
| 31 | | import os, sys, time |
| | 31 | import os, sys, time, datetime |
| 32 | 32 | |
| 33 | 33 | try: |
| 34 | 34 | import thread |
| … |
… |
try:
|
| 42 | 42 | except ImportError: |
| 43 | 43 | pass |
| 44 | 44 | |
| | 45 | USE_INOTIFY = False |
| | 46 | try: |
| | 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) |
| | 54 | except ImportError: |
| | 55 | pass |
| 45 | 56 | |
| 46 | 57 | RUN_RELOADER = True |
| 47 | 58 | |
| 48 | 59 | _mtimes = {} |
| 49 | 60 | _win = (sys.platform == "win32") |
| | 61 | _last_update = None |
| 50 | 62 | |
| 51 | 63 | def gen_filenames(): |
| 52 | 64 | for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): |
| … |
… |
def gen_filenames():
|
| 56 | 68 | continue # File might be in an egg, so it can't be reloaded. |
| 57 | 69 | yield filename |
| 58 | 70 | |
| | 71 | def 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 | |
| 59 | 106 | def code_changed(): |
| 60 | 107 | global _mtimes, _win |
| | 108 | |
| 61 | 109 | for filename in gen_filenames(): |
| 62 | 110 | stat = os.stat(filename) |
| 63 | 111 | mtime = stat.st_mtime |
| … |
… |
def code_changed():
|
| 72 | 120 | return False |
| 73 | 121 | |
| 74 | 122 | def reloader_thread(): |
| | 123 | if USE_INOTIFY: |
| | 124 | fn = inotify_code_changed |
| | 125 | else: |
| | 126 | fn = code_changed |
| | 127 | |
| 75 | 128 | while RUN_RELOADER: |
| 76 | | if code_changed(): |
| | 129 | if fn(): |
| 77 | 130 | sys.exit(3) # force reload |
| 78 | 131 | time.sleep(1) |
| 79 | 132 | |