Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0


SAMM_VERSION = "2.2.0"
JAVA_CLI_VERSION = "2.10.2"
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Any

from esmf_aspect_meta_model_python.samm_cli.constants import SAMMCLICommands, SAMMCLICommandTypes
from scripts.download_samm_cli import download_samm_cli
from esmf_aspect_meta_model_python.samm_cli.download import download_samm_cli


class SammCli:
Expand All @@ -33,7 +33,7 @@ def __init__(self):
def _get_client_path():
"""Get path to the SAMM CLI executable file."""
base_path = Path(__file__).resolve()
cli_path = join(base_path.parents[2], "samm-cli", "samm.exe")
cli_path = join(base_path.parents[0], "samm-cli", "samm.exe")

return cli_path

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
# SPDX-License-Identifier: MPL-2.0
"""Constants for SAMM CLI commands and types."""

from string import Template

from esmf_aspect_meta_model_python.constants import JAVA_CLI_VERSION, SAMM_VERSION


class SAMMCliConstants:
BASE_PATH = Template("https://github.com/eclipse-esmf/esmf-sdk/releases/download/v$version_number/$file_name")
JAVA_CLI_VERSION = JAVA_CLI_VERSION
LINUX_FILE_NAME = Template("samm-cli-$version_number-linux-x86_64.tar.gz")
SAMM_VERSION = SAMM_VERSION
WIN_FILE_NAME = Template("samm-cli-$version_number-windows-x86_64.zip")


class SAMMCLICommands:
"""SAMM CLI command names."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Download SAMM CLI.

Windows: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2-windows-x86_64.zip
Linux: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2-linux-x86_64.tar.gz
JAR: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2.jar
"""

import os
import platform
import sys
import zipfile

from pathlib import Path

import requests

from esmf_aspect_meta_model_python.samm_cli.constants import SAMMCliConstants as Const


def get_samm_cli_file_name():
"""Get a SAMM CLI file name for the current platform."""

if platform.system() == "Windows":
file_name = Const.WIN_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
elif platform.system() == "Linux":
file_name = Const.LINUX_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
else:
raise NotImplementedError(
f"Please download a SAMM CLI manually for your operation system from '{Const.BASE_PATH}'"
)

return file_name


def download_archive_file(url, archive_file):
"""Download an archive file."""
with open(archive_file, "wb") as f:
print("Downloading %s" % archive_file)
response = requests.get(url, allow_redirects=True, stream=True)
content_len = response.headers.get("content-length")

if content_len is None:
f.write(response.content)
else:
total_len = int(content_len)
data_len = 0
chunk = 4096
progress_bar_len = 50

for content_data in response.iter_content(chunk_size=chunk):
data_len += len(content_data)

f.write(content_data)

curr_progress = int(50 * data_len / total_len)
sys.stdout.write(f"\r[{'*' * curr_progress}{' ' * (progress_bar_len - curr_progress)}]")
sys.stdout.flush()


def download_samm_cli():
try:
samm_cli_file_name = get_samm_cli_file_name()
except NotImplementedError as error:
print(error)
else:
print(f"Start downloading SAMM CLI {samm_cli_file_name}")
url = Const.BASE_PATH.substitute(version_number=Const.JAVA_CLI_VERSION, file_name=samm_cli_file_name)
dir_path = Path(__file__).resolve().parents[0]
archive_file = os.path.join(dir_path, samm_cli_file_name)

download_archive_file(url, archive_file)
print("\nSAMM CLI archive file downloaded")

print("Start extracting files")
archive = zipfile.ZipFile(archive_file)
extracted_files_path = os.path.join(dir_path, "samm-cli")
for file in archive.namelist():
archive.extract(file, extracted_files_path)
archive.close()
print("Done extracting files")

print("Deleting SAMM CLI archive file")
os.remove(archive_file)
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

import rdflib

import esmf_aspect_meta_model_python.constants as const


class SammUnitsGraph:
"""Model units graph."""

SAMM_VERSION = "2.2.0"
SAMM_VERSION = const.SAMM_VERSION
UNIT_FILE_PATH = f"samm_aspect_meta_model/samm/unit/{SAMM_VERSION}/units.ttl"
QUERY_TEMPLATE = Template("SELECT ?key ?value WHERE {$unit ?key ?value .}")

Expand Down
12 changes: 1 addition & 11 deletions core/esmf-aspect-meta-model-python/scripts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@
from os.path import join
from string import Template


SAMM_VERSION = "2.2.0"
JAVA_CLI_VERSION = "2.10.2"


class SAMMCliConstants:
BASE_PATH = Template("https://github.com/eclipse-esmf/esmf-sdk/releases/download/v$version_number/$file_name")
JAVA_CLI_VERSION = JAVA_CLI_VERSION
LINUX_FILE_NAME = Template("samm-cli-$version_number-linux-x86_64.tar.gz")
SAMM_VERSION = SAMM_VERSION
WIN_FILE_NAME = Template("samm-cli-$version_number-windows-x86_64.zip")
from esmf_aspect_meta_model_python.constants import JAVA_CLI_VERSION, SAMM_VERSION


class TestModelConstants:
Expand Down
74 changes: 1 addition & 73 deletions core/esmf-aspect-meta-model-python/scripts/download_samm_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,7 @@
JAR: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2.jar
"""

import os
from pathlib import Path
import platform
import requests
import sys
import zipfile

from scripts.constants import SAMMCliConstants as Const


def get_samm_cli_file_name():
"""Get a SAMM CLI file name for the current platform."""

if platform.system() == "Windows":
file_name = Const.WIN_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
elif platform.system() == "Linux":
file_name = Const.LINUX_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
else:
raise NotImplementedError(
f"Please download a SAMM CLI manually for your operation system from '{Const.BASE_PATH}'"
)

return file_name


def download_archive_file(url, archive_file):
"""Download an archive file."""
with open(archive_file, "wb") as f:
print("Downloading %s" % archive_file)
response = requests.get(url, allow_redirects=True, stream=True)
content_len = response.headers.get('content-length')

if content_len is None:
f.write(response.content)
else:
total_len = int(content_len)
data_len = 0
chunk = 4096
progress_bar_len = 50

for content_data in response.iter_content(chunk_size=chunk):
data_len += len(content_data)

f.write(content_data)

curr_progress = int(50 * data_len / total_len)
sys.stdout.write(f"\r[{'*' * curr_progress}{' ' * (progress_bar_len - curr_progress)}]")
sys.stdout.flush()


def download_samm_cli():
try:
samm_cli_file_name = get_samm_cli_file_name()
except NotImplementedError as error:
print(error)
else:
print(f"Start downloading SAMM CLI {samm_cli_file_name}")
url = Const.BASE_PATH.substitute(version_number=Const.JAVA_CLI_VERSION, file_name=samm_cli_file_name)
dir_path = Path(__file__).resolve().parents[1]
archive_file = os.path.join(dir_path, samm_cli_file_name)

download_archive_file(url, archive_file)
print("\nSAMM CLI archive file downloaded")

print("Start extracting files")
archive = zipfile.ZipFile(archive_file)
for file in archive.namelist():
archive.extract(file, "samm-cli")
archive.close()
print("Done extracting files.")

print("Deleting SAMM CLI archive file.")
os.remove(archive_file)
from esmf_aspect_meta_model_python.samm_cli.download import download_samm_cli


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_init(get_client_path_mock, validate_client_mock):
@mock.patch(f"{CLASS_PATH}._validate_client")
def test_get_client_path(_, path_mock, join_mock):
base_path_mock = mock.MagicMock(name="base_path")
base_path_mock.parents = ["parent_0", "parent_1", "parent_2"]
base_path_mock.parents = ["parent", "parent_1", "parent_2"]
path_mock.return_value = path_mock
path_mock.resolve.return_value = base_path_mock
join_mock.return_value = "cli_path"
Expand All @@ -53,7 +53,7 @@ def test_get_client_path(_, path_mock, join_mock):

assert result == "cli_path"
path_mock.resolve.assert_called_once()
join_mock.assert_called_once_with("parent_2", "samm-cli", "samm.exe")
join_mock.assert_called_once_with("parent", "samm-cli", "samm.exe")


@mock.patch(f"{BASE_PATH}.download_samm_cli")
Expand Down
Loading