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

Check new version from pypi instead of our api: fix pysimai version check #110

Merged
merged 3 commits into from
Nov 22, 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
29 changes: 9 additions & 20 deletions src/ansys/simai/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from typing import List, Optional

from pydantic import ValidationError
from semver.version import Version

from ansys.simai.core import __version__
from ansys.simai.core.api.client import ApiClient
Expand Down Expand Up @@ -54,6 +53,7 @@
)
from ansys.simai.core.utils.config_file import get_config
from ansys.simai.core.utils.configuration import ClientConfig
from ansys.simai.core.utils.misc import notify_if_package_outdated
from ansys.simai.core.utils.typing import steal_kwargs_type_on_method

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -324,26 +324,15 @@
raise errors[0]
raise MultipleErrors(errors)

def _check_for_new_version(self, client_name="ansys.simai.core", current_version=__version__):
def _check_for_new_version(self) -> None:
awoimbee marked this conversation as resolved.
Show resolved Hide resolved
try:
latest_version = self._api._get(f"info/{client_name}/version")["version"]
version_current = Version.parse(current_version)
version_latest = Version.parse(latest_version)
except (SimAIError, ValueError):
pass
else:
if version_current < version_latest:
warn_template = (
f"A new version of {client_name} is %s. "
"Upgrade to get new features and ensure compatibility with the server."
)
if (
version_current.major < version_latest.major
or version_current.minor < version_latest.minor
):
raise SimAIError(warn_template % "required")
else:
logger.warning(warn_template % "available")
latest_version = self._api._get("https://pypi.org/pypi/ansys-simai-core/json")["info"][
"version"
]
except (SimAIError, KeyError) as e:
logger.debug(f"Could not query package version on pypi: {e}")
return None

Check warning on line 334 in src/ansys/simai/core/client.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/simai/core/client.py#L332-L334

Added lines #L332 - L334 were not covered by tests
notify_if_package_outdated("ansys-simai-core", __version__, latest_version)


from_config = SimAIClient.from_config
26 changes: 26 additions & 0 deletions src/ansys/simai/core/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
# SOFTWARE.

import getpass
import logging
from typing import Any, Dict, Optional

from semver.version import Version

logger = logging.getLogger(__name__)


def prompt_for_input(name: str, hide_input: Optional[bool] = False):
return input(f"{name}:") if not hide_input else getpass.getpass(f"{name}:")
Expand All @@ -41,3 +46,24 @@
for k in keys:
obj = obj.get(k, {}) or {}
return obj or default


def notify_if_package_outdated(package: str, current_version: str, latest_version: str):
try:
version_current = Version.parse(current_version)
version_latest = Version.parse(latest_version)
except ValueError as e:
logger.debug(f"Could not parse package version: {e}")

Check warning on line 56 in src/ansys/simai/core/utils/misc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/simai/core/utils/misc.py#L55-L56

Added lines #L55 - L56 were not covered by tests
else:
if version_current < version_latest:
warn_template = (
f"A new version of {package} is %s. "
"Upgrade to get new features and ensure compatibility with the server."
)
if (
version_current.major < version_latest.major
or version_current.minor < version_latest.minor
awoimbee marked this conversation as resolved.
Show resolved Hide resolved
):
logger.critical(warn_template % "required")
else:
logger.warning(warn_template % "available")
47 changes: 14 additions & 33 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import functools
from pathlib import Path

import pytest
Expand All @@ -40,19 +39,27 @@ def test_client_creation_invalid_config():
SimAIClient.from_config(path=Path(__file__).resolve())


@pytest.mark.parametrize(
"local_ver,latest_ver,expected",
[
("1.1.0", "1.1.1", "available."),
("1.0.9-rc8", "1.0.9", "available."),
("1.0.9", "1.9.0", "required."),
],
)
@responses.activate
def test_client_version_auto_warn(caplog, mocker):
def test_client_version_auto_warn(caplog, mocker, local_ver, latest_ver, expected):
"""WHEN the SDK version is slightly outdated compared to what the API responds
THEN a warning is printed
"""
mocker.patch(
"ansys.simai.core.client.SimAIClient._check_for_new_version",
functools.partialmethod(SimAIClient._check_for_new_version, current_version="1.1.0"),
"ansys.simai.core.client.__version__",
local_ver,
)
responses.add(
responses.GET,
"https://test.test/info/ansys.simai.core/version",
json={"version": "1.1.1"},
"https://pypi.org/pypi/ansys-simai-core/json",
json={"info": {"version": latest_ver}},
status=200,
)
SimAIClient(
Expand All @@ -62,30 +69,4 @@ def test_client_version_auto_warn(caplog, mocker):
no_sse_connection=True,
skip_version_check=False,
)
assert "A new version of ansys.simai.core is available" in caplog.text


@responses.activate
def test_client_version_auto_error(caplog, mocker):
"""WHEN the SDK version is grossly outdated compared to what the API responds
THEN an exception is raised
"""
mocker.patch(
"ansys.simai.core.client.SimAIClient._check_for_new_version",
functools.partialmethod(SimAIClient._check_for_new_version, current_version="1.0.9-rc8"),
)
responses.add(
responses.GET,
"https://test.test/info/ansys.simai.core/version",
json={"version": "1.9.0"},
status=200,
)
with pytest.raises(err.SimAIError) as exc:
SimAIClient(
url="https://test.test",
organization="dummy",
_disable_authentication=True,
no_sse_connection=True,
skip_version_check=False,
)
assert "A new version of ansys.simai.core is required" in str(exc.value)
assert f"A new version of ansys-simai-core is {expected}" in caplog.text
Loading