Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#245: Implemented support for Json for language definition #246

Merged
merged 7 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 32 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,11 @@
DockerAnalyzeImageTask,
)

from exasol.slc.models.language_definition_model import (
LANGUAGE_DEFINITON_SCHEMA_VERSION,
LanguageDefinitionsModel,
)


class DockerFlavorAnalyzeImageTask(DockerAnalyzeImageTask, FlavorBaseTask):
# TODO change task inheritance with composition.
Expand Down Expand Up @@ -56,7 +61,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
the language definition JSON Schema 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 +107,26 @@ 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
model = LanguageDefinitionsModel.model_validate_json(
lang_def_path.read_text(), strict=True
)
if model.schema_version != LANGUAGE_DEFINITON_SCHEMA_VERSION:
raise RuntimeError(
f"Unsupported schema version. Version from JSON: {model.schema_version}. Expected: {LANGUAGE_DEFINITON_SCHEMA_VERSION}"
)
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
34 changes: 34 additions & 0 deletions exasol/slc/models/language_definition_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from dataclasses import dataclass
from typing import List

from pydantic import BaseModel

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

LANGUAGE_DEFINITON_SCHEMA_VERSION = 1


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

protocol: str
aliases: List[str]
language: SLCLanguage
parameters: List[SLCParameter]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add environment variables, for example for the option parser

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, let's do it if we decided to really use it, because the alternative would be to use the parameters field

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.
"""

schema_version: int

language_definitions: List[LanguageDefinition]
Loading
Loading