diff options
Diffstat (limited to 'morphlib/remoteartifactcache.py')
-rw-r--r-- | morphlib/remoteartifactcache.py | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/morphlib/remoteartifactcache.py b/morphlib/remoteartifactcache.py index 427e4cbb..16bba546 100644 --- a/morphlib/remoteartifactcache.py +++ b/morphlib/remoteartifactcache.py @@ -14,13 +14,27 @@ import cliapp -import logging -import urllib -import urllib2 -import urlparse - -class HeadRequest(urllib2.Request): # pragma: no cover +import logging +import sys + +if sys.version_info[0] == 2: # pragma: no cover + import urlparse + from urllib import quote as url_quote + from urllib2 import Request as urllib_request + from urllib2 import HTTPError as urllib_httperror + from urllib2 import URLError as urllib_urlerror + from urllib2 import urlopen as url_open +else: # pragma: no cover + import urllib.parse as urlparse + from urllib.error import HTTPError as urllib_httperror + from urllib.error import URLError as urllib_urlerror + from urllib.parse import quote as url_quote + from urllib.request import Request as urllib_request + from urllib.request import urlopen as url_open + + +class HeadRequest(urllib_request): # pragma: no cover def get_method(self): return 'HEAD' @@ -71,14 +85,14 @@ class RemoteArtifactCache(object): def get(self, artifact, log=logging.error): try: return self._get_file(artifact.basename()) - except urllib2.URLError as e: + except urllib_urlerror as e: log(str(e)) raise GetError(self, artifact) def get_artifact_metadata(self, artifact, name, log=logging.error): try: return self._get_file(artifact.metadata_basename(name)) - except urllib2.URLError as e: + except urllib_urlerror as e: log(str(e)) raise GetArtifactMetadataError(self, artifact, name) @@ -86,7 +100,7 @@ class RemoteArtifactCache(object): filename = '%s.%s' % (cachekey, name) try: return self._get_file(filename) - except urllib2.URLError: + except urllib_urlerror as e: raise GetSourceMetadataError(self, source, cachekey, name) def _has_file(self, filename): # pragma: no cover @@ -94,15 +108,15 @@ class RemoteArtifactCache(object): logging.debug('RemoteArtifactCache._has_file: url=%s' % url) request = HeadRequest(url) try: - urllib2.urlopen(request) + url_open(request) return True - except (urllib2.HTTPError, urllib2.URLError): + except (urllib_httperror, urllib_urlerror): return False def _get_file(self, filename): # pragma: no cover url = self._request_url(filename) logging.debug('RemoteArtifactCache._get_file: url=%s' % url) - return urllib2.urlopen(url) + return url_open(url) def _request_url(self, filename): # pragma: no cover server_url = self.server_url @@ -110,7 +124,7 @@ class RemoteArtifactCache(object): server_url += '/' return urlparse.urljoin( server_url, '/1.0/artifacts?filename=%s' % - urllib.quote(filename)) + url_quote(filename)) def __str__(self): # pragma: no cover return self.server_url |