Fix Requests breaking download progress bar

The move to the requests library (dbb242b) broke the progress bar during
downloading an image with --progress enabled, due to requests returning
a generator, which has no __len__ function. This patch adds an iterable
wrapper which provides the length, sourced from Content-Length headers.

Closes-Bug: #1384664

Change-Id: I48598a824396bc6e7cba69eb3ce32e7c8f30a18a
This commit is contained in:
Louis Taylor
2014-11-01 20:49:03 +00:00
parent 7ce1ff9325
commit df02ee8e2a
4 changed files with 36 additions and 5 deletions
+5 -3
View File
@@ -115,10 +115,12 @@ class Controller(object):
url = '/v2/images/%s/file' % image_id
resp, body = self.http_client.get(url)
checksum = resp.headers.get('content-md5', None)
content_length = int(resp.headers.get('content-length', 0))
if do_checksum and checksum is not None:
return utils.integrity_iter(body, checksum)
else:
return body
body = utils.integrity_iter(body, checksum)
return utils.IterableWithLength(body, content_length)
def upload(self, image_id, image_data, image_size=None):
"""