diff -r 534e337c9144 django/utils/autoreload.py
a
|
b
|
|
45 | 45 | |
46 | 46 | RUN_RELOADER = True |
47 | 47 | |
| 48 | _mtimes = {} |
| 49 | _win = (sys.platform == "win32") |
| 50 | def code_changed(): |
| 51 | global _mtimes, _win |
| 52 | for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): |
| 53 | if filename.endswith(".pyc") or filename.endswith("*.pyo"): |
| 54 | filename = filename[:-1] |
| 55 | if not os.path.exists(filename): |
| 56 | continue # File might be in an egg, so it can't be reloaded. |
| 57 | stat = os.stat(filename) |
| 58 | mtime = stat.st_mtime |
| 59 | if _win: |
| 60 | mtime -= stat.st_ctime |
| 61 | if filename not in _mtimes: |
| 62 | _mtimes[filename] = mtime |
| 63 | continue |
| 64 | if mtime != _mtimes[filename]: |
| 65 | _mtimes = {} |
| 66 | return True |
| 67 | return False |
| 68 | |
48 | 69 | def reloader_thread(): |
49 | | mtimes = {} |
50 | | win = (sys.platform == "win32") |
51 | 70 | while RUN_RELOADER: |
52 | | for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): |
53 | | if filename.endswith(".pyc") or filename.endswith("*.pyo"): |
54 | | filename = filename[:-1] |
55 | | if not os.path.exists(filename): |
56 | | continue # File might be in an egg, so it can't be reloaded. |
57 | | stat = os.stat(filename) |
58 | | mtime = stat.st_mtime |
59 | | if win: |
60 | | mtime -= stat.st_ctime |
61 | | if filename not in mtimes: |
62 | | mtimes[filename] = mtime |
63 | | continue |
64 | | if mtime != mtimes[filename]: |
65 | | sys.exit(3) # force reload |
| 71 | if code_changed(): |
| 72 | sys.exit(3) # force reload |
66 | 73 | time.sleep(1) |
67 | 74 | |
68 | 75 | def restart_with_reloader(): |
… |
… |
|
76 | 83 | if exit_code != 3: |
77 | 84 | return exit_code |
78 | 85 | |
79 | | def main(main_func, args=None, kwargs=None): |
| 86 | def python_reloader(main_func, args, kwargs): |
80 | 87 | if os.environ.get("RUN_MAIN") == "true": |
81 | | if args is None: |
82 | | args = () |
83 | | if kwargs is None: |
84 | | kwargs = {} |
85 | 88 | thread.start_new_thread(main_func, args, kwargs) |
86 | 89 | try: |
87 | 90 | reloader_thread() |
… |
… |
|
92 | 95 | sys.exit(restart_with_reloader()) |
93 | 96 | except KeyboardInterrupt: |
94 | 97 | pass |
| 98 | |
| 99 | def jython_reloader(main_func, args, kwargs): |
| 100 | from _systemrestart import SystemRestart |
| 101 | thread.start_new_thread(main_func, args) |
| 102 | while True: |
| 103 | if code_changed(): |
| 104 | raise SystemRestart |
| 105 | time.sleep(1) |
| 106 | |
| 107 | |
| 108 | def main(main_func, args=None, kwargs=None): |
| 109 | if args is None: |
| 110 | args = () |
| 111 | if kwargs is None: |
| 112 | kwargs = {} |
| 113 | if sys.platform.startswith('java'): |
| 114 | reloader = jython_reloader |
| 115 | else: |
| 116 | reloader = python_reloader |
| 117 | reloader(main_func, args, kwargs) |
| 118 | |