Skip to content

Commit 371e631

Browse files
authored
Merge pull request #271 from superannotateai/friday_427
Added logs
2 parents 38af56b + d5d8e66 commit 371e631

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/superannotate/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import os
33
import sys
44

5+
import requests
6+
import superannotate.lib.core as constances
7+
from packaging.version import parse
58
from superannotate.lib.app.analytics.class_analytics import attribute_distribution
69
from superannotate.lib.app.analytics.class_analytics import class_distribution
710
from superannotate.lib.app.annotation_helpers import add_annotation_bbox_to_json
@@ -298,9 +301,27 @@
298301

299302
__author__ = "Superannotate"
300303

304+
301305
WORKING_DIR = os.path.split(os.path.realpath(__file__))[0]
302306
sys.path.append(WORKING_DIR)
303307
logging.getLogger("botocore").setLevel(logging.CRITICAL)
304308
logging.config.fileConfig(
305309
os.path.join(WORKING_DIR, "logging.conf"), disable_existing_loggers=False
306310
)
311+
312+
local_version = parse(__version__)
313+
if local_version.is_prerelease:
314+
logging.info(constances.PACKAGE_VERSION_INFO_MESSAGE.format(__version__))
315+
req = requests.get('https://pypi.python.org/pypi/superannotate/json')
316+
if req.ok:
317+
releases = req.json().get('releases', [])
318+
pip_version = parse('0')
319+
for release in releases:
320+
ver = parse(release)
321+
if not ver.is_prerelease or local_version.is_prerelease:
322+
pip_version = max(pip_version, ver)
323+
if pip_version.major > local_version.major:
324+
logging.warning(constances.PACKAGE_VERSION_MAJOR_UPGRADE.format(local_version, pip_version))
325+
elif pip_version > local_version:
326+
logging.warning(constances.PACKAGE_VERSION_UPGRADE.format(local_version, pip_version)
327+
)

src/superannotate/lib/core/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@
102102
)
103103
MOVE_PROJECT_LIMIT_ERROR_MESSAGE = "The number of items you want to move exceeds the limit of 500 000 items per project."
104104

105+
PACKAGE_VERSION_INFO_MESSAGE = "Development version {} of SuperAnnotate SDK is being used."
106+
107+
PACKAGE_VERSION_MAJOR_UPGRADE = "There is a major upgrade of SuperAnnotate Python SDK available on PyPI. " \
108+
"We recommend upgrading. Run 'pip install --upgrade superannotate' to " \
109+
"upgrade from your version {} to {}."
110+
111+
PACKAGE_VERSION_UPGRADE = "There is a newer version of SuperAnnotate Python SDK available on PyPI." \
112+
" Run 'pip install --upgrade superannotate' to" \
113+
" upgrade from your version {} to {}"
105114
__alL__ = (
106115
ProjectType,
107116
UserRole,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from unittest import TestCase
2+
from unittest.mock import patch
3+
from unittest.mock import Mock
4+
from tests.utils.helpers import catch_prints
5+
6+
7+
TEST_VERSION = "1.0.0b1"
8+
TEST_NEW_MAJOR_VERSION = "22.2.2"
9+
TEST_NEW_VERSION = "1.2.2"
10+
11+
12+
mock_response_1 = Mock(status_code=200)
13+
mock_response_1.json.return_value = {"releases": [TEST_NEW_MAJOR_VERSION]}
14+
mock_get_1 = Mock(return_value=mock_response_1)
15+
16+
mock_response_2 = Mock(status_code=200)
17+
mock_response_2.json.return_value = {"releases": [TEST_NEW_VERSION]}
18+
mock_get_2 = Mock(return_value=mock_response_2)
19+
20+
21+
class TestVersionCheck(TestCase):
22+
23+
@patch('superannotate.version.__version__', TEST_VERSION)
24+
@patch("requests.get", mock_get_1)
25+
def test_dev_version_first_info_log(self):
26+
with catch_prints() as out:
27+
import src.superannotate
28+
import src.superannotate.lib.core as constances
29+
logs = out.getvalue().split("\n")
30+
self.assertEqual(
31+
logs[0],
32+
"SA-PYTHON-SDK - INFO - " + constances.PACKAGE_VERSION_INFO_MESSAGE.format(TEST_VERSION)
33+
)
34+
35+
@patch('superannotate.version.__version__', TEST_VERSION)
36+
@patch("requests.get", mock_get_1)
37+
def test_dev_version_major_update(self):
38+
with catch_prints() as out:
39+
import src.superannotate
40+
import src.superannotate.lib.core as constances
41+
logs = out.getvalue().split("\n")
42+
self.assertEqual(
43+
logs[1],
44+
"SA-PYTHON-SDK - WARNING - " + constances.PACKAGE_VERSION_MAJOR_UPGRADE.format(
45+
TEST_VERSION, TEST_NEW_MAJOR_VERSION
46+
)
47+
)
48+
49+
@patch('superannotate.version.__version__', TEST_VERSION)
50+
@patch("requests.get", mock_get_2)
51+
def test_dev_version_update(self):
52+
with catch_prints() as out:
53+
import src.superannotate
54+
import src.superannotate.lib.core as constances
55+
logs = out.getvalue().split("\n")
56+
self.assertEqual(
57+
logs[1],
58+
"SA-PYTHON-SDK - WARNING - " + constances.PACKAGE_VERSION_UPGRADE.format(TEST_VERSION,
59+
TEST_NEW_VERSION)
60+
)
61+

0 commit comments

Comments
 (0)