Commit 51839a7f authored by Lucas Carvalho's avatar Lucas Carvalho

Use urlretrieve instead of httplib.

httplib does not handle the download as expected.
The implementation of urlretrieve is much better and consistent, it does
the download
writing in chunks instead of loading all the file content into memory.

Also, the httplib can easily break the file content, if the client does
not
handle it. And this lib must be very simple.

The timeout is not in favor of httplib GET method.

Based in the above reasons, httplib was replaced to
urlretrieve, once there is no reason to reimplement what already exists.
parent 578fc968
...@@ -18,6 +18,7 @@ import hashlib ...@@ -18,6 +18,7 @@ import hashlib
import httplib import httplib
import json import json
import os import os
from urllib import urlretrieve
from urlparse import urlparse from urlparse import urlparse
...@@ -54,6 +55,7 @@ class NetworkcacheClient(object): ...@@ -54,6 +55,7 @@ class NetworkcacheClient(object):
self.shacache_host = shacache_urlparse.hostname self.shacache_host = shacache_urlparse.hostname
if shacache_urlparse.port is not None: if shacache_urlparse.port is not None:
self.shacache_host += ':%s' % shacache_urlparse.port self.shacache_host += ':%s' % shacache_urlparse.port
self.shacache_url = shacache
# ShaDir Properties # ShaDir Properties
if shadir is not None: if shadir is not None:
...@@ -134,23 +136,13 @@ class NetworkcacheClient(object): ...@@ -134,23 +136,13 @@ class NetworkcacheClient(object):
return json.loads(data) return json.loads(data)
def download(self, sha512sum): def download(self, path, sha512sum):
''' Download the file. ''' Download the file.
It uses http GET request method. It uses http GET request method.
''' '''
path_info = os.path.join(self.shacache_path, sha512sum) sha_cache_url = os.path.join(self.shacache_url, sha512sum)
shacache_connection = httplib.HTTPConnection(self.shacache_host) path, headers = urlretrieve(sha_cache_url, path)
try: return headers
shacache_connection.request('GET', path_info, headers=self.shacache_header_dict)
result = shacache_connection.getresponse()
file_content = result.read()
finally:
shacache_connection.close()
if result.status != 200:
raise FileNotFound(result.read())
return file_content
def select(self, directory_key=None): def select(self, directory_key=None):
'''Download a file from shacache by selecting the entry in shadir '''Download a file from shacache by selecting the entry in shadir
...@@ -174,10 +166,6 @@ class NetworkcacheClient(object): ...@@ -174,10 +166,6 @@ class NetworkcacheClient(object):
return json.loads(data) return json.loads(data)
class FileNotFound(Exception):
pass
class DirectoryNotFound(Exception): class DirectoryNotFound(Exception):
pass pass
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment