Skip to content

Commit 9adc577

Browse files
Oleksandr Muzyka (EPAM)Hanna-Shalamitskaya-EPAM
authored andcommitted
Fix SAMM CLI usage
1 parent 6f4fcac commit 9adc577

File tree

9 files changed

+281
-89
lines changed

9 files changed

+281
-89
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
2+
#
3+
# See the AUTHORS file(s) distributed with this work for additional
4+
# information regarding authorship.
5+
#
6+
# This Source Code Form is subject to the terms of the Mozilla Public
7+
# License, v. 2.0. If a copy of the MPL was not distributed with this
8+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
9+
#
10+
# SPDX-License-Identifier: MPL-2.0
11+
12+
13+
SAMM_VERSION = "2.2.0"
14+
JAVA_CLI_VERSION = "2.10.2"

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/samm_cli/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import Any
1616

1717
from esmf_aspect_meta_model_python.samm_cli.constants import SAMMCLICommands, SAMMCLICommandTypes
18-
from scripts.download_samm_cli import download_samm_cli
18+
from esmf_aspect_meta_model_python.samm_cli.download import download_samm_cli
1919

2020

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

3838
return cli_path
3939

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/samm_cli/constants.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
# SPDX-License-Identifier: MPL-2.0
1111
"""Constants for SAMM CLI commands and types."""
1212

13+
from string import Template
14+
15+
from esmf_aspect_meta_model_python.constants import JAVA_CLI_VERSION, SAMM_VERSION
16+
17+
18+
class SAMMCliConstants:
19+
BASE_PATH = Template("https://github.com/eclipse-esmf/esmf-sdk/releases/download/v$version_number/$file_name")
20+
JAVA_CLI_VERSION = JAVA_CLI_VERSION
21+
LINUX_FILE_NAME = Template("samm-cli-$version_number-linux-x86_64.tar.gz")
22+
SAMM_VERSION = SAMM_VERSION
23+
WIN_FILE_NAME = Template("samm-cli-$version_number-windows-x86_64.zip")
24+
1325

1426
class SAMMCLICommands:
1527
"""SAMM CLI command names."""
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Download SAMM CLI.
2+
3+
Windows: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2-windows-x86_64.zip
4+
Linux: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2-linux-x86_64.tar.gz
5+
JAR: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2.jar
6+
"""
7+
8+
import os
9+
import platform
10+
import sys
11+
import zipfile
12+
13+
from pathlib import Path
14+
15+
import requests
16+
17+
from esmf_aspect_meta_model_python.samm_cli.constants import SAMMCliConstants as Const
18+
19+
20+
def get_samm_cli_file_name():
21+
"""Get a SAMM CLI file name for the current platform."""
22+
23+
if platform.system() == "Windows":
24+
file_name = Const.WIN_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
25+
elif platform.system() == "Linux":
26+
file_name = Const.LINUX_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
27+
else:
28+
raise NotImplementedError(
29+
f"Please download a SAMM CLI manually for your operation system from '{Const.BASE_PATH}'"
30+
)
31+
32+
return file_name
33+
34+
35+
def download_archive_file(url, archive_file):
36+
"""Download an archive file."""
37+
with open(archive_file, "wb") as f:
38+
print("Downloading %s" % archive_file)
39+
response = requests.get(url, allow_redirects=True, stream=True)
40+
content_len = response.headers.get("content-length")
41+
42+
if content_len is None:
43+
f.write(response.content)
44+
else:
45+
total_len = int(content_len)
46+
data_len = 0
47+
chunk = 4096
48+
progress_bar_len = 50
49+
50+
for content_data in response.iter_content(chunk_size=chunk):
51+
data_len += len(content_data)
52+
53+
f.write(content_data)
54+
55+
curr_progress = int(50 * data_len / total_len)
56+
sys.stdout.write(f"\r[{'*' * curr_progress}{' ' * (progress_bar_len - curr_progress)}]")
57+
sys.stdout.flush()
58+
59+
60+
def download_samm_cli():
61+
try:
62+
samm_cli_file_name = get_samm_cli_file_name()
63+
except NotImplementedError as error:
64+
print(error)
65+
else:
66+
print(f"Start downloading SAMM CLI {samm_cli_file_name}")
67+
url = Const.BASE_PATH.substitute(version_number=Const.JAVA_CLI_VERSION, file_name=samm_cli_file_name)
68+
dir_path = Path(__file__).resolve().parents[0]
69+
archive_file = os.path.join(dir_path, samm_cli_file_name)
70+
71+
download_archive_file(url, archive_file)
72+
print("\nSAMM CLI archive file downloaded")
73+
74+
print("Start extracting files")
75+
archive = zipfile.ZipFile(archive_file)
76+
extracted_files_path = os.path.join(dir_path, "samm-cli")
77+
for file in archive.namelist():
78+
archive.extract(file, extracted_files_path)
79+
archive.close()
80+
print("Done extracting files")
81+
82+
print("Deleting SAMM CLI archive file")
83+
os.remove(archive_file)

core/esmf-aspect-meta-model-python/esmf_aspect_meta_model_python/samm_meta_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
import rdflib
1818

19+
import esmf_aspect_meta_model_python.constants as const
20+
1921

2022
class SammUnitsGraph:
2123
"""Model units graph."""
2224

23-
SAMM_VERSION = "2.2.0"
25+
SAMM_VERSION = const.SAMM_VERSION
2426
UNIT_FILE_PATH = f"samm_aspect_meta_model/samm/unit/{SAMM_VERSION}/units.ttl"
2527
QUERY_TEMPLATE = Template("SELECT ?key ?value WHERE {$unit ?key ?value .}")
2628

core/esmf-aspect-meta-model-python/scripts/constants.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
from os.path import join
1313
from string import Template
1414

15-
16-
SAMM_VERSION = "2.2.0"
17-
JAVA_CLI_VERSION = "2.10.2"
18-
19-
20-
class SAMMCliConstants:
21-
BASE_PATH = Template("https://github.com/eclipse-esmf/esmf-sdk/releases/download/v$version_number/$file_name")
22-
JAVA_CLI_VERSION = JAVA_CLI_VERSION
23-
LINUX_FILE_NAME = Template("samm-cli-$version_number-linux-x86_64.tar.gz")
24-
SAMM_VERSION = SAMM_VERSION
25-
WIN_FILE_NAME = Template("samm-cli-$version_number-windows-x86_64.zip")
15+
from esmf_aspect_meta_model_python.constants import JAVA_CLI_VERSION, SAMM_VERSION
2616

2717

2818
class TestModelConstants:

core/esmf-aspect-meta-model-python/scripts/download_samm_cli.py

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,7 @@
55
JAR: https://github.com/eclipse-esmf/esmf-sdk/releases/download/v2.10.2/samm-cli-2.10.2.jar
66
"""
77

