* Used a more pythonic approach to buffered file reading

* Alphabetised standard library imports
* Made some minor PEP8 compliance changes
* Fixed issue #273
This commit is contained in:
Bradley Ayers 2011-04-29 09:04:03 +10:00
parent 07fd30bbef
commit 439e1ed6f8
1 changed files with 16 additions and 13 deletions

View File

@ -1,9 +1,10 @@
import re
import cgi
import getpass
import sys
import os
import mimetypes
import os
import re
import shutil
import sys
import tempfile
from pip.backwardcompat import (md5, copytree, xmlrpclib, urllib, urllib2,
urlparse, string_types, HTTPError)
@ -330,10 +331,7 @@ def _check_md5(download_hash, link):
def _get_md5_from_file(target_file, link):
download_hash = md5()
fp = open(target_file, 'rb')
while 1:
chunk = fp.read(4096)
if not chunk:
break
for chunk in iter(lambda: fp.read(4096), ''):
download_hash.update(chunk)
fp.close()
return download_hash
@ -362,10 +360,7 @@ def _download_url(resp, link, temp_location):
logger.notify('Downloading %s' % show_url)
logger.debug('Downloading from URL %s' % link)
while 1:
chunk = resp.read(4096)
if not chunk:
break
for chunk in iter(lambda: resp.read(4096), ''):
downloaded += len(chunk)
if show_progress:
if not total_length:
@ -416,7 +411,7 @@ def unpack_http_url(link, location, download_cache, only_download):
create_download_cache_folder(download_cache)
if (target_file
and os.path.exists(target_file)
and os.path.exists(target_file+'.content-type')):
and os.path.exists(target_file + '.content-type')):
fp = open(target_file+'.content-type')
content_type = fp.read().strip()
fp.close()
@ -427,7 +422,14 @@ def unpack_http_url(link, location, download_cache, only_download):
else:
resp = _get_response_from_url(target_url, link)
content_type = resp.info()['content-type']
filename = link.filename
filename = link.filename # fallback
# Have a look at the Content-Disposition header for a better guess
content_disposition = resp.info().get('content-disposition')
if content_disposition:
type, params = cgi.parse_header(content_disposition)
# We use ``or`` here because we don't want to use an "empty" value
# from the filename param.
filename = params.get('filename') or filename
ext = splitext(filename)[1]
if not ext:
ext = mimetypes.guess_extension(content_type)
@ -466,6 +468,7 @@ def _get_response_from_url(target_url, link):
raise
return resp
class Urllib2HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"