From c353860d6232632b3ce6fc458e3e88502a3dc936 Mon Sep 17 00:00:00 2001 From: Igor Davidyuk Date: Thu, 1 Aug 2024 09:14:32 +0200 Subject: [PATCH] add platform version to SDK comparison Signed-off-by: Igor Davidyuk --- geti_sdk/__init__.py | 3 +- geti_sdk/_version.py | 15 ++++++ geti_sdk/geti.py | 37 +++++++++++++ geti_sdk/http_session/geti_session.py | 7 +-- setup.py | 2 +- tests/fixtures/unit_tests/geti.py | 4 +- .../http_session/test_geti_session_unit.py | 32 ----------- tests/pre-merge/unit/test_geti_unit.py | 54 +++++++++++++++++++ 8 files changed, 111 insertions(+), 43 deletions(-) create mode 100644 geti_sdk/_version.py diff --git a/geti_sdk/__init__.py b/geti_sdk/__init__.py index 4e12a879..c46cd914 100644 --- a/geti_sdk/__init__.py +++ b/geti_sdk/__init__.py @@ -110,9 +110,8 @@ """ +from ._version import __version__ # noqa: F401 from .geti import Geti from .prediction_visualization.visualizer import Visualizer -__version__ = "2.2.0" - __all__ = ["Geti", "Visualizer"] diff --git a/geti_sdk/_version.py b/geti_sdk/_version.py new file mode 100644 index 00000000..839c537d --- /dev/null +++ b/geti_sdk/_version.py @@ -0,0 +1,15 @@ +# Copyright (C) 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions +# and limitations under the License. + +__version__ = "2.3.0" diff --git a/geti_sdk/geti.py b/geti_sdk/geti.py index fd0631df..1e4d2dd1 100644 --- a/geti_sdk/geti.py +++ b/geti_sdk/geti.py @@ -18,10 +18,13 @@ from typing import Dict, List, Optional, Sequence, Tuple, Union import numpy as np +from packaging.version import Version from geti_sdk.data_models.enums.dataset_format import DatasetFormat from geti_sdk.import_export.import_export_module import GetiIE +from geti_sdk.platform_versions import GETI_116_VERSION +from ._version import __version__ as sdk_version_string from .annotation_readers import AnnotationReader, DatumAnnotationReader from .data_models import ( Dataset, @@ -57,6 +60,8 @@ DEFAULT_LOG_LEVEL = logging.INFO DEFAULT_LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" +logging.captureWarnings(True) + class Geti: """ @@ -170,10 +175,15 @@ def __init__( "please update the `server_config` accordingly." ) + # Set the Intel Geti SDK version + self.sdk_version = Version(sdk_version_string) # Initialize session and get workspace id self.session = GetiSession( server_config=server_config, ) + # Now that the connection to the server is established, check the platform version + self._check_platform_version() + # Get workspace ID if workspace_id is None: workspace_id = get_default_workspace_id(self.session) self.workspace_id = workspace_id @@ -189,6 +199,33 @@ def __init__( # Cache of deployment clients for projects in the workspace self._deployment_clients: Dict[str, DeploymentClient] = {} + def _check_platform_version(self) -> None: + """ + Check the version of the Intel® Geti™ server that this `Geti` instance is + connected to. If the version is not supported by the SDK, a warning will be + issued. + + :raises: ValueError if the Intel® Geti™ server version is not supported by the + Intel® Geti™ SDK. + """ + # Get the build version without a timestamp + platform_version = self.session.version.version + # Check if the platform version is newer than the SDK version + if platform_version > self.sdk_version: + warnings.warn( + f"The Intel® Geti™ server version {platform_version} is newer than " + f"the Geti SDK version {self.sdk_version}. Some features may not be " + "supported and you may encounter errors.\n" + "Please update the Intel Geti SDK to the latest version" + "with `pip install --upgrade geti-sdk`." + ) + # Check if the platform version is older than the last supported version + if self.session.version < GETI_116_VERSION: + raise ValueError( + "The Intel® Geti™ server version is not supported by this Intel Geti SDK package. Please " + "update the Intel® Geti™ server to version 2.0 or later, or use a previous version of the SDK." + ) + @property def projects(self) -> List[Project]: """ diff --git a/geti_sdk/http_session/geti_session.py b/geti_sdk/http_session/geti_session.py index 20f948fc..20223701 100644 --- a/geti_sdk/http_session/geti_session.py +++ b/geti_sdk/http_session/geti_session.py @@ -26,7 +26,7 @@ from requests.structures import CaseInsensitiveDict from urllib3.exceptions import InsecureRequestWarning -from geti_sdk.platform_versions import GETI_116_VERSION, GetiVersion +from geti_sdk.platform_versions import GetiVersion from .exception import GetiRequestException from .server_config import ServerCredentialConfig, ServerTokenConfig @@ -110,11 +110,6 @@ def __init__( # Get server version self._product_info = self._get_product_info_and_set_api_version() self._organization_id: Optional[str] = self._get_organization_id() - if self.version < GETI_116_VERSION: - raise ValueError( - "The Intel® Geti™ server version is not supported by this SDK. Please " - "update the Intel® Geti™ server to version 2.0 or later, or us the previous version of the SDK." - ) @property @cache diff --git a/setup.py b/setup.py index f3ea4e4e..3f7de6b1 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ def get_requirements(filename: str) -> List[str]: return required_packages -with open("geti_sdk/__init__.py", "r", encoding="utf-8") as init_file: +with open("geti_sdk/_version.py", "r", encoding="utf-8") as init_file: for line in init_file: line = line.strip() if line.startswith("__version__"): diff --git a/tests/fixtures/unit_tests/geti.py b/tests/fixtures/unit_tests/geti.py index 0aa5c8c7..63e97c66 100644 --- a/tests/fixtures/unit_tests/geti.py +++ b/tests/fixtures/unit_tests/geti.py @@ -52,8 +52,8 @@ def _mocked_session_factory( mocker.patch( "geti_sdk.http_session.geti_session.GetiSession._get_product_info_and_set_api_version", return_value={ - "product-version": "2.0.0", - "build-version": "2.0.0-test-20240417130126", + "product-version": "2.3.0", + "build-version": "2.3.0-test-20240417130126", "smtp-defined": "True", "environment": "on-prem", }, diff --git a/tests/pre-merge/unit/http_session/test_geti_session_unit.py b/tests/pre-merge/unit/http_session/test_geti_session_unit.py index 74d220e5..c0b5d485 100644 --- a/tests/pre-merge/unit/http_session/test_geti_session_unit.py +++ b/tests/pre-merge/unit/http_session/test_geti_session_unit.py @@ -92,35 +92,3 @@ def test_authentication_onprem( # Username and password will soon be deprecated for on-prem with pytest.warns(): GetiSession(fxt_mocked_server_credential_config) - - def test_authentication_legacy( - self, - fxt_mocked_server_credential_config, - mocker: MockerFixture, - ): - # Arrange - mocker.patch( - "geti_sdk.http_session.geti_session.GetiSession.platform_serving_mode", - ONPREM_MODE, - ) - mocker.patch( - "geti_sdk.http_session.geti_session.GetiSession._get_product_info_and_set_api_version", - return_value={ - "product-version": "1.8.0", - "build-version": "1.8.0-test-20240417130126", - "smtp-defined": "True", - "environment": "on-prem", - }, - ) - mocker.patch( - "geti_sdk.http_session.geti_session.GetiSession._get_organization_id", - return_value="dummy_org_id", - ) - mocker.patch( - "geti_sdk.http_session.geti_session.GetiSession.authenticate_with_password" - ) - - # Act - # Legacy platforms are no longer supported - with pytest.raises(ValueError): - GetiSession(fxt_mocked_server_credential_config) diff --git a/tests/pre-merge/unit/test_geti_unit.py b/tests/pre-merge/unit/test_geti_unit.py index ee5a770d..45dec52f 100644 --- a/tests/pre-merge/unit/test_geti_unit.py +++ b/tests/pre-merge/unit/test_geti_unit.py @@ -21,6 +21,7 @@ from geti_sdk import Geti from geti_sdk.data_models import Project from geti_sdk.http_session import ServerCredentialConfig, ServerTokenConfig +from geti_sdk.http_session.geti_session import ONPREM_MODE from tests.helpers.constants import DUMMY_HOST, DUMMY_PASSWORD, DUMMY_TOKEN, DUMMY_USER @@ -72,6 +73,59 @@ def test_exception_flow_init( geti = Geti(host=DUMMY_HOST, token=DUMMY_TOKEN) assert "x-api-key" in geti.session.headers.keys() + def test_connection_to_legacy_platform( + self, + fxt_mocked_server_credential_config, + fxt_mocked_session_factory, + mocker: MockerFixture, + ): + # Arrange + mocker.patch( + "geti_sdk.http_session.geti_session.GetiSession.platform_serving_mode", + ONPREM_MODE, + ) + mocker.patch( + "geti_sdk.http_session.geti_session.GetiSession._get_product_info_and_set_api_version", + return_value={ + "product-version": "1.8.0", + "build-version": "1.8.0-test-20240417130126", + "smtp-defined": "True", + "environment": "on-prem", + }, + ) + mocker.patch( + "geti_sdk.http_session.geti_session.GetiSession._get_organization_id", + return_value="dummy_org_id", + ) + mocker.patch( + "geti_sdk.http_session.geti_session.GetiSession.authenticate_with_password" + ) + + # Act + # Legacy platforms are no longer supported + with pytest.raises(ValueError): + Geti(host=DUMMY_HOST, server_config=fxt_mocked_server_credential_config) + + def test_connection_to_legacynewer_platform( + self, + fxt_mocked_session_factory, + mocker: MockerFixture, + ): + # Arrange + mocker.patch("geti_sdk.geti.GetiSession", new=fxt_mocked_session_factory) + mock_session = fxt_mocked_session_factory() + mocker.patch("geti_sdk.geti.get_default_workspace_id", return_value=1) + # Decrease the minor version for the SDK + platform_version = str(mock_session.version.version).split(".") + mocker.patch( + "geti_sdk.geti.sdk_version_string", + f"{platform_version[0]}.{int(platform_version[1]) - 1}.0", + ) + + # Act + with pytest.warns(): + Geti(host=DUMMY_HOST, token=DUMMY_TOKEN) + def test_logout(self, mocker: MockerFixture, fxt_mocked_geti: Geti): # Arrange mock_logout = mocker.patch.object(fxt_mocked_geti.session, "logout")