| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
version = "$Revision: 1.9 $".split()[1] |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
import time, os, threading, sys, types, imp |
|---|
| 44 |
|
|---|
| 45 |
def _get_compiled_ext(): |
|---|
| 46 |
for ext, mode, typ in imp.get_suffixes(): |
|---|
| 47 |
if typ == imp.PY_COMPILED: |
|---|
| 48 |
return ext |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
PY_COMPILED_EXT = _get_compiled_ext() |
|---|
| 52 |
|
|---|
| 53 |
class ModuleWatcher: |
|---|
| 54 |
running = 0 |
|---|
| 55 |
def __init__(self): |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
import atexit |
|---|
| 59 |
atexit.register(self.stop) |
|---|
| 60 |
|
|---|
| 61 |
def run(self): |
|---|
| 62 |
if self.running: |
|---|
| 63 |
print "# autoreload already running" |
|---|
| 64 |
return |
|---|
| 65 |
print "# starting autoreload" |
|---|
| 66 |
self.running = 1 |
|---|
| 67 |
self.thread = threading.Thread(target=self._check_modules) |
|---|
| 68 |
self.thread.setDaemon(1) |
|---|
| 69 |
self.thread.start() |
|---|
| 70 |
|
|---|
| 71 |
def stop(self): |
|---|
| 72 |
if not self.running: |
|---|
| 73 |
print "# autoreload not running" |
|---|
| 74 |
return |
|---|
| 75 |
self.running = 0 |
|---|
| 76 |
self.thread.join() |
|---|
| 77 |
print "# autoreload stopped" |
|---|
| 78 |
|
|---|
| 79 |
def _check_modules(self): |
|---|
| 80 |
while self.running: |
|---|
| 81 |
time.sleep(0.01) |
|---|
| 82 |
for m in sys.modules.values(): |
|---|
| 83 |
if not hasattr(m, '__file__'): |
|---|
| 84 |
continue |
|---|
| 85 |
if m.__name__ == '__main__': |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
continue |
|---|
| 94 |
file = m.__file__ |
|---|
| 95 |
path, ext = os.path.splitext(file) |
|---|
| 96 |
|
|---|
| 97 |
if ext.lower() == '.py': |
|---|
| 98 |
ext = PY_COMPILED_EXT |
|---|
| 99 |
file = path + PY_COMPILED_EXT |
|---|
| 100 |
if ext != PY_COMPILED_EXT: |
|---|
| 101 |
continue |
|---|
| 102 |
|
|---|
| 103 |
try: |
|---|
| 104 |
if os.stat(file[:-1])[8] <= os.stat(file)[8]: |
|---|
| 105 |
continue |
|---|
| 106 |
except OSError: |
|---|
| 107 |
continue |
|---|
| 108 |
|
|---|
| 109 |
try: |
|---|
| 110 |
superreload(m) |
|---|
| 111 |
except: |
|---|
| 112 |
import traceback |
|---|
| 113 |
traceback.print_exc(0) |
|---|
| 114 |
|
|---|
| 115 |
def update_function(old, new, attrnames): |
|---|
| 116 |
for name in attrnames: |
|---|
| 117 |
setattr(old, name, getattr(new, name)) |
|---|
| 118 |
|
|---|
| 119 |
def superreload(module, |
|---|
| 120 |
reload=reload, |
|---|
| 121 |
_old_objects = {}): |
|---|
| 122 |
"""superreload (module) -> module |
|---|
| 123 |
|
|---|
| 124 |
Enhanced version of the builtin reload function. |
|---|
| 125 |
superreload replaces the class dictionary of every top-level |
|---|
| 126 |
class in the module with the new one automatically, |
|---|
| 127 |
as well as every function's code object. |
|---|
| 128 |
""" |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
for name, object in module.__dict__.items(): |
|---|
| 133 |
key = (module.__name__, name) |
|---|
| 134 |
_old_objects.setdefault(key, []).append(object) |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
module = reload(module) |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
count = 0 |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
for name, new_obj in module.__dict__.items(): |
|---|
| 151 |
key = (module.__name__, name) |
|---|
| 152 |
if _old_objects.has_key(key): |
|---|
| 153 |
for old_obj in _old_objects[key]: |
|---|
| 154 |
if type(new_obj) == types.ClassType: |
|---|
| 155 |
old_obj.__dict__.update(new_obj.__dict__) |
|---|
| 156 |
count += 1 |
|---|
| 157 |
elif type(new_obj) == types.FunctionType: |
|---|
| 158 |
update_function(old_obj, |
|---|
| 159 |
new_obj, |
|---|
| 160 |
"func_code func_defaults func_doc".split()) |
|---|
| 161 |
count += 1 |
|---|
| 162 |
elif type(new_obj) == types.MethodType: |
|---|
| 163 |
update_function(old_obj.im_func, |
|---|
| 164 |
new_obj.im_func, |
|---|
| 165 |
"func_code func_defaults func_doc".split()) |
|---|
| 166 |
count += 1 |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
return module |
|---|
| 172 |
|
|---|
| 173 |
_watcher = ModuleWatcher() |
|---|
| 174 |
|
|---|
| 175 |
run = _watcher.run |
|---|
| 176 |
stop = _watcher.stop |
|---|
| 177 |
|
|---|
| 178 |
__all__ = ['run', 'stop', 'superreload'] |
|---|