Skip to content

Commit

Permalink
some changes to support python < 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgamezh committed Jul 16, 2017
1 parent 6d4eebd commit 85bb3f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 4 additions & 0 deletions fades/__init__.py
Expand Up @@ -21,3 +21,7 @@

REPO_PYPI = 'pypi'
REPO_VCS = 'vcs'

# Not using http.server.HTTPStatus to support python < 3.5
HTTP_STATUS_NOT_FOUND = 404
HTTP_STATUS_OK = 200
7 changes: 4 additions & 3 deletions fades/helpers.py
Expand Up @@ -22,12 +22,13 @@
import logging
import subprocess

from http.server import HTTPStatus
from urllib import request
from urllib.error import HTTPError

import pkg_resources

from fades import HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK

logger = logging.getLogger(__name__)

# command to retrieve the version from an external Python
Expand Down Expand Up @@ -218,12 +219,12 @@ def _pypi_head_package(pkg_name):
try:
response = request.urlopen(req)
except HTTPError as http_error:
if http_error.code == HTTPStatus.NOT_FOUND:
if http_error.code == HTTP_STATUS_NOT_FOUND:
return False
else:
raise http_error
else:
if response.status == HTTPStatus.OK:
if response.status == HTTP_STATUS_OK:
logger.debug("%s exists in Pypi.", pkg_name)
return True

Expand Down
14 changes: 7 additions & 7 deletions tests/test_helpers.py
Expand Up @@ -22,14 +22,14 @@
import tempfile
import unittest

from http.server import HTTPStatus
from unittest.mock import patch
from urllib.error import HTTPError

import logassert

from xdg import BaseDirectory

from fades import HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK
from fades import helpers
from fades import parsing

Expand Down Expand Up @@ -295,7 +295,7 @@ def test_exists(self):

with patch('urllib.request.urlopen') as mock_urlopen:
with patch('http.client.HTTPResponse') as mock_http_response:
mock_http_response.status = HTTPStatus.OK
mock_http_response.status = HTTP_STATUS_OK
mock_urlopen.return_value = mock_http_response

exists = helpers.check_pypi_exists(deps)
Expand All @@ -307,7 +307,7 @@ def test_all_exists(self):

with patch('urllib.request.urlopen') as mock_urlopen:
with patch('http.client.HTTPResponse') as mock_http_response:
mock_http_response.status = HTTPStatus.OK
mock_http_response.status = HTTP_STATUS_OK
mock_urlopen.side_effect = [mock_http_response] * 3

exists = helpers.check_pypi_exists(dependencies)
Expand All @@ -318,7 +318,7 @@ def test_doesnt_exists(self):
dependency = parsing.parse_manual(["foo"])

with patch('urllib.request.urlopen') as mock_urlopen:
mock_http_error = HTTPError("url", HTTPStatus.NOT_FOUND, "mgs", {}, io.BytesIO())
mock_http_error = HTTPError("url", HTTP_STATUS_NOT_FOUND, "mgs", {}, io.BytesIO())
mock_urlopen.side_effect = mock_http_error

exists = helpers.check_pypi_exists(dependency)
Expand All @@ -331,8 +331,8 @@ def test_one_doesnt_exists(self):

with patch('urllib.request.urlopen') as mock_urlopen:
with patch('http.client.HTTPResponse') as mock_http_response:
mock_http_error = HTTPError("url", HTTPStatus.NOT_FOUND, "mgs", {}, io.BytesIO())
mock_http_response.status = HTTPStatus.OK
mock_http_error = HTTPError("url", HTTP_STATUS_NOT_FOUND, "mgs", {}, io.BytesIO())
mock_http_response.status = HTTP_STATUS_OK
mock_urlopen.side_effect = [mock_http_response, mock_http_error]

exists = helpers.check_pypi_exists(dependencies)
Expand All @@ -357,7 +357,7 @@ def test_status_code_error(self):

with patch('sys.exit') as mocked_exit:
with patch('urllib.request.urlopen') as mock_urlopen:
mock_http_error = HTTPError("url", HTTPStatus.BAD_REQUEST, "mgs", {}, io.BytesIO())
mock_http_error = HTTPError("url", 400, "mgs", {}, io.BytesIO())
mock_urlopen.side_effect = mock_http_error

helpers.check_pypi_exists(dependency)
Expand Down

0 comments on commit 85bb3f4

Please sign in to comment.