From 1e35016e52743e73baf2325f0c48f60e3e508738 Mon Sep 17 00:00:00 2001 From: Pedro Crespo <32402063+pcrespov@users.noreply.github.com> Date: Mon, 24 Oct 2022 17:01:41 +0200 Subject: [PATCH] adds python binder --- hooks/post_gen_project.py | 11 ++- ...er_program.py => Python.binder_program.py} | 79 +++++++++++-------- ...quirements.txt => Python.requirements.txt} | 0 3 files changed, 54 insertions(+), 36 deletions(-) rename {{cookiecutter.project_slug}}/.osparc/{osparc_binder_program.py => Python.binder_program.py} (89%) rename {{cookiecutter.project_slug}}/.osparc/{requirements.txt => Python.requirements.txt} (100%) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 55d6293f..463296ee 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -62,9 +62,14 @@ def create_repo_folder(): def check_python(): - if "python" not in SELECTED_DOCKER_BASE: - Path(".osparc/osparc_binder_program.py").unlink(missing_ok=True) - Path(".osparc/requirements.txt").unlink(missing_ok=True) + is_pyconfig = "python" in SELECTED_DOCKER_BASE + + for fp in Path(".osparc").glob("Python.*"): + if fp.is_file(): + if is_pyconfig: + fp.rename( fp.parent / fp.name.removeprefix("Python.")) + else: + fp.unlink(missing_ok=True) # TODO: if python, then service.cli has to be removed! diff --git a/{{cookiecutter.project_slug}}/.osparc/osparc_binder_program.py b/{{cookiecutter.project_slug}}/.osparc/Python.binder_program.py similarity index 89% rename from {{cookiecutter.project_slug}}/.osparc/osparc_binder_program.py rename to {{cookiecutter.project_slug}}/.osparc/Python.binder_program.py index 653753a5..f7a7f920 100755 --- a/{{cookiecutter.project_slug}}/.osparc/osparc_binder_program.py +++ b/{{cookiecutter.project_slug}}/.osparc/Python.binder_program.py @@ -18,7 +18,6 @@ # typer[all]>=0.6.1,<1.0.0 # typing_extensions ; python_version<'3.8' -from curses import meta import importlib # nopycln: import import importlib.util # nopycln: import import inspect @@ -26,7 +25,9 @@ import logging import os import sys +from contextlib import suppress from copy import deepcopy +from curses import meta from inspect import Parameter, Signature from pathlib import Path from textwrap import indent @@ -66,8 +67,7 @@ class ConfigSettings(BaseModel): - image: Optional[str] = None - bind_functions: list[str] + publish_functions: list[str] def discover_published_functions( @@ -255,30 +255,44 @@ def _as_args_tuple(return_annotation: Any) -> tuple: config_folder = DOT_OSPARC_DIR / core_func.__name__ config_folder.mkdir(parents=True, exist_ok=True) - metadata_path = config_folder / "metadata.yml" - metadata = { - "name": f"{core_func.__name__}", - "thumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Test.svg/315px-Test.svg.png", - "description": "", - "key": f"simcore/services/comp/ofs-{core_func.__name__}", - "integration-version": "1.0.0", - "type": "computational", - } - - try: - prev_metadata = yaml.safe_load(metadata_path.read_text()) - metadata.update(prev_metadata) - except Exception: - pass - - metadata.update( - **{ - "inputs": inputs, - "outputs": outputs, + def _update_metadata_file(): + metadata_path = config_folder / "metadata.yml" + metadata = { + "name": f"{core_func.__name__}", + "thumbnail": "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Test.svg/315px-Test.svg.png", + "description": "", + "key": f"simcore/services/comp/ofs-{core_func.__name__}", + "integration-version": "1.0.0", + "type": "computational", } - ) - runtime = { - "settings": [ + + with suppress(FileNotFoundError): + prev_metadata = yaml.safe_load(metadata_path.read_text()) + metadata.update(prev_metadata) + + metadata.update( + **{ + "inputs": inputs, + "outputs": outputs, + } + ) + + with metadata_path.open("wt") as fh: + yaml.safe_dump(metadata, fh, indent=1, sort_keys=False) + + def _update_runtime_file(): + runtime_path = config_folder / "runtime.yml" + runtime = {"settings": []} + with suppress(FileNotFoundError): + runtime = yaml.safe_load(runtime_path.read_text()) + + delete = [ + item for item in runtime["settings"] if item["name"] == "ContainerSpec" + ] + for item in delete: + runtime["settings"].remove(item) + + runtime["settings"].append( { "name": "ContainerSpec", "type": "ContainerSpec", @@ -286,14 +300,13 @@ def _as_args_tuple(return_annotation: Any) -> tuple: "Command": [".osparc/binder_program.py", core_func.__name__, "run"] }, }, - ] - } + ) - with (config_folder / "metadata.yml").open("wt") as fh: - yaml.safe_dump(metadata, fh, indent=1, sort_keys=False) + with runtime_path.open("wt") as fh: + yaml.safe_dump(runtime, fh, indent=1, sort_keys=False) - with (config_folder / "runtime.yml").open("wt") as fh: - yaml.safe_dump(runtime, fh, indent=1, sort_keys=False) + _update_metadata_file() + _update_runtime_file() def echo_jsonschema(core_func: Callable): @@ -467,7 +480,7 @@ def create_cli(expose: list[Callable], settings: dict[str, Any]) -> typer.Typer: main = create_cli( expose=discover_published_functions( - config_settings.bind_functions, dot_osparc_dir=DOT_OSPARC_DIR + config_settings.publish_functions, dot_osparc_dir=DOT_OSPARC_DIR ), settings=config_settings, ) diff --git a/{{cookiecutter.project_slug}}/.osparc/requirements.txt b/{{cookiecutter.project_slug}}/.osparc/Python.requirements.txt similarity index 100% rename from {{cookiecutter.project_slug}}/.osparc/requirements.txt rename to {{cookiecutter.project_slug}}/.osparc/Python.requirements.txt