Skip to content

Commit

Permalink
sonar on _config
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Jun 5, 2024
1 parent 5ee17a0 commit 6cc7039
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rich.console import Console

from ..compose_spec_model import ComposeSpecification
from ..errors import UndefinedOciImageSpec
from ..errors import UndefinedOciImageSpecError
from ..oci_image_spec import LS_LABEL_PREFIX, OCI_LABEL_PREFIX
from ..osparc_config import (
OSPARC_CONFIG_DIRNAME,
Expand Down Expand Up @@ -63,10 +63,10 @@ def create_docker_compose_image_spec(

config_basedir = meta_config_path.parent

# required
# REQUIRED
meta_cfg = MetadataConfig.from_yaml(meta_config_path)

# required
# REQUIRED
if docker_compose_overwrite_path:
docker_compose_overwrite_cfg = DockerComposeOverwriteConfig.from_yaml(
docker_compose_overwrite_path
Expand All @@ -76,7 +76,7 @@ def create_docker_compose_image_spec(
service_name=meta_cfg.service_name()
)

# optional
# OPTIONAL
runtime_cfg = None
if service_config_path:
try:
Expand All @@ -91,11 +91,11 @@ def create_docker_compose_image_spec(
(config_basedir / f"{OCI_LABEL_PREFIX}.yml").read_text()
)
if not oci_spec:
raise UndefinedOciImageSpec
raise UndefinedOciImageSpecError

oci_labels = to_labels(oci_spec, prefix_key=OCI_LABEL_PREFIX)
extra_labels.update(oci_labels)
except (FileNotFoundError, UndefinedOciImageSpec):
except (FileNotFoundError, UndefinedOciImageSpecError):
try:
# if not OCI, try label-schema
ls_spec = yaml.safe_load(
Expand Down
42 changes: 21 additions & 21 deletions packages/service-integration/src/service_integration/cli/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@
import rich
import typer
import yaml
from pydantic import ValidationError
from pydantic.main import BaseModel
from pydantic import BaseModel

from ..compose_spec_model import ComposeSpecification
from ..errors import InvalidLabelsError
from ..osparc_config import (
OSPARC_CONFIG_COMPOSE_SPEC_NAME,
OSPARC_CONFIG_DIRNAME,
OSPARC_CONFIG_METADATA_NAME,
OSPARC_CONFIG_RUNTIME_NAME,
DockerComposeOverwriteConfig,
MetadataConfig,
RuntimeConfig,
)


class InvalidLabelsError(ValueError):
template_msg = "Invalid build labels {build_labels}"
def _get_labels_or_raise(build_labels) -> dict[str, str]:
if isinstance(build_labels, list):
return dict(item.strip().split("=") for item in build_labels)
if isinstance(build_labels, dict):
return build_labels
if labels__root__ := build_labels.__root__:
assert isinstance(labels__root__, dict) # nosec
return labels__root__
raise InvalidLabelsError(build_labels=build_labels)


def _create_config_from_compose_spec(
compose_spec_path: Path,
docker_compose_overwrite_path: Path = Path("docker-compose.overwrite.yml"),
metadata_path: Path = Path("metadata.yml"),
service_specs_path: Path = Path("runtime-spec.yml"),
docker_compose_overwrite_path: Path,
metadata_path: Path,
service_specs_path: Path,
):
rich.print(f"Creating osparc config files from {compose_spec_path}")

Expand Down Expand Up @@ -58,16 +68,8 @@ def _save(service_name: str, filename: Path, model: BaseModel):
if build_labels := compose_spec.services[
service_name
].build.labels: # AttributeError if build is str
if isinstance(build_labels, list):
labels = dict(item.strip().split("=") for item in build_labels)
elif isinstance(build_labels, dict):
labels = build_labels
elif labels__root__ := build_labels.__root__:
assert isinstance(labels__root__, dict) # nosec
labels = labels__root__
else:
raise InvalidLabelsError(build_labels=build_labels)

labels: dict[str, str] = _get_labels_or_raise(build_labels)
meta_cfg = MetadataConfig.from_labels_annotations(labels)
_save(service_name, metadata_path, meta_cfg)

Expand All @@ -87,7 +89,6 @@ def _save(service_name: str, filename: Path, model: BaseModel):

except ( # noqa: PERF203
AttributeError,
ValidationError,
TypeError,
ValueError,
) as err:
Expand All @@ -113,11 +114,10 @@ def create_config(
] = Path("docker-compose.yml"),
):
"""Creates osparc configuration folder from a complete docker compose-spec"""
# TODO: sync defaults among CLI commands
config_dir = from_spec_file.parent / OSPARC_CONFIG_DIRNAME
project_cfg_path = config_dir / "docker-compose.overwrite.yml"
meta_cfg_path = config_dir / "metadata.yml"
runtime_cfg_path = config_dir / "runtime.yml"
project_cfg_path = config_dir / OSPARC_CONFIG_COMPOSE_SPEC_NAME
meta_cfg_path = config_dir / OSPARC_CONFIG_METADATA_NAME
runtime_cfg_path = config_dir / OSPARC_CONFIG_RUNTIME_NAME

meta_cfg_path.parent.mkdir(parents=True, exist_ok=True)
runtime_cfg_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ class ConfigNotFoundError(ServiceIntegrationError):
msg_template = "could not find any osparc config under {basedir}"


class UndefinedOciImageSpec(ServiceIntegrationError):
class UndefinedOciImageSpecError(ServiceIntegrationError):
...


class InvalidLabelsError(PydanticErrorMixin, ValueError):
template_msg = "Invalid build labels {build_labels}"
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
_logger = logging.getLogger(__name__)

OSPARC_CONFIG_DIRNAME = ".osparc"
OSPARC_CONFIG_COMPOSE_SPEC_NAME = "docker-compose.overwrite.yml"
OSPARC_CONFIG_METADATA_NAME = "metadata.yml"
OSPARC_CONFIG_RUNTIME_NAME = "runtime.yml"


SERVICE_KEY_FORMATS = {
Expand Down

0 comments on commit 6cc7039

Please sign in to comment.