def version_from_git(ref): ''' Get the git-version of a specific ref, e.g. HEAD, origin/master. ''' cmd = ["git", "describe", "--tags", "--always", ref] try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) code = proc.wait() except OSError: write("Failed to run: %s" % " ".join(cmd)) sys.exit(1) if code != 0: write("Failed to get version for %s" % ref) sys.exit(1) version = proc.stdout.read().decode('utf-8').strip() try: # git-version = tag-num-gSHA1 tag, _, sha1 = version.split("-") except ValueError: return version else: return "%s-%s" % (tag, sha1[1:])
def bokeh_server(request: pytest.FixtureRequest, log_file: IO[str]) -> str: bokeh_port: int = request.config.option.bokeh_port cmd = ["python", "-m", "bokeh", "serve"] argv = [f"--port={bokeh_port}"] bokeh_server_url = f"http://localhost:{bokeh_port}" env = os.environ.copy() env['BOKEH_MINIFIED'] = 'false' try: proc = subprocess.Popen(cmd + argv, env=env, stdout=log_file, stderr=log_file) except OSError: write(f"Failed to run: {' '.join(cmd + argv)}") sys.exit(1) else: # Add in the clean-up code def stop_bokeh_server() -> None: write("Shutting down bokeh-server ...") proc.kill() request.addfinalizer(stop_bokeh_server) def wait_until(func: Callable[[], Any], timeout: float = 5.0, interval: float = 0.01) -> bool: start = time.time() while True: if func(): return True if time.time() - start > timeout: return False time.sleep(interval) def wait_for_bokeh_server() -> bool: def helper() -> Any: if proc.returncode is not None: return True try: # type: ignore[unreachable] # XXX: typeshed bug, proc.returncode: int return requests.get(bokeh_server_url) except ConnectionError: return False return wait_until(helper) if not wait_for_bokeh_server(): write(f"Timeout when running: {' '.join(cmd + argv)}") sys.exit(1) if proc.returncode is not None: write(f"bokeh server exited with code {proc.returncode}") sys.exit(1) return bokeh_server_url # type: ignore[unreachable] # XXX: typeshed bug, proc.returncode: int
def bokeh_server(request, log_file): bokeh_port = request.config.option.bokeh_port cmd = ["python", "-m", "bokeh", "serve"] argv = ["--port=%s" % bokeh_port] bokeh_server_url = 'http://localhost:%s' % bokeh_port env = os.environ.copy() env['BOKEH_MINIFIED'] = 'false' try: proc = subprocess.Popen(cmd + argv, env=env, stdout=log_file, stderr=log_file) except OSError: write("Failed to run: %s" % " ".join(cmd + argv)) sys.exit(1) else: # Add in the clean-up code def stop_bokeh_server(): write("Shutting down bokeh-server ...") proc.kill() request.addfinalizer(stop_bokeh_server) def wait_until(func, timeout=5.0, interval=0.01): start = time.time() while True: if func(): return True if time.time() - start > timeout: return False time.sleep(interval) def wait_for_bokeh_server(): def helper(): if proc.returncode is not None: return True try: return requests.get(bokeh_server_url) except ConnectionError: return False return wait_until(helper) if not wait_for_bokeh_server(): write("Timeout when running: %s" % " ".join(cmd + argv)) sys.exit(1) if proc.returncode is not None: write("bokeh server exited with code " + str(proc.returncode)) sys.exit(1) return bokeh_server_url
def bokeh_server(request, log_file): bokeh_port = request.config.option.bokeh_port cmd = ["python", "-m", "bokeh", "serve"] argv = ["--port=%s" % bokeh_port] bokeh_server_url = 'http://localhost:%s' % bokeh_port env = os.environ.copy() env['BOKEH_MINIFIED'] = 'false' try: proc = subprocess.Popen(cmd + argv, env=env, stdout=log_file, stderr=log_file) except OSError: write("Failed to run: %s" % " ".join(cmd + argv)) sys.exit(1) else: # Add in the clean-up code def stop_bokeh_server(): write("Shutting down bokeh-server ...") proc.kill() request.addfinalizer(stop_bokeh_server) def wait_until(func, timeout=5.0, interval=0.01): start = time.time() while True: if func(): return True if time.time() - start > timeout: return False time.sleep(interval) def wait_for_bokeh_server(): def helper(): if proc.returncode is not None: return True try: return requests.get(bokeh_server_url) except ConnectionError: return False return wait_until(helper) if not wait_for_bokeh_server(): write("Timeout when running: %s" % " ".join(cmd + argv)) sys.exit(1) if proc.returncode is not None: write("bokeh server exited with code " + str(proc.returncode)) sys.exit(1) return bokeh_server_url
def jupyter_notebook(request, log_file): """ Starts a jupyter notebook server at the beginning of a session, and closes at the end of a session. Adds custom.js that runs all the cells on notebook opening. Cleans out this custom.js at the end of the test run. Returns the url that the jupyter notebook is running at. """ # First - set-up the notebooks to run all cells when they're opened # # Can be cleaned up further to remember the user's existing customJS # and then restore it after the test run. from jupyter_core import paths config_dir = paths.jupyter_config_dir() body = """ require(["base/js/namespace", "base/js/events"], function (IPython, events) { events.on("kernel_ready.Kernel", function () { IPython.notebook.execute_all_cells(); }); }); """ custom = join(config_dir, "custom") if not exists(custom): os.makedirs(custom) customjs = join(custom, "custom.js") old_customjs = None if exists(customjs): with open(customjs) as f: old_customjs = f.read() with open(customjs, "w") as f: f.write(body) # Add in the clean-up code def clean_up_customjs(): text = old_customjs if old_customjs is not None else "" with open(customjs, "w") as f: f.write(text) request.addfinalizer(clean_up_customjs) # Second - Run a notebook server at the examples directory # notebook_port = request.config.option.notebook_port env = os.environ.copy() env['BOKEH_RESOURCES'] = 'server' # Launch from the base directory of bokeh repo notebook_dir = join(dirname(__file__), pardir, pardir) cmd = ["jupyter", "notebook"] argv = ["--no-browser", "--port=%s" % notebook_port, "--notebook-dir=%s" % notebook_dir] jupter_notebook_url = "http://localhost:%d" % notebook_port try: proc = subprocess.Popen(cmd + argv, env=env, stdout=log_file, stderr=log_file) except OSError: write("Failed to run: %s" % " ".join(cmd + argv)) sys.exit(1) else: # Add in the clean-up code def stop_jupyter_notebook(): write("Shutting down jupyter-notebook ...") proc.kill() request.addfinalizer(stop_jupyter_notebook) def wait_until(func, timeout=5.0, interval=0.01): start = time.time() while True: if func(): return True if time.time() - start > timeout: return False time.sleep(interval) def wait_for_jupyter_notebook(): def helper(): if proc.returncode is not None: return True try: return requests.get(jupter_notebook_url) except ConnectionError: return False return wait_until(helper) if not wait_for_jupyter_notebook(): write("Timeout when running: %s" % " ".join(cmd + argv)) sys.exit(1) if proc.returncode is not None: write("Jupyter notebook exited with code " + str(proc.returncode)) sys.exit(1) return jupter_notebook_url
def stop_jupyter_notebook(): write("Shutting down jupyter-notebook ...") proc.kill()
def stop_bokeh_server(): write("Shutting down bokeh-server ...") proc.kill()
def jupyter_notebook(request, log_file): """ Starts a jupyter notebook server at the beginning of a session, and closes at the end of a session. Adds custom.js that runs all the cells on notebook opening. Cleans out this custom.js at the end of the test run. Returns the url that the jupyter notebook is running at. """ # First - set-up the notebooks to run all cells when they're opened # # Can be cleaned up further to remember the user's existing customJS # and then restore it after the test run. from jupyter_core import paths config_dir = paths.jupyter_config_dir() body = """ require(["base/js/namespace", "base/js/events"], function (IPython, events) { events.on("kernel_ready.Kernel", function () { IPython.notebook.execute_all_cells(); }); }); """ custom = join(config_dir, "custom") if not exists(custom): os.makedirs(custom) customjs = join(custom, "custom.js") old_customjs = None if exists(customjs): old_customjs = open(customjs, "r").read() with open(customjs, "w") as f: f.write(body) # Add in the clean-up code def clean_up_customjs(): text = old_customjs if old_customjs is not None else "" with open(customjs, "w") as f: f.write(text) request.addfinalizer(clean_up_customjs) # Second - Run a notebook server at the examples directory # notebook_port = pytest.config.option.notebook_port env = os.environ.copy() env['BOKEH_RESOURCES'] = 'server' # Launch from the base directory of bokeh repo notebook_dir = join(dirname(__file__), pardir, pardir) cmd = ["jupyter", "notebook"] argv = ["--no-browser", "--port=%s" % notebook_port, "--notebook-dir=%s" % notebook_dir] jupter_notebook_url = "http://localhost:%d" % notebook_port try: proc = subprocess.Popen(cmd + argv, env=env, stdout=log_file, stderr=log_file) except OSError: write("Failed to run: %s" % " ".join(cmd + argv)) sys.exit(1) else: # Add in the clean-up code def stop_jupyter_notebook(): write("Shutting down jupyter-notebook ...") proc.kill() request.addfinalizer(stop_jupyter_notebook) def wait_until(func, timeout=5.0, interval=0.01): start = time.time() while True: if func(): return True if time.time() - start > timeout: return False time.sleep(interval) def wait_for_jupyter_notebook(): def helper(): if proc.returncode is not None: return True try: return requests.get(jupter_notebook_url) except ConnectionError: return False return wait_until(helper) if not wait_for_jupyter_notebook(): write("Timeout when running: %s" % " ".join(cmd + argv)) sys.exit(1) if proc.returncode is not None: write("Jupyter notebook exited with code " + str(proc.returncode)) sys.exit(1) return jupter_notebook_url
def stop_jupyter_notebook(): write("Shutting down jupyter-notebook ...") proc.kill()
def stop_bokeh_server(): write("Shutting down bokeh-server ...") proc.kill()