def test_execution_timeout_config(tmp_path): """ tests the timeout value passed to the executor""" from jupyter_cache.executors import load_executor db = JupyterCacheBase(str(tmp_path)) db.stage_notebook_file( path=os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")) executor = load_executor("basic", db) result = executor.run_and_cache(timeout=60) assert result == { "succeeded": [os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")], "excepted": [], "errored": [], } db.clear_cache() db.stage_notebook_file( path=os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")) executor = load_executor("basic", db) result = executor.run_and_cache(timeout=1) assert result == { "succeeded": [], "excepted": [os.path.join(NB_PATH, "complex_outputs_unrun.ipynb")], "errored": [], }
def execute_staged_nb( cache_base, pk_list, timeout: Optional[int], exec_in_temp: bool, allow_errors: bool, env: BuildEnvironment, ): """Executing the staged notebook.""" try: executor = load_executor("basic", cache_base, logger=LOGGER) except ImportError as error: LOGGER.error(str(error)) return 1 def _converter(path): text = Path(path).read_text(encoding="utf8") return get_nb_converter(path, env).func(text) result = executor.run_and_cache( filter_pks=pk_list or None, converter=_converter, timeout=timeout, allow_errors=allow_errors, run_in_temp=exec_in_temp, ) return result
def execute_staged_nb(cache_base, pk_list): """ executing the staged notebook """ try: executor = load_executor("basic", cache_base, logger=LOGGER) except ImportError as error: LOGGER.error(str(error)) return 1 result = executor.run_and_cache(filter_pks=pk_list or None, converter=path_to_notebook) return result
def test_run_in_temp_false(tmp_path): """ tests the timeout value passed to the executor""" from jupyter_cache.executors import load_executor db = JupyterCacheBase(str(tmp_path)) db.stage_notebook_file(path=os.path.join(NB_PATH, "basic.ipynb")) executor = load_executor("basic", db) result = executor.run_and_cache(run_in_temp=False) assert result == { "succeeded": [os.path.join(NB_PATH, "basic.ipynb")], "excepted": [], "errored": [], }
def test_execution_timeout_metadata(tmp_path): """ tests the timeout metadata key in notebooks""" from jupyter_cache.executors import load_executor db = JupyterCacheBase(str(tmp_path)) db.stage_notebook_file( path=os.path.join(NB_PATH, "sleep_2_timeout_1.ipynb")) executor = load_executor("basic", db) result = executor.run_and_cache() assert result == { "succeeded": [], "excepted": [os.path.join(NB_PATH, "sleep_2_timeout_1.ipynb")], "errored": [], }
def execute_nbs(cache_path, entry_point, pks, timeout): """Execute staged notebooks that are outdated.""" import yaml from jupyter_cache.executors import load_executor db = get_cache(cache_path) try: executor = load_executor("basic", db, logger=logger) except ImportError as error: logger.error(str(error)) return 1 result = executor.run_and_cache(filter_pks=pks or None, timeout=timeout) click.secho("Finished! Successfully executed notebooks have been cached.", fg="green") click.echo(yaml.safe_dump(result, sort_keys=False))
def test_execution(tmp_path): from jupyter_cache.executors import load_executor db = JupyterCacheBase(str(tmp_path)) db.stage_notebook_file(path=os.path.join(NB_PATH, "basic_unrun.ipynb")) db.stage_notebook_file(path=os.path.join(NB_PATH, "basic_failing.ipynb")) db.stage_notebook_file( path=os.path.join(NB_PATH, "external_output.ipynb"), assets=(os.path.join(NB_PATH, "basic.ipynb"), ), ) executor = load_executor("basic", db) result = executor.run_and_cache() print(result) assert result == { "succeeded": [ os.path.join(NB_PATH, "basic_unrun.ipynb"), os.path.join(NB_PATH, "external_output.ipynb"), ], "excepted": [os.path.join(NB_PATH, "basic_failing.ipynb")], "errored": [], } assert len(db.list_cache_records()) == 2 bundle = db.get_cache_bundle(1) assert bundle.nb.cells[0] == { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{ "name": "stdout", "output_type": "stream", "text": "1\n" }], "source": "a=1\nprint(a)", } assert "execution_seconds" in bundle.record.data with db.cache_artefacts_temppath(2) as path: paths = [ str(p.relative_to(path)) for p in path.glob("**/*") if p.is_file() ] assert paths == ["artifact.txt"] assert path.joinpath("artifact.txt").read_text(encoding="utf8") == "hi" stage_record = db.get_staged_record(2) assert stage_record.traceback is not None assert "Exception: oopsie!" in stage_record.traceback