|
41 |
import hashlib |
41 |
import hashlib |
42 |
import lzma |
42 |
import lzma |
43 |
import httplib |
43 |
import httplib |
|
|
44 |
import urllib2 |
44 |
import urlparse |
45 |
import urlparse |
45 |
import contextlib |
46 |
import contextlib |
46 |
import re |
47 |
import re |
|
191 |
ucc/image/path via a HTTP HEAD request.''' |
192 |
ucc/image/path via a HTTP HEAD request.''' |
192 |
|
193 |
|
193 |
url = '%s/%s' % (UCC_BASE_URL, filename) |
194 |
url = '%s/%s' % (UCC_BASE_URL, filename) |
194 |
parts = urlparse.urlparse(url) |
195 |
request = urllib2.Request(url) |
195 |
if parts.scheme == 'http': |
196 |
request.get_method = lambda : 'HEAD' |
196 |
connection = httplib.HTTPConnection(parts.hostname) |
197 |
try: |
197 |
elif parts.scheme == 'https': |
198 |
response = urllib2.urlopen(request) |
198 |
connection = httplib.HTTPSConnection(parts.hostname) |
199 |
except urllib2.HTTPError as exc: |
199 |
else: |
200 |
# raise an httplib error since this exception type is handled |
200 |
raise httplib.error(_('Uknown protocol %s.') % parts.scheme) |
201 |
raise httplib.error(_('Could not download file %s: %s') % (url, exc)) |
201 |
|
202 |
|
202 |
connection.request('HEAD', parts.path) |
203 |
info = response.info() |
203 |
response = connection.getresponse() |
204 |
size = int(info.getheader('content-length', '0')) |
204 |
if response.status >= 400: |
|
|
205 |
raise httplib.error(_('Could not download file (%s - %s): %s') % (response.status, httplib.responses.get(response.status, _('Unknown error')), url)) |
206 |
headers = dict(response.getheaders()) |
207 |
size = int(headers.get('content-length', '0')) |
208 |
return size |
205 |
return size |
209 |
|
206 |
|
210 |
|
207 |
|
|
216 |
return proxies |
213 |
return proxies |
217 |
|
214 |
|
218 |
|
215 |
|
|
|
216 |
# inspired from from u.m.c.modules.appcenter.util |
217 |
def _install_opener(): |
218 |
proxies = _get_proxies() |
219 |
if proxies: |
220 |
proxy = urllib2.ProxyHandler(proxies) |
221 |
opener = urllib2.build_opener(proxy) |
222 |
urllib2.install_opener(opener) |
223 |
_install_opener() |
224 |
|
225 |
|
219 |
def _download_url(url, filepath, progress=_dummy_progress): |
226 |
def _download_url(url, filepath, progress=_dummy_progress): |
220 |
'''Download specified URL to the given location on the hard disk. |
227 |
'''Download specified URL to the given location on the hard disk. |
221 |
Optionally with a callback function that receives the parameters (bytes_downloaded, bytes_total).''' |
228 |
Optionally with a callback function that receives the parameters (bytes_downloaded, bytes_total).''' |