Skip to content

Commit

Permalink
#245: Implemented support for Json for language definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tomuben committed Dec 3, 2024
1 parent 11bf21c commit 61d0d55
Show file tree
Hide file tree
Showing 11 changed files with 559 additions and 43 deletions.
2 changes: 1 addition & 1 deletion doc/changes/changes_1.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ t.b.d.

## Features

n/a
- #245: Implemented support for Json for language definition

## Refactoring

Expand Down
36 changes: 25 additions & 11 deletions exasol/slc/internal/tasks/build/docker_flavor_image_task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Dict
from typing import Dict, Optional

from exasol_integration_test_docker_environment.lib.base.flavor_task import (
FlavorBaseTask,
Expand All @@ -15,6 +15,8 @@
DockerAnalyzeImageTask,
)

from exasol.slc.models.language_definition_model import LanguageDefinitionsModel


class DockerFlavorAnalyzeImageTask(DockerAnalyzeImageTask, FlavorBaseTask):
# TODO change task inheritance with composition.
Expand Down Expand Up @@ -56,7 +58,15 @@ def get_additional_build_directories_mapping(self) -> Dict[str, str]:
"""
return {}

def get_path_in_flavor(self):
def get_language_definition(self) -> str:
"""
Called by the constructor to get a language definition file which will be validated against to
the language definition JSON and (if validations succeeded) copied to the temporary build directory.
:return: string with source path of language definition JSON or an empty string
"""
return ""

def get_path_in_flavor(self) -> Optional[Path]:
"""
Called by the constructor to get the path to the build context of the build step within the flavor path.
Sub classes need to implement this method.
Expand Down Expand Up @@ -94,18 +104,22 @@ def get_mapping_of_build_files_and_directories(self) -> Dict[str, str]:
build_step_path = self.get_build_step_path()
result = {self.build_step: str(build_step_path)}
result.update(self.additional_build_directories_mapping)
if language_definition := self.get_language_definition():
lang_def_path = self.get_path_in_flavor_path() / language_definition
LanguageDefinitionsModel.model_validate_json(
lang_def_path.read_text(), strict=True
)
result.update({"language_definitions.json": str(lang_def_path)})
return result

def get_build_step_path(self):
path_in_flavor = ( # pylint: disable=assignment-from-none
self.get_path_in_flavor()
)
if path_in_flavor is None:
build_step_path_in_flavor = Path(self.build_step)
def get_build_step_path(self) -> Path:
return self.get_path_in_flavor_path() / self.get_build_step()

def get_path_in_flavor_path(self) -> Path:
if path_in_flavor := self.get_path_in_flavor():
return Path(self.flavor_path) / path_in_flavor
else:
build_step_path_in_flavor = Path(path_in_flavor).joinpath(self.build_step)
build_step_path = Path(self.flavor_path).joinpath(build_step_path_in_flavor)
return build_step_path
return Path(self.flavor_path)

def get_dockerfile(self) -> str:
return str(self.get_build_step_path().joinpath("Dockerfile"))
3 changes: 2 additions & 1 deletion exasol/slc/internal/tasks/upload/language_def_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def parse_language_definition(
if parsed_url.hostname:
raise ValueError(f"Invalid language definition: '{lang_def}'")
slc_parameters = [
SLCParameter(key, value) for key, value in parse_qs(parsed_url.query).items()
SLCParameter(key=key, value=value)
for key, value in parse_qs(parsed_url.query).items()
]
try:
udf_client_path = _parse_udf_client_path(parsed_url.fragment)
Expand Down
33 changes: 33 additions & 0 deletions exasol/slc/models/language_definition_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import PurePosixPath
from typing import List

from pydantic import BaseModel


class SLCLanguage(str, Enum):
Java = "java"
Python3 = "python"
R = "r"


class SLCParameter(BaseModel):
"""
Key value pair of a parameter passed to the Udf client. For example: `lang=java`
"""

key: str
value: List[str]


class UdfClientRelativePath(BaseModel):
"""
Path to the udf client relative to the Script Languages Container root path.
For example `/exaudf/exaudfclient_py3`
"""

executable: PurePosixPath

def __str__(self) -> str:
return str(self.executable)
34 changes: 5 additions & 29 deletions exasol/slc/models/language_definition_components.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import PurePosixPath
from typing import List, Optional, Union
from urllib.parse import ParseResult, urlencode, urlunparse


class SLCLanguage(Enum):
Java = "java"
Python3 = "python"
R = "r"


@dataclass
class SLCParameter:
"""
Key value pair of a parameter passed to the Udf client. For example: `lang=java`
"""

key: str
value: List[str]
from exasol.slc.models.language_definition_common import (
SLCLanguage,
SLCParameter,
UdfClientRelativePath,
)


@dataclass
Expand All @@ -35,19 +24,6 @@ def __str__(self) -> str:
return f"buckets/{self.bucketfs_name}/{self.bucket_name}/" f"{self.executable}"


@dataclass
class UdfClientRelativePath:
"""
Path to the udf client relative to the Script Languages Container root path.
For example `/exaudf/exaudfclient_py3`
"""

executable: PurePosixPath

def __str__(self) -> str:
return str(self.executable)


@dataclass
class ChrootPath:
"""
Expand Down
30 changes: 30 additions & 0 deletions exasol/slc/models/language_definition_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from dataclasses import dataclass
from typing import Annotated, List

from pydantic import BaseModel, Field

from exasol.slc.models.language_definition_common import (
SLCLanguage,
SLCParameter,
UdfClientRelativePath,
)


class LanguageDefinition(BaseModel):
"""
Contains information about a supported language and the respective path of the UDF client of an Script-Languages-Container.
"""

protocol: str
default_alias: str
language: SLCLanguage
parameters: List[SLCParameter]
udf_client_path: UdfClientRelativePath


class LanguageDefinitionsModel(BaseModel):
"""
Contains information about all supported languages and the respective path of the UDF client of an Script-Languages-Container.
"""

language_definitions: List[LanguageDefinition]
Loading

0 comments on commit 61d0d55

Please sign in to comment.