diff --git a/AUTHORS b/AUTHORS
index 4921f7c..67d6a47 100644
a
|
b
|
answer newbie questions, and generally made Django that much better:
|
510 | 510 | Cheng Zhang |
511 | 511 | Glenn Maynard <glenn@zewt.org> |
512 | 512 | bthomas |
| 513 | Chris Lamb <lamby@debian.org> |
| 514 | Pascal Hartig <phartig@rdrei.net> |
513 | 515 | |
514 | 516 | A big THANK YOU goes to: |
515 | 517 | |
diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
index 8d9d6f2..3eb02b1 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 | import pyinotify |
| 49 | |
| 50 | fd = pyinotify.LIBC.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 | | def code_changed(): |
52 | | global _mtimes, _win |
| 63 | |
| 64 | def gen_filenames(): |
| 65 | """ |
| 66 | Yields a generator over filenames referenced in |
| 67 | sys.modules. |
| 68 | """ |
53 | 69 | for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): |
54 | 70 | if filename.endswith(".pyc") or filename.endswith(".pyo"): |
55 | 71 | filename = filename[:-1] |
56 | 72 | if not os.path.exists(filename): |
57 | 73 | continue # File might be in an egg, so it can't be reloaded. |
| 74 | |
| 75 | yield filename |
| 76 | |
| 77 | |
| 78 | def 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 | |
| 117 | def code_changed(): |
| 118 | global _mtimes, _win |
| 119 | for filename in gen_filenames(): |
58 | 120 | stat = os.stat(filename) |
59 | 121 | mtime = stat.st_mtime |
60 | 122 | if _win: |
… |
… |
def code_changed():
|
68 | 130 | return False |
69 | 131 | |
70 | 132 | def reloader_thread(): |
| 133 | if USE_INOTIFY: |
| 134 | fn = inotify_code_changed |
| 135 | else: |
| 136 | fn = code_changed |
| 137 | |
71 | 138 | while RUN_RELOADER: |
72 | | if code_changed(): |
| 139 | if fn(): |
73 | 140 | sys.exit(3) # force reload |
74 | 141 | time.sleep(1) |
75 | 142 | |