Skip to content

Commit 35d67a7

Browse files
rzatsdshemetovmelange396
authored
One-time version check (#1456)
* One-time version check from package registry (once per module load) --------- Co-authored-by: Dmitry Shemetov <dshemetov@ucdavis.edu> Co-authored-by: george <george.haff@gmail.com>
1 parent 46a46a2 commit 35d67a7

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

integrations/client/test_delphi_epidata.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# standard library
44
import time
5+
import json
56
from json import JSONDecodeError
6-
from requests.models import Response
77
from unittest.mock import MagicMock, patch
88

99
# first party
@@ -306,6 +306,30 @@ def test_sandbox(self, get, post):
306306
Epidata.debug = False
307307
Epidata.sandbox = False
308308

309+
@patch('requests.get')
310+
def test_version_check(self, get):
311+
"""Test that the _version_check() function correctly logs a version discrepancy."""
312+
class MockJson:
313+
def __init__(self, content, status_code):
314+
self.content = content
315+
self.status_code = status_code
316+
def raise_for_status(self): pass
317+
def json(self): return json.loads(self.content)
318+
get.reset_mock()
319+
get.return_value = MockJson(b'{"info": {"version": "0.0.1"}}', 200)
320+
Epidata._version_check()
321+
captured = self.capsys.readouterr()
322+
output = captured.err.splitlines()
323+
self.assertEqual(len(output), 1)
324+
self.assertIn("Client version not up to date", output[0])
325+
self.assertIn("\'latest_version\': \'0.0.1\'", output[0])
326+
327+
@patch('delphi.epidata.client.delphi_epidata.Epidata._version_check')
328+
def test_version_check_once(self, version_check):
329+
"""Test that the _version_check() function is only called once on initial module import."""
330+
from delphi.epidata.client.delphi_epidata import Epidata
331+
version_check.assert_not_called()
332+
309333
def test_geo_value(self):
310334
"""test different variants of geo types: single, *, multi."""
311335

src/client/delphi_epidata.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class Epidata:
4343
BASE_URL = "https://api.delphi.cmu.edu/epidata"
4444
auth = None
4545

46-
client_version = __version__
47-
4846
debug = False # if True, prints extra logging statements
4947
sandbox = False # if True, will not execute any queries
5048

@@ -54,6 +52,24 @@ def log(evt, **kwargs):
5452
kwargs['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S %z")
5553
return sys.stderr.write(str(kwargs) + "\n")
5654

55+
# Check that this client's version matches the most recent available, runs just once per program execution (on initial module load).
56+
@staticmethod
57+
def _version_check():
58+
try:
59+
latest_version = requests.get('https://pypi.org/pypi/delphi-epidata/json').json()['info']['version']
60+
if latest_version != __version__:
61+
Epidata.log(
62+
"Client version not up to date",
63+
client_version=__version__,
64+
latest_version=latest_version
65+
)
66+
except Exception as e:
67+
Epidata.log("Error getting latest client version", exception=str(e))
68+
69+
# Run this once on module load. Use dunder method for Python <= 3.9 compatibility
70+
# https://stackoverflow.com/a/12718272
71+
_version_check.__func__()
72+
5773
# Helper function to cast values and/or ranges to strings
5874
@staticmethod
5975
def _listitem(value):

src/client/packaging/pypi/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable future changes to the `delphi_epidata` python client will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/).
55

6+
## [4.1.25] - 2024-07-18
7+
8+
### Includes
9+
- https://github.com/cmu-delphi/delphi-epidata/pull/1456
10+
11+
### Changed
12+
- Added a one-time check which logs a warning when the newest client version does not match the client version in use.
13+
614
## [4.1.24] - 2024-07-09
715

816
### Includes

0 commit comments

Comments
 (0)