diff --git a/setup.py b/setup.py index 8c480d3e..eaae81fe 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,8 @@ """ import os -from typing import Tuple, List, Dict +from typing import Dict, List, Tuple + from setuptools import find_packages, setup # default variables to be overwritten by the version.py file diff --git a/src/sparsezoo/__init__.py b/src/sparsezoo/__init__.py index acdfe7a6..78d04453 100644 --- a/src/sparsezoo/__init__.py +++ b/src/sparsezoo/__init__.py @@ -15,7 +15,8 @@ # limitations under the License. """ -Functionality for accessing models, recipes, and supporting files in the SparseZoo +- Functionality for accessing models, recipes, and supporting files in the SparseZoo +- Notify the user the last pypi package version """ # flake8: noqa @@ -25,3 +26,12 @@ from .main import * from .models.zoo import * from .objects import * +from .package import * + + +from sparsezoo.package import check_package_version as _check_package_version + +_check_package_version( + package_name=__name__ if is_release else f"{__name__}-nightly", + package_version=version, +) diff --git a/src/sparsezoo/package.py b/src/sparsezoo/package.py new file mode 100644 index 00000000..8c8d049d --- /dev/null +++ b/src/sparsezoo/package.py @@ -0,0 +1,112 @@ +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. +# +# 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. + +import logging +import os +import threading +from typing import Optional + +import requests + +from sparsezoo.requests import LATEST_PACKAGE_VERSION_URL + + +LOGGER = logging.getLogger(__name__) + + +def package_version_check_request( + package_name: str, package_version: str, package_integration: Optional[str] +): + """ + Make an api call to api-neuralmagic.com, retrieve payload and check if the + user is on the latest package version. Lambda: nm-get-latest-version + + :param package_name: package name of the client + :param package_version: package version of the client + :param package_integration: package integration of the client + """ + url = ( + f"{LATEST_PACKAGE_VERSION_URL}?" + f"packages={package_name}" + f"&integrations={package_integration}" + f"&versions={package_version}" + ) + try: + response = requests.get(url) # no token-headers required + response.raise_for_status() + response_json = response.json() + + for checked_package in response_json["checked_packages"]: + if not checked_package["is_latest"]: + LOGGER.warning( + "WARNING: " + f"You are using {checked_package['package_name']} " + f"version {checked_package['user_package_version']} " + f"however version {checked_package['latest_package_version']} " + "is available.\n" + "Consider upgrading via executing the " + f"'pip install --upgrade' command.\n" + "To turn off set an environmental variable " + "NM_VERSION_CHECK=false" + ) + except Exception as err: + raise RuntimeError( + f"Execption occured in the Neural Magic's internal version-api check\n{err}" + ) + + +def version_check_execution_condition( + package_name: str, package_version: str, package_integration: Optional[str] +): + """ + Check if conditions are met to run the version-check api + + :param package_name: package name of the client + :param package_version: package version of the client + :param package_integration: package integration of the client + """ + if ( + os.getenv("NM_VERSION_CHECK") is None + or os.getenv("NM_VERSION_CHECK").lower().strip() != "false" + ): + package_version_check_request( + package_name=package_name, + package_integration=package_integration, + package_version=package_version, + ) + else: + LOGGER.info( + "Skipping Neural Magic's internal code exection: " + "latest package version check" + ) + + +def check_package_version( + package_name: str, package_version: str, package_integration: Optional[str] = None +): + """ + Run a background thread to run version-check api + + :param package_name: package name of the client + :param package_version: package version of the client + :param package_integration: package integration of the client + """ + threading.Thread( + target=version_check_execution_condition, + kwargs={ + "package_name": package_name, + "package_version": package_version, + "package_integration": package_integration, + }, + ).start() diff --git a/src/sparsezoo/requests/base.py b/src/sparsezoo/requests/base.py index 67f075ba..b59c5174 100644 --- a/src/sparsezoo/requests/base.py +++ b/src/sparsezoo/requests/base.py @@ -31,6 +31,7 @@ "RecipeArgs", "RECIPES_API_URL", "parse_zoo_stub", + "LATEST_PACKAGE_VERSION_URL", ] @@ -43,6 +44,7 @@ ) MODELS_API_URL = f"{BASE_API_URL}/models" RECIPES_API_URL = f"{BASE_API_URL}/recipes" +LATEST_PACKAGE_VERSION_URL = f"{BASE_API_URL}/packages/check-latest" # optional prefix for stubs ZOO_STUB_PREFIX = "zoo:" diff --git a/src/sparsezoo/version.py b/src/sparsezoo/version.py index d24bd879..0d8d8c23 100644 --- a/src/sparsezoo/version.py +++ b/src/sparsezoo/version.py @@ -16,6 +16,7 @@ Functionality for storing and setting the version info for SparseZoo """ + from datetime import date