Skip to content

Commit a07f694

Browse files
horheynmmarkurtz
andauthored
version-api-pypi integration (#114)
* [WIP] version-api-pypi integration * added thread to run version check in the background * successful api call and added env var * code run in background thread, imported as a private function * changed pip command in logger * fix issues with nightly not being pushed to the package version api * change base_version to version Co-authored-by: Mark Kurtz <mark.kurtz@neuralmagic.com>
1 parent a55fa9f commit a07f694

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"""
2525

2626
import os
27-
from typing import Tuple, List, Dict
27+
from typing import Dict, List, Tuple
28+
2829
from setuptools import find_packages, setup
2930

3031
# default variables to be overwritten by the version.py file

src/sparsezoo/__init__.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# limitations under the License.
1616

1717
"""
18-
Functionality for accessing models, recipes, and supporting files in the SparseZoo
18+
- Functionality for accessing models, recipes, and supporting files in the SparseZoo
19+
- Notify the user the last pypi package version
1920
"""
2021

2122
# flake8: noqa
@@ -25,3 +26,12 @@
2526
from .main import *
2627
from .models.zoo import *
2728
from .objects import *
29+
from .package import *
30+
31+
32+
from sparsezoo.package import check_package_version as _check_package_version
33+
34+
_check_package_version(
35+
package_name=__name__ if is_release else f"{__name__}-nightly",
36+
package_version=version,
37+
)

src/sparsezoo/package.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing,
10+
# software distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
import os
17+
import threading
18+
from typing import Optional
19+
20+
import requests
21+
22+
from sparsezoo.requests import LATEST_PACKAGE_VERSION_URL
23+
24+
25+
LOGGER = logging.getLogger(__name__)
26+
27+
28+
def package_version_check_request(
29+
package_name: str, package_version: str, package_integration: Optional[str]
30+
):
31+
"""
32+
Make an api call to api-neuralmagic.com, retrieve payload and check if the
33+
user is on the latest package version. Lambda: nm-get-latest-version
34+
35+
:param package_name: package name of the client
36+
:param package_version: package version of the client
37+
:param package_integration: package integration of the client
38+
"""
39+
url = (
40+
f"{LATEST_PACKAGE_VERSION_URL}?"
41+
f"packages={package_name}"
42+
f"&integrations={package_integration}"
43+
f"&versions={package_version}"
44+
)
45+
try:
46+
response = requests.get(url) # no token-headers required
47+
response.raise_for_status()
48+
response_json = response.json()
49+
50+
for checked_package in response_json["checked_packages"]:
51+
if not checked_package["is_latest"]:
52+
LOGGER.warning(
53+
"WARNING: "
54+
f"You are using {checked_package['package_name']} "
55+
f"version {checked_package['user_package_version']} "
56+
f"however version {checked_package['latest_package_version']} "
57+
"is available.\n"
58+
"Consider upgrading via executing the "
59+
f"'pip install --upgrade' command.\n"
60+
"To turn off set an environmental variable "
61+
"NM_VERSION_CHECK=false"
62+
)
63+
except Exception as err:
64+
raise RuntimeError(
65+
f"Execption occured in the Neural Magic's internal version-api check\n{err}"
66+
)
67+
68+
69+
def version_check_execution_condition(
70+
package_name: str, package_version: str, package_integration: Optional[str]
71+
):
72+
"""
73+
Check if conditions are met to run the version-check api
74+
75+
:param package_name: package name of the client
76+
:param package_version: package version of the client
77+
:param package_integration: package integration of the client
78+
"""
79+
if (
80+
os.getenv("NM_VERSION_CHECK") is None
81+
or os.getenv("NM_VERSION_CHECK").lower().strip() != "false"
82+
):
83+
package_version_check_request(
84+
package_name=package_name,
85+
package_integration=package_integration,
86+
package_version=package_version,
87+
)
88+
else:
89+
LOGGER.info(
90+
"Skipping Neural Magic's internal code exection: "
91+
"latest package version check"
92+
)
93+
94+
95+
def check_package_version(
96+
package_name: str, package_version: str, package_integration: Optional[str] = None
97+
):
98+
"""
99+
Run a background thread to run version-check api
100+
101+
:param package_name: package name of the client
102+
:param package_version: package version of the client
103+
:param package_integration: package integration of the client
104+
"""
105+
threading.Thread(
106+
target=version_check_execution_condition,
107+
kwargs={
108+
"package_name": package_name,
109+
"package_version": package_version,
110+
"package_integration": package_integration,
111+
},
112+
).start()

src/sparsezoo/requests/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"RecipeArgs",
3232
"RECIPES_API_URL",
3333
"parse_zoo_stub",
34+
"LATEST_PACKAGE_VERSION_URL",
3435
]
3536

3637

@@ -43,6 +44,7 @@
4344
)
4445
MODELS_API_URL = f"{BASE_API_URL}/models"
4546
RECIPES_API_URL = f"{BASE_API_URL}/recipes"
47+
LATEST_PACKAGE_VERSION_URL = f"{BASE_API_URL}/packages/check-latest"
4648

4749
# optional prefix for stubs
4850
ZOO_STUB_PREFIX = "zoo:"

src/sparsezoo/version.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Functionality for storing and setting the version info for SparseZoo
1717
"""
1818

19+
1920
from datetime import date
2021

2122

0 commit comments

Comments
 (0)