|
Revision 711
(checked in by fumanchu, 3 years ago)
|
autoreload now calls server.stop() on interrupt. No test for it though. :(
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
import os |
|---|
| 5 |
import sys |
|---|
| 6 |
import time |
|---|
| 7 |
import thread |
|---|
| 8 |
|
|---|
| 9 |
RUN_RELOADER = True |
|---|
| 10 |
reloadFiles = [] |
|---|
| 11 |
|
|---|
| 12 |
def reloader_thread(): |
|---|
| 13 |
mtimes = {} |
|---|
| 14 |
|
|---|
| 15 |
def fileattr(m): |
|---|
| 16 |
return getattr(m, "__file__", None) |
|---|
| 17 |
|
|---|
| 18 |
while RUN_RELOADER: |
|---|
| 19 |
for filename in map(fileattr, sys.modules.values()) + reloadFiles: |
|---|
| 20 |
if filename: |
|---|
| 21 |
if filename.endswith(".pyc"): |
|---|
| 22 |
filename = filename[:-1] |
|---|
| 23 |
try: |
|---|
| 24 |
mtime = os.stat(filename).st_mtime |
|---|
| 25 |
except OSError: |
|---|
| 26 |
sys.exit(3) |
|---|
| 27 |
if filename not in mtimes: |
|---|
| 28 |
mtimes[filename] = mtime |
|---|
| 29 |
continue |
|---|
| 30 |
if mtime > mtimes[filename]: |
|---|
| 31 |
sys.exit(3) |
|---|
| 32 |
time.sleep(1) |
|---|
| 33 |
|
|---|
| 34 |
def restart_with_reloader(): |
|---|
| 35 |
while True: |
|---|
| 36 |
args = [sys.executable] + sys.argv |
|---|
| 37 |
if sys.platform == "win32": |
|---|
| 38 |
args = ['"%s"' % arg for arg in args] |
|---|
| 39 |
new_environ = os.environ.copy() |
|---|
| 40 |
new_environ["RUN_MAIN"] = 'true' |
|---|
| 41 |
exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ) |
|---|
| 42 |
if exit_code != 3: |
|---|
| 43 |
return exit_code |
|---|
| 44 |
|
|---|
| 45 |
def main(main_func, args=None, kwargs=None): |
|---|
| 46 |
if os.environ.get("RUN_MAIN") == "true": |
|---|
| 47 |
|
|---|
| 48 |
if args is None: |
|---|
| 49 |
args = () |
|---|
| 50 |
if kwargs is None: |
|---|
| 51 |
kwargs = {} |
|---|
| 52 |
thread.start_new_thread(main_func, args, kwargs) |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
reloader_thread() |
|---|
| 57 |
else: |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
sys.exit(restart_with_reloader()) |
|---|