Skip to content

Commit

Permalink
Merge pull request #214 from QualiSystems/replace-xmlrpc-with-requests
Browse files Browse the repository at this point in the history
Replace xmlrpc with requests
  • Loading branch information
Costya-Y authored Oct 3, 2024
2 parents 67b6e35 + 11e8f65 commit 367f147
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
36 changes: 13 additions & 23 deletions shellfoundry/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
)


Expand Down
6 changes: 5 additions & 1 deletion shellfoundry/utilities/template_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
58 changes: 33 additions & 25 deletions tests/test_utilities/test_versions.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.25
1.2.26

0 comments on commit 367f147

Please sign in to comment.