-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1835 from backend-developers-ltd/feature/version-…
…check-cache Add version check caching, fix version comparison
- Loading branch information
Showing
7 changed files
with
284 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from typing import Optional | ||
from pathlib import Path | ||
import time | ||
from packaging.version import Version | ||
|
||
import bittensor | ||
import requests | ||
|
||
VERSION_CHECK_THRESHOLD = 86400 | ||
|
||
|
||
class VersionCheckError(Exception): | ||
pass | ||
|
||
|
||
def _get_version_file_path() -> Path: | ||
return Path.home() / ".bittensor" / ".last_known_version" | ||
|
||
|
||
def _get_version_from_file(version_file: Path) -> Optional[str]: | ||
try: | ||
mtime = version_file.stat().st_mtime | ||
bittensor.logging.debug(f"Found version file, last modified: {mtime}") | ||
diff = time.time() - mtime | ||
|
||
if diff >= VERSION_CHECK_THRESHOLD: | ||
bittensor.logging.debug("Version file expired") | ||
return None | ||
|
||
return version_file.read_text() | ||
except FileNotFoundError: | ||
bittensor.logging.debug("No bitensor version file found") | ||
return None | ||
except OSError: | ||
bittensor.logging.exception("Failed to read version file") | ||
return None | ||
|
||
|
||
def _get_version_from_pypi(timeout: int = 15) -> str: | ||
bittensor.logging.debug( | ||
f"Checking latest Bittensor version at: {bittensor.__pipaddress__}" | ||
) | ||
try: | ||
response = requests.get(bittensor.__pipaddress__, timeout=timeout) | ||
latest_version = response.json()["info"]["version"] | ||
return latest_version | ||
except requests.exceptions.RequestException: | ||
bittensor.logging.exception("Failed to get latest version from pypi") | ||
raise | ||
|
||
|
||
def get_and_save_latest_version(timeout: int = 15) -> str: | ||
version_file = _get_version_file_path() | ||
|
||
if last_known_version := _get_version_from_file(version_file): | ||
return last_known_version | ||
|
||
latest_version = _get_version_from_pypi(timeout) | ||
|
||
try: | ||
version_file.write_text(latest_version) | ||
except OSError: | ||
bittensor.logging.exception("Failed to save latest version to file") | ||
|
||
return latest_version | ||
|
||
|
||
def check_version(timeout: int = 15): | ||
""" | ||
Check if the current version of Bittensor is up to date with the latest version on PyPi. | ||
Raises a VersionCheckError if the version check fails. | ||
""" | ||
|
||
try: | ||
latest_version = get_and_save_latest_version(timeout) | ||
|
||
if Version(latest_version) > Version(bittensor.__version__): | ||
print( | ||
"\u001b[33mBittensor Version: Current {}/Latest {}\nPlease update to the latest version at your earliest convenience. " | ||
"Run the following command to upgrade:\n\n\u001b[0mpython -m pip install --upgrade bittensor".format( | ||
bittensor.__version__, latest_version | ||
) | ||
) | ||
except Exception as e: | ||
raise VersionCheckError("Version check failed") from e | ||
|
||
|
||
def version_checking(timeout: int = 15): | ||
""" | ||
Deprecated, kept for backwards compatibility. Use check_version() instead. | ||
""" | ||
|
||
from warnings import warn | ||
|
||
warn( | ||
"version_checking() is deprecated, please use check_version() instead", | ||
DeprecationWarning, | ||
) | ||
|
||
try: | ||
check_version(timeout) | ||
except VersionCheckError: | ||
bittensor.logging.exception("Version check failed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ hypothesis==6.81.1 | |
flake8==7.0.0 | ||
mypy==1.8.0 | ||
types-retry==0.9.9.4 | ||
freezegun==1.5.0 | ||
torch>=1.13.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.