-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(update): Adding caching to checking for latest version
- Loading branch information
1 parent
0f26f0b
commit 3749bdb
Showing
7 changed files
with
286 additions
and
17 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,75 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
import os | ||
from datetime import datetime | ||
from urllib.request import urlopen | ||
|
||
from devservices.constants import DEVSERVICES_CACHE_DIR | ||
from devservices.constants import DEVSERVICES_LATEST_VERSION_CACHE_FILE | ||
from devservices.constants import DEVSERVICES_LATEST_VERSION_CACHE_TTL | ||
from devservices.constants import DEVSERVICES_RELEASES_URL | ||
|
||
|
||
def _delete_cached_version() -> None: | ||
os.remove(DEVSERVICES_LATEST_VERSION_CACHE_FILE) | ||
|
||
|
||
def _get_cached_version() -> str | None: | ||
if not os.path.exists(DEVSERVICES_LATEST_VERSION_CACHE_FILE): | ||
return None | ||
try: | ||
with open(DEVSERVICES_LATEST_VERSION_CACHE_FILE, "r", encoding="utf-8") as f: | ||
cached_data = json.load(f) | ||
except (OSError, json.JSONDecodeError): | ||
_delete_cached_version() | ||
return None | ||
|
||
timestamp = cached_data["timestamp"] | ||
cached_latest_version = cached_data["latest_version"] | ||
|
||
try: | ||
cached_time = datetime.fromisoformat(timestamp) | ||
except ValueError: | ||
_delete_cached_version() | ||
return None | ||
|
||
if ( | ||
isinstance(cached_latest_version, str) | ||
and datetime.now() - cached_time < DEVSERVICES_LATEST_VERSION_CACHE_TTL | ||
): | ||
return cached_latest_version | ||
|
||
# If the cache file exists but is stale or has an invalid version, | ||
# remove it. | ||
_delete_cached_version() | ||
return None | ||
|
||
|
||
def _set_cached_version(latest_version: str) -> None: | ||
with open(DEVSERVICES_LATEST_VERSION_CACHE_FILE, "w", encoding="utf-8") as f: | ||
json.dump( | ||
{ | ||
"latest_version": latest_version, | ||
"timestamp": datetime.now().isoformat(), | ||
}, | ||
f, | ||
) | ||
|
||
|
||
def check_for_update() -> str | None: | ||
os.makedirs(DEVSERVICES_CACHE_DIR, exist_ok=True) | ||
|
||
cached_version = _get_cached_version() | ||
if cached_version is not None: | ||
return cached_version | ||
|
||
with urlopen(DEVSERVICES_RELEASES_URL) as response: | ||
if response.status == 200: | ||
data = json.loads(response.read()) | ||
latest_version = str(data["tag_name"]) | ||
|
||
_set_cached_version(latest_version) | ||
|
||
return latest_version | ||
return None | ||
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 |
---|---|---|
|
@@ -8,3 +8,4 @@ types-PyYAML==6.0.11 | |
setuptools==70.0.0 | ||
build==0.8.0 | ||
wheel==0.42.0 | ||
freezegun==1.2.2 |
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,197 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from datetime import datetime | ||
from datetime import timedelta | ||
from pathlib import Path | ||
from unittest import mock | ||
|
||
from freezegun import freeze_time | ||
|
||
from devservices.constants import DEVSERVICES_RELEASES_URL | ||
from devservices.utils.check_for_update import check_for_update | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_not_cached(mock_urlopen: mock.Mock, tmp_path: Path) -> None: | ||
mock_response = mock.mock_open(read_data=b'{"tag_name": "1.0.0"}').return_value | ||
mock_response.status = 200 | ||
mock_urlopen.side_effect = [mock_response] | ||
with ( | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_CACHE_DIR", | ||
str(tmp_path / "cache"), | ||
), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_LATEST_VERSION_CACHE_FILE", | ||
str(tmp_path / "cache" / "latest_version.json"), | ||
), | ||
): | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_cached_fresh(mock_urlopen: mock.Mock, tmp_path: Path) -> None: | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.json" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_CACHE_DIR", | ||
str(cache_dir), | ||
), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_LATEST_VERSION_CACHE_FILE", | ||
str(cached_file), | ||
), | ||
): | ||
fourteen_minutes_ago = datetime.now() - timedelta(minutes=14) | ||
cache_dir.mkdir() | ||
cached_file.write_text( | ||
f"""{{ | ||
"latest_version": "1.0.0", | ||
"timestamp": "{fourteen_minutes_ago.isoformat()}" | ||
}}""" | ||
) | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_not_called() | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_cached_stale_without_update( | ||
mock_urlopen: mock.Mock, tmp_path: Path | ||
) -> None: | ||
mock_response = mock.mock_open(read_data=b'{"tag_name": "1.0.0"}').return_value | ||
mock_response.status = 200 | ||
mock_urlopen.side_effect = [mock_response] | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.json" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_CACHE_DIR", | ||
str(cache_dir), | ||
), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_LATEST_VERSION_CACHE_FILE", | ||
str(cached_file), | ||
), | ||
): | ||
sixteen_minutes_ago = datetime.now() - timedelta(minutes=16) | ||
cache_dir.mkdir() | ||
cached_file.write_text( | ||
f"""{{ | ||
"latest_version": "1.0.0", | ||
"timestamp": "{sixteen_minutes_ago.isoformat()}" | ||
}}""" | ||
) | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
with cached_file.open("r") as f: | ||
cached_data = json.load(f) | ||
cached_data["latest_version"] = "1.0.0" | ||
cached_data["timestamp"] = datetime.now().isoformat() | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_cached_stale_with_update( | ||
mock_urlopen: mock.Mock, tmp_path: Path | ||
) -> None: | ||
mock_response = mock.mock_open(read_data=b'{"tag_name": "1.0.1"}').return_value | ||
mock_response.status = 200 | ||
mock_urlopen.side_effect = [mock_response] | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.json" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_CACHE_DIR", | ||
str(cache_dir), | ||
), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_LATEST_VERSION_CACHE_FILE", | ||
str(cached_file), | ||
), | ||
): | ||
sixteen_minutes_ago = datetime.now() - timedelta(minutes=16) | ||
cache_dir.mkdir() | ||
cached_file.write_text( | ||
f"""{{ | ||
"latest_version": "1.0.0", | ||
"timestamp": "{sixteen_minutes_ago.isoformat()}" | ||
}}""" | ||
) | ||
assert check_for_update() == "1.0.1" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
with cached_file.open("r") as f: | ||
cached_data = json.load(f) | ||
cached_data["latest_version"] = "1.0.1" | ||
cached_data["timestamp"] = datetime.now().isoformat() | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_invalid_cached_value( | ||
mock_urlopen: mock.Mock, tmp_path: Path | ||
) -> None: | ||
mock_response = mock.mock_open(read_data=b'{"tag_name": "1.0.0"}').return_value | ||
mock_response.status = 200 | ||
mock_urlopen.side_effect = [mock_response] | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.json" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_CACHE_DIR", | ||
str(cache_dir), | ||
), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_LATEST_VERSION_CACHE_FILE", | ||
str(cached_file), | ||
), | ||
): | ||
cache_dir.mkdir() | ||
cached_file.write_text("invalid json") | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
with cached_file.open("r") as f: | ||
cached_data = json.load(f) | ||
cached_data["latest_version"] = "1.0.0" | ||
cached_data["timestamp"] = datetime.now().isoformat() | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_invalid_date(mock_urlopen: mock.Mock, tmp_path: Path) -> None: | ||
mock_response = mock.mock_open(read_data=b'{"tag_name": "1.0.0"}').return_value | ||
mock_response.status = 200 | ||
mock_urlopen.side_effect = [mock_response] | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.json" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_CACHE_DIR", | ||
str(cache_dir), | ||
), | ||
mock.patch( | ||
"devservices.utils.check_for_update.DEVSERVICES_LATEST_VERSION_CACHE_FILE", | ||
str(cached_file), | ||
), | ||
): | ||
cache_dir.mkdir() | ||
cached_file.write_text( | ||
"""{{ | ||
"latest_version": "1.0.0", | ||
"timestamp": "invalid" | ||
}}""" | ||
) | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
with cached_file.open("r") as f: | ||
cached_data = json.load(f) | ||
cached_data["latest_version"] = "1.0.0" | ||
cached_data["timestamp"] = datetime.now().isoformat() |