|
| 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() |
0 commit comments