diff --git a/shellfoundry/utilities/__init__.py b/shellfoundry/utilities/__init__.py index 8e8593b..1306079 100644 --- a/shellfoundry/utilities/__init__.py +++ b/shellfoundry/utilities/__init__.py @@ -2,16 +2,9 @@ # -*- coding: utf-8 -*- import json -import ssl import pkg_resources - -try: - # Python 2.x version - from xmlrpclib import ProtocolError, ServerProxy -except ImportError: - # Python 3.x version - from xmlrpc.client import ProtocolError, ServerProxy +import requests try: from urllib import urlopen @@ -43,9 +36,6 @@ def __init__(self, url): self.url = url -PyPI = Index("https://pypi.python.org/pypi/") - - def get_installed_version(package_name): return pkg_resources.get_distribution(package_name).version @@ -74,20 +64,20 @@ def is_index_version_greater_than_current(): def max_version_from_index(): try: - ctx = ssl.SSLContext(protocol=ssl.PROTOCOL_SSLv23) - proxy = ServerProxy(PyPI.url, context=ctx) - releases = proxy.package_releases(PACKAGE_NAME) - max_version = max(releases) - return max_version - except ProtocolError as err: - raise ShellFoundryVersionException( - "Cannot retrieve latest shellfoundry version, " - "are you offline? Error: {}".format(err) - ) + url = "https://pypi.org/pypi/{}/json".format(PACKAGE_NAME) + r = requests.get(url) + if r.status_code != requests.codes.ok: + raise ShellFoundryVersionException( + "Cannot retrieve latest shellfoundry version, " "are you offline?" + ) + else: + content = json.loads(r.content) + max_version = content["info"]["version"] + return max_version except Exception as err: raise ShellFoundryVersionException( - "Unexpected error during shellfoundry version check. " - "Error: {}.".format(err) + "Cannot retrieve latest shellfoundry version, " + "are you offline? Error: {}".format(err.message) ) diff --git a/shellfoundry/utilities/template_retriever.py b/shellfoundry/utilities/template_retriever.py index b99f6cd..8932e07 100644 --- a/shellfoundry/utilities/template_retriever.py +++ b/shellfoundry/utilities/template_retriever.py @@ -11,7 +11,11 @@ import requests import yaml from pkg_resources import parse_version -from pkg_resources._vendor.packaging.version import Version + +try: + from pkg_resources._vendor.packaging.version import Version +except ImportError: + from packaging.version import Version from .filters import CompositeFilter diff --git a/tests/test_utilities/test_versions.py b/tests/test_utilities/test_versions.py index c9dac8a..091cd5e 100644 --- a/tests/test_utilities/test_versions.py +++ b/tests/test_utilities/test_versions.py @@ -1,6 +1,6 @@ #!/usr/bin/python import unittest -from unittest.mock import MagicMock, patch +from unittest.mock import Mock, patch from shellfoundry.utilities import is_index_version_greater_than_current @@ -10,18 +10,18 @@ class TestVersionsHelpers(unittest.TestCase): def test_current_version_greater_than_index(self): # Arrange - releases = { - "0.2.7": "data", - "0.2.8": "some other data", - "1.0.0": "amazing data", - } - - server_proxy = MagicMock(package_releases=MagicMock(return_value=releases)) + return_json = """{ + "info":{ + "version":"1.0.0" + }}""" + get_response = Mock() + get_response.status_code = 200 + get_response.content = return_json # Act with patch( "shellfoundry.utilities.get_installed_version", return_value="1.0.0" - ), patch("shellfoundry.utilities.ServerProxy", return_value=server_proxy): + ), patch("shellfoundry.utilities.requests.get", return_value=get_response): ( is_greater_version, is_major_release, @@ -33,14 +33,18 @@ def test_current_version_greater_than_index(self): def test_current_version_lower_than_index_by_a_patch(self): # Arrange - releases = {"0.2.7": "data", "0.2.8": "some other data"} - - server_proxy = MagicMock(package_releases=MagicMock(return_value=releases)) + return_json = """{ + "info":{ + "version":"0.2.8" + }}""" + get_response = Mock() + get_response.status_code = 200 + get_response.content = return_json # Act with patch( "shellfoundry.utilities.get_installed_version", return_value="0.2.7" - ), patch("shellfoundry.utilities.ServerProxy", return_value=server_proxy): + ), patch("shellfoundry.utilities.requests.get", return_value=get_response): ( is_greater_version, is_major_release, @@ -52,18 +56,18 @@ def test_current_version_lower_than_index_by_a_patch(self): def test_current_version_lower_than_index_by_a_major(self): # Arrange - releases = { - "0.2.7": "data", - "0.2.8": "some other data", - "1.0.0": "amazing data", - } - - server_proxy = MagicMock(package_releases=MagicMock(return_value=releases)) + return_json = """{ + "info":{ + "version":"1.0.0" + }}""" + get_response = Mock() + get_response.status_code = 200 + get_response.content = return_json # Act with patch( "shellfoundry.utilities.get_installed_version", return_value="0.2.7" - ), patch("shellfoundry.utilities.ServerProxy", return_value=server_proxy): + ), patch("shellfoundry.utilities.requests.get", return_value=get_response): ( is_greater_version, is_major_release, @@ -75,14 +79,18 @@ def test_current_version_lower_than_index_by_a_major(self): def test_current_version_is_equal_to_index_version(self): # Arrange - releases = {"0.2.7": "data"} - - server_proxy = MagicMock(package_releases=MagicMock(return_value=releases)) + return_json = """{ + "info":{ + "version":"0.2.7" + }}""" + get_response = Mock() + get_response.status_code = 200 + get_response.content = return_json # Act with patch( "shellfoundry.utilities.get_installed_version", return_value="0.2.7" - ), patch("shellfoundry.utilities.ServerProxy", return_value=server_proxy): + ), patch("shellfoundry.utilities.requests.get", return_value=get_response): ( is_greater_version, is_major_release, diff --git a/version.txt b/version.txt index 8060c02..cba64f4 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.2.25 +1.2.26