-
-
Notifications
You must be signed in to change notification settings - Fork 1
ref(update): Adding caching to checking for latest version #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
import os | ||
from datetime import datetime | ||
from datetime import timedelta | ||
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: | ||
if os.path.exists(DEVSERVICES_LATEST_VERSION_CACHE_FILE): | ||
os.remove(DEVSERVICES_LATEST_VERSION_CACHE_FILE) | ||
|
||
|
||
def _get_cache_age() -> timedelta: | ||
if os.path.exists(DEVSERVICES_LATEST_VERSION_CACHE_FILE): | ||
file_modification_time = datetime.fromtimestamp( | ||
os.path.getmtime(DEVSERVICES_LATEST_VERSION_CACHE_FILE) | ||
) | ||
return datetime.now() - file_modification_time | ||
return timedelta.max | ||
|
||
|
||
def _get_cached_version() -> str | None: | ||
cache_age = _get_cache_age() | ||
if cache_age < DEVSERVICES_LATEST_VERSION_CACHE_TTL: | ||
with open(DEVSERVICES_LATEST_VERSION_CACHE_FILE, "r", encoding="utf-8") as f: | ||
return f.read() | ||
else: | ||
_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: | ||
f.write(latest_version) | ||
|
||
|
||
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 | ||
IanWoodard marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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,170 @@ | ||
from __future__ import annotations | ||
|
||
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_LATEST_VERSION_CACHE_TTL | ||
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] | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.txt" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.os.path.getmtime", | ||
return_value=datetime.now().timestamp(), | ||
), | ||
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), | ||
), | ||
): | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
with cached_file.open("r") as f: | ||
cached_version = f.read() | ||
assert cached_version == "1.0.0" | ||
|
||
|
||
@mock.patch("devservices.utils.check_for_update.urlopen") | ||
def test_check_for_update_no_cache_not_ok( | ||
mock_urlopen: mock.Mock, tmp_path: Path | ||
) -> None: | ||
mock_response = mock.mock_open().return_value | ||
mock_response.status = 500 | ||
mock_urlopen.side_effect = [mock_response] | ||
cache_dir = tmp_path / "cache" | ||
cached_file = cache_dir / "latest_version.txt" | ||
with ( | ||
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), | ||
), | ||
): | ||
assert check_for_update() is None | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
assert not cached_file.exists() | ||
|
||
|
||
@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.txt" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.os.path.getmtime", | ||
return_value=( | ||
datetime.now() | ||
- DEVSERVICES_LATEST_VERSION_CACHE_TTL | ||
+ timedelta(minutes=1) | ||
).timestamp(), | ||
), | ||
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("1.0.0") | ||
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.txt" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.os.path.getmtime", | ||
return_value=( | ||
datetime.now() - DEVSERVICES_LATEST_VERSION_CACHE_TTL | ||
).timestamp(), | ||
), | ||
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("1.0.0") | ||
assert check_for_update() == "1.0.0" | ||
mock_urlopen.assert_called_once_with(DEVSERVICES_RELEASES_URL) | ||
|
||
with cached_file.open("r") as f: | ||
cached_version = f.read() | ||
assert cached_version == "1.0.0" | ||
|
||
|
||
@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.txt" | ||
with ( | ||
freeze_time("2024-05-14 05:43:21"), | ||
mock.patch( | ||
"devservices.utils.check_for_update.os.path.getmtime", | ||
return_value=( | ||
datetime.now() | ||
- DEVSERVICES_LATEST_VERSION_CACHE_TTL | ||
- timedelta(minutes=1) | ||
).timestamp(), | ||
), | ||
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("1.0.0") | ||
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 = f.read() | ||
assert cached_data == "1.0.1" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.