8-
import os
9-
from pathlib import Path
10-
import platform
11-
import requests
12-
import sys
13-
import zipfile
14-
15-
from scripts.constants import SAMMCliConstants as Const
16-
17-
18-
def get_samm_cli_file_name():
19-
"""Get a SAMM CLI file name for the current platform."""
20-
21-
if platform.system() == "Windows":
22-
file_name = Const.WIN_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
23-
elif platform.system() == "Linux":
24-
file_name = Const.LINUX_FILE_NAME.substitute(version_number=Const.JAVA_CLI_VERSION)
25-
else:
26-
raise NotImplementedError(
27-
f"Please download a SAMM CLI manually for your operation system from '{Const.BASE_PATH}'"
28-
)
29-
30-
return file_name
31-
32-
33-
def download_archive_file(url, archive_file):
34-
"""Download an archive file."""
35-
with open(archive_file, "wb") as f:
36-
print("Downloading %s" % archive_file)
37-
response = requests.get(url, allow_redirects=True, stream=True)
38-
content_len = response.headers.get('content-length')
39-
40-
if content_len is None:
41-
f.write(response.content)
42-
else:
43-
total_len = int(content_len)
44-
data_len = 0
45-
chunk = 4096
46-
progress_bar_len = 50
47-
48-
for content_data in response.iter_content(chunk_size=chunk):
49-
data_len += len(content_data)
50-
51-
f.write(content_data)
52-
53-
curr_progress = int(50 * data_len / total_len)
54-
sys.stdout.write(f"\r[{'*' * curr_progress}{' ' * (progress_bar_len - curr_progress)}]")
55-
sys.stdout.flush()
56-
57-
58-
def download_samm_cli():
59-
try:
60-
samm_cli_file_name = get_samm_cli_file_name()
61-
except NotImplementedError as error:
62-
print(error)
63-
else:
64-
print(f"Start downloading SAMM CLI {samm_cli_file_name}")
65-
url = Const.BASE_PATH.substitute(version_number=Const.JAVA_CLI_VERSION, file_name=samm_cli_file_name)
66-
dir_path = Path(__file__).resolve().parents[1]
67-
archive_file = os.path.join(dir_path, samm_cli_file_name)
68-
69-
download_archive_file(url, archive_file)
70-
print("\nSAMM CLI archive file downloaded")
71-
72-
print("Start extracting files")
73-
archive = zipfile.ZipFile(archive_file)
74-
for file in archive.namelist():
75-
archive.extract(file, "samm-cli")
76-
archive.close()
77-
print("Done extracting files.")
78-
79-
print("Deleting SAMM CLI archive file.")
80-
os.remove(archive_file)
8+
from esmf_aspect_meta_model_python.samm_cli.download import download_samm_cli
819

8210

8311
if __name__ == "__main__":

core/esmf-aspect-meta-model-python/tests/unit/samm_cli/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_init(get_client_path_mock, validate_client_mock):
4343
@mock.patch(f"{CLASS_PATH}._validate_client")
4444
def test_get_client_path(_, path_mock, join_mock):
4545
base_path_mock = mock.MagicMock(name="base_path")
46-
base_path_mock.parents = ["parent_0", "parent_1", "parent_2"]
46+
base_path_mock.parents = ["parent", "parent_1", "parent_2"]
4747
path_mock.return_value = path_mock
4848
path_mock.resolve.return_value = base_path_mock
4949
join_mock.return_value = "cli_path"
@@ -53,7 +53,7 @@ def test_get_client_path(_, path_mock, join_mock):
5353

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

5858

5959
@mock.patch(f"{BASE_PATH}.download_samm_cli")

0 commit comments

Comments
 (0)