Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.
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
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/sparsezoo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
112 changes: 112 additions & 0 deletions src/sparsezoo/package.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions src/sparsezoo/requests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"RecipeArgs",
"RECIPES_API_URL",
"parse_zoo_stub",
"LATEST_PACKAGE_VERSION_URL",
]


Expand All @@ -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:"
Expand Down
1 change: 1 addition & 0 deletions src/sparsezoo/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Functionality for storing and setting the version info for SparseZoo
"""


from datetime import date


Expand Down