| | 48 | _mtimes = {} |
|---|
| | 49 | _win = (sys.platform == "win32") |
|---|
| | 50 | |
|---|
| | 51 | def code_changed(): |
|---|
| | 52 | global _mtimes, _win |
|---|
| | 53 | for filename in filter(lambda v: v, map(lambda m: getattr(m, "__file__", None), sys.modules.values())): |
|---|
| | 54 | if filename.endswith(".pyc") or filename.endswith(".pyo"): |
|---|
| | 55 | filename = filename[:-1] |
|---|
| | 56 | if not os.path.exists(filename): |
|---|
| | 57 | continue # File might be in an egg, so it can't be reloaded. |
|---|
| | 58 | stat = os.stat(filename) |
|---|
| | 59 | mtime = stat.st_mtime |
|---|
| | 60 | if _win: |
|---|
| | 61 | mtime -= stat.st_ctime |
|---|
| | 62 | if filename not in _mtimes: |
|---|
| | 63 | _mtimes[filename] = mtime |
|---|
| | 64 | continue |
|---|
| | 65 | if mtime != _mtimes[filename]: |
|---|
| | 66 | _mtimes = {} |
|---|
| | 67 | return True |
|---|
| | 68 | return False |
|---|
| | 69 | |
|---|
| 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 |
|---|
| | 72 | if code_changed(): |
|---|
| | 73 | sys.exit(3) # force reload |
|---|
| | 99 | |
|---|
| | 100 | def jython_reloader(main_func, args, kwargs): |
|---|
| | 101 | from _systemrestart import SystemRestart |
|---|
| | 102 | thread.start_new_thread(main_func, args) |
|---|
| | 103 | while True: |
|---|
| | 104 | if code_changed(): |
|---|
| | 105 | raise SystemRestart |
|---|
| | 106 | time.sleep(1) |
|---|
| | 107 | |
|---|
| | 108 | |
|---|
| | 109 | def main(main_func, args=None, kwargs=None): |
|---|
| | 110 | if args is None: |
|---|
| | 111 | args = () |
|---|
| | 112 | if kwargs is None: |
|---|
| | 113 | kwargs = {} |
|---|
| | 114 | if sys.platform.startswith('java'): |
|---|
| | 115 | reloader = jython_reloader |
|---|
| | 116 | else: |
|---|
| | 117 | reloader = python_reloader |
|---|
| | 118 | reloader(main_func, args, kwargs) |
|---|
| | 119 | |
|---|