Path: blob/develop/rootfs/usr/local/lib/web/backend/run.py
387 views
#!/usr/bin/env python31from __future__ import (2absolute_import, division, print_function, with_statement3)4import os5import time6import sys7import subprocess8from vnc.util import ignored91011def main():12def run_with_reloader(main_func, extra_files=None, interval=3):13"""Run the given function in an independent python interpreter."""14def find_files(directory="./"):15for root, dirs, files in os.walk(directory):16for basename in files:17if basename.endswith('.py'):18filename = os.path.join(root, basename)19yield filename2021if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':22try:23main_func()24except KeyboardInterrupt:25pass26return2728proc = None29try:30while True:31log.info('Restarting with reloader {} {}'.format(32sys.executable,33' '.join(sys.argv))34)35args = [sys.executable] + sys.argv36new_environ = os.environ.copy()37new_environ['WERKZEUG_RUN_MAIN'] = 'true'3839proc = subprocess.Popen(40args,41env=new_environ,42close_fds=True,43preexec_fn=os.setsid44)45mtimes = {}46restart = False47while not restart:48for filename in find_files():49try:50mtime = os.stat(filename).st_mtime51except OSError:52continue5354old_time = mtimes.get(filename)55if old_time is None:56mtimes[filename] = mtime57continue58elif mtime > old_time:59log.info(60'Detected change in {}, reloading'.format(61filename62)63)64restart = True65proc.terminate()66break67time.sleep(interval)68except KeyboardInterrupt:69pass70finally:71with ignored(Exception):72proc.terminate()7374def run_server():75import socket76from gevent.pywsgi import WSGIServer77from vnc.app import app7879# websocket conflict: WebSocketHandler80if DEBUG:81# from werkzeug.debug import DebuggedApplication82app.debug = True83# app = DebuggedApplication(app, evalex=True)8485try:86log.info('Listening on http://localhost:{}'.format(PORT))87http_server = WSGIServer(('localhost', PORT), app)88http_server.serve_forever()89# app.run(host='localhost', port=PORT)90except socket.error as e:91log.exception(e)92except KeyboardInterrupt:93pass94finally:95http_server.stop(timeout=10)96log.info('shutdown gracefully')9798PORT = 607999DEBUG = False100os.environ['CONFIG'] = 'config.Production'101entrypoint = run_server102if '--debug' in sys.argv:103DEBUG = True104os.environ['CONFIG'] = 'config.Development'105entrypoint = lambda: run_with_reloader(run_server)106107# logging108import logging109from log.config import LoggingConfiguration110LoggingConfiguration.set(111logging.DEBUG if DEBUG else logging.INFO,112'/var/log/web.log'113)114logging.getLogger("werkzeug").setLevel(logging.WARNING)115log = logging.getLogger('novnc2')116117entrypoint()118119120if __name__ == "__main__":121main()122123124