Ticket #9589: autoreload.diff
File autoreload.diff, 2.7 KB (added by , 16 years ago) |
---|
-
django/utils/autoreload.py
47 47 48 48 _mtimes = {} 49 49 _win = (sys.platform == "win32") 50 _extra_files_to_monitor = [] 50 51 51 52 def code_changed(): 52 global _mtimes, _win53 for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())):53 def check(filename): 54 global _mtimes, _win 54 55 if filename.endswith(".pyc") or filename.endswith(".pyo"): 55 56 filename = filename[:-1] 56 57 if not os.path.exists(filename): 57 continue # File might be in an egg, so it can't be reloaded.58 return False # File might be in an egg, so it can't be reloaded. 58 59 stat = os.stat(filename) 59 60 mtime = stat.st_mtime 60 61 if _win: 61 62 mtime -= stat.st_ctime 62 63 if filename not in _mtimes: 63 64 _mtimes[filename] = mtime 64 continue65 return False 65 66 if mtime != _mtimes[filename]: 66 67 _mtimes = {} 67 68 return True 69 70 for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): 71 if check(filename): return True 72 for filename in _extra_files_to_monitor: 73 if check(filename): return True 68 74 return False 69 75 70 76 def reloader_thread(): … … 84 90 if exit_code != 3: 85 91 return exit_code 86 92 93 def watched_main_func(*args, **kwargs): 94 try: 95 return args[0](*args[1:], **kwargs) 96 except StandardError: 97 # Keep track of files that fail to load, so we can still try to 98 # monitor them for changes. This won't work if the cause of the 99 # exception is external. 100 t, v, tb = sys.exc_info() 101 if t == SyntaxError: 102 _extra_files_to_monitor.append(v.filename) 103 else: 104 while tb: 105 filename = tb.tb_frame.f_code.co_filename 106 if filename: 107 _extra_files_to_monitor.append(filename) 108 tb = tb.tb_next 109 raise 110 87 111 def python_reloader(main_func, args, kwargs): 88 112 if os.environ.get("RUN_MAIN") == "true": 89 thread.start_new_thread( main_func,args, kwargs)113 thread.start_new_thread(watched_main_func, (main_func,) + args, kwargs) 90 114 try: 91 115 reloader_thread() 92 116 except KeyboardInterrupt: … … 99 123 100 124 def jython_reloader(main_func, args, kwargs): 101 125 from _systemrestart import SystemRestart 102 thread.start_new_thread( main_func,args)126 thread.start_new_thread(watched_main_func, (main_func,) + args, kwargs) 103 127 while True: 104 128 if code_changed(): 105 129 raise SystemRestart