def preview(base_path): """ Context manager for ASV preview web server. Gives the base URL to use. Parameters ---------- base_path : str Path to serve files from """ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): def translate_path(self, path): # Don't serve from cwd, but from a different directory path = SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self, path) path = os.path.join(base_path, os.path.relpath(path, os.getcwd())) return util.long_path(path) httpd, base_url = create_httpd(Handler) def run(): try: httpd.serve_forever() except: import traceback traceback.print_exc() return thread = threading.Thread(target=run) thread.start() try: yield base_url finally: httpd.shutdown() thread.join()
def preview(base_path): """ Context manager for ASV preview web server. Gives the base URL to use. Parameters ---------- base_path : str Path to serve files from """ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): def translate_path(self, path): # Don't serve from cwd, but from a different directory path = SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path( self, path) path = os.path.join(base_path, os.path.relpath(path, os.getcwd())) return util.long_path(path) httpd, base_url = create_httpd(Handler) def run(): try: httpd.serve_forever() except: import traceback traceback.print_exc() finally: httpd.server_close() thread = threading.Thread(target=run) thread.daemon = True thread.start() try: yield base_url finally: # Stop must be run in a separate thread, because # httpd.shutdown blocks until serve_forever returns. We don't # want to block here --- it appears in some environments # problems shutting down the server may arise. stopper = threading.Thread(target=httpd.shutdown) stopper.daemon = True stopper.start() stopper.join(5.0)
def preview(base_path): """ Context manager for ASV preview web server. Gives the base URL to use. Parameters ---------- base_path : str Path to serve files from """ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): def translate_path(self, path): # Don't serve from cwd, but from a different directory path = SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self, path) path = os.path.join(base_path, os.path.relpath(path, os.getcwd())) return util.long_path(path) httpd, base_url = create_httpd(Handler) def run(): try: httpd.serve_forever() except: import traceback traceback.print_exc() finally: httpd.server_close() thread = threading.Thread(target=run) thread.daemon = True thread.start() try: yield base_url finally: # Stop must be run in a separate thread, because # httpd.shutdown blocks until serve_forever returns. We don't # want to block here --- it appears in some environments # problems shutting down the server may arise. stopper = threading.Thread(target=httpd.shutdown) stopper.daemon = True stopper.start() stopper.join(5.0)