| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
import errno |
|---|
| 5 |
import os |
|---|
| 6 |
import re |
|---|
| 7 |
import sys |
|---|
| 8 |
import time |
|---|
| 9 |
import thread |
|---|
| 10 |
|
|---|
| 11 |
RUN_RELOADER = True |
|---|
| 12 |
reloadFiles = [] |
|---|
| 13 |
ignoreFiles = ['<string>'] |
|---|
| 14 |
match = ".*" |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
def reloader_thread(freq): |
|---|
| 18 |
mtimes = {} |
|---|
| 19 |
|
|---|
| 20 |
while RUN_RELOADER: |
|---|
| 21 |
sysfiles = [] |
|---|
| 22 |
for k, m in sys.modules.items(): |
|---|
| 23 |
if re.match(match, k): |
|---|
| 24 |
if hasattr(m, "__loader__"): |
|---|
| 25 |
if hasattr(m.__loader__, "archive"): |
|---|
| 26 |
k = m.__loader__.archive |
|---|
| 27 |
k = getattr(m, "__file__", None) |
|---|
| 28 |
sysfiles.append(k) |
|---|
| 29 |
|
|---|
| 30 |
for filename in sysfiles + reloadFiles: |
|---|
| 31 |
if filename and filename not in ignoreFiles: |
|---|
| 32 |
|
|---|
| 33 |
orig = filename |
|---|
| 34 |
if filename.endswith(".pyc"): |
|---|
| 35 |
filename = filename[:-1] |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
try: |
|---|
| 39 |
mtime = os.stat(filename).st_mtime |
|---|
| 40 |
except OSError, e: |
|---|
| 41 |
if orig.endswith('.pyc') and e[0] == errno.ENOENT: |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
continue |
|---|
| 48 |
sys.exit(3) |
|---|
| 49 |
|
|---|
| 50 |
if filename not in mtimes: |
|---|
| 51 |
mtimes[filename] = mtime |
|---|
| 52 |
continue |
|---|
| 53 |
|
|---|
| 54 |
if mtime > mtimes[filename]: |
|---|
| 55 |
sys.exit(3) |
|---|
| 56 |
time.sleep(freq) |
|---|
| 57 |
|
|---|
| 58 |
def restart_with_reloader(): |
|---|
| 59 |
while True: |
|---|
| 60 |
args = [sys.executable] + sys.argv |
|---|
| 61 |
if sys.platform == "win32": |
|---|
| 62 |
args = ['"%s"' % arg for arg in args] |
|---|
| 63 |
new_environ = os.environ.copy() |
|---|
| 64 |
new_environ["RUN_MAIN"] = 'true' |
|---|
| 65 |
exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) |
|---|
| 66 |
if exit_code != 3: |
|---|
| 67 |
return exit_code |
|---|
| 68 |
|
|---|
| 69 |
def main(main_func, args=None, kwargs=None, freq=1): |
|---|
| 70 |
if os.environ.get("RUN_MAIN") == "true": |
|---|
| 71 |
|
|---|
| 72 |
if args is None: |
|---|
| 73 |
args = () |
|---|
| 74 |
if kwargs is None: |
|---|
| 75 |
kwargs = {} |
|---|
| 76 |
thread.start_new_thread(main_func, args, kwargs) |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
reloader_thread(freq) |
|---|
| 81 |
else: |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
sys.exit(restart_with_reloader()) |
|---|