Skip to content

Commit

Permalink
check new version from pypi instead of our api
Browse files Browse the repository at this point in the history
  • Loading branch information
awoimbee committed Nov 19, 2024
1 parent 9612243 commit d67aef0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
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 warn_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 @@ def wait(self) -> None:
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:
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
warn_package_outdated("ansys-simai-core", __version__, latest_version)


from_config = SimAIClient.from_config
28 changes: 28 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,15 @@
# SOFTWARE.

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

from semver.version import Version

from ansys.simai.core.errors import SimAIError

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 +48,24 @@ def dict_get(obj: dict, *keys: str, default=None):
for k in keys:
obj = obj.get(k, {}) or {}
return obj or default


def warn_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 58 in src/ansys/simai/core/utils/misc.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/simai/core/utils/misc.py#L57-L58

Added lines #L57 - L58 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
):
raise SimAIError(warn_template % "required")
else:
logger.warning(warn_template % "available")
22 changes: 11 additions & 11 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 @@ -46,13 +45,13 @@ def test_client_version_auto_warn(caplog, mocker):
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__",
"1.1.0",
)
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": "1.1.1"}},
status=200,
)
SimAIClient(
Expand All @@ -62,7 +61,7 @@ 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
assert "A new version of ansys-simai-core is available" in caplog.text


@responses.activate
Expand All @@ -71,13 +70,14 @@ def test_client_version_auto_error(caplog, mocker):
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"),
"ansys.simai.core.client.__version__",
"1.0.9-rc8",
)

responses.add(
responses.GET,
"https://test.test/info/ansys.simai.core/version",
json={"version": "1.9.0"},
"https://pypi.org/pypi/ansys-simai-core/json",
json={"info": {"version": "1.9.0"}},
status=200,
)
with pytest.raises(err.SimAIError) as exc:
Expand All @@ -88,4 +88,4 @@ def test_client_version_auto_error(caplog, mocker):
no_sse_connection=True,
skip_version_check=False,
)
assert "A new version of ansys.simai.core is required" in str(exc.value)
assert "A new version of ansys-simai-core is required" in str(exc.value)

0 comments on commit d67aef0

Please sign in to comment.