summaryrefslogtreecommitdiff
path: root/morphlib/remoteartifactcache.py
diff options
context:
space:
mode:
authorJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-04-20 11:11:46 +0100
committerJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-04-20 11:15:46 +0100
commit9ee4e4c57624cfee21d3bcc132f780bff04a59c4 (patch)
tree0d68a328d2b2e051de108008a7b552fbce790f59 /morphlib/remoteartifactcache.py
parentcc84de3d6ccd63e1d53a2f66815ff1f192a89d36 (diff)
downloadmorph-9ee4e4c57624cfee21d3bcc132f780bff04a59c4.tar.gz
Drop httplib2 in RemoteRepoCache, use custom HeadRequest and urllib2.
With a neat little trick, urllib2 can be made to only perform a HEAD request in urllib2.urlopen(). This is done by creating a dedicated HeadRequest class for HEAD requests and by passing such a HeadRequest to urllib2.urlopen(). This also means we no longer have to check the response status ourselves again as urllib2 will raise an exception on negative HTTP responses like 404 or 500.
Diffstat (limited to 'morphlib/remoteartifactcache.py')
-rw-r--r--morphlib/remoteartifactcache.py19
1 files changed, 13 insertions, 6 deletions
diff --git a/morphlib/remoteartifactcache.py b/morphlib/remoteartifactcache.py
index 6df91a4e..e2101b5d 100644
--- a/morphlib/remoteartifactcache.py
+++ b/morphlib/remoteartifactcache.py
@@ -15,11 +15,16 @@
import cliapp
-import httplib2
import urllib2
import urlparse
+class HeadRequest(urllib2.Request): # pragma: no cover
+
+ def get_method(self):
+ return 'HEAD'
+
+
class GetError(cliapp.AppException):
def __init__(self, cache, artifact):
@@ -81,10 +86,12 @@ class RemoteArtifactCache(object):
def _has_file(self, filename): # pragma: no cover
url = self._request_url(filename)
- http = httplib2.Http()
- response = http.request(url, 'HEAD')
- status = response[0]['status']
- return status >= 200 and status < 400
+ request = HeadRequest(url)
+ try:
+ urllib2.urlopen(request)
+ return True
+ except urllib2.HTTPError:
+ return False
def _get_file(self, filename): # pragma: no cover
url = self._request_url(filename)
@@ -95,4 +102,4 @@ class RemoteArtifactCache(object):
if not server_url.endswith('/'):
server_url += '/'
return urlparse.urljoin(
- server_url, '/1.0/artifacts/filename=%s' % filename)
+ server_url, '/1.0/artifacts?filename=%s' % filename)