Show a pretty progressbar when uploading and downloading an image.

Add a new module that contain generic wrapper for file and iterator, which
are used to wrap image to upload and the request body iterator in upload and
download cases repectively, to show and advance a pretty progress bar when this
laters are consumed, The progress bar is triggered by adding a --progress command
line argument to commands: image-create, image-download or image-update.

Change-Id: I2ba42fd0c58f4fa087adb568ec3f08246cae3759
bug fix: LP#1112309
blueprint: progressbar-when-uploading
This commit is contained in:
mouad benchchaoui
2013-07-08 21:18:16 +02:00
parent 43e71e3993
commit 1d7da740b2
13 changed files with 354 additions and 94 deletions
+44 -4
View File
@@ -14,6 +14,8 @@
# under the License.
import copy
import errno
import hashlib
import httplib
import logging
import posixpath
@@ -435,17 +437,55 @@ class VerifiedHTTPSConnection(HTTPSConnection):
class ResponseBodyIterator(object):
"""A class that acts as an iterator over an HTTP response."""
"""
A class that acts as an iterator over an HTTP response.
This class will also check response body integrity when iterating over
the instance and if a checksum was supplied using `set_checksum` method,
else by default the class will not do any integrity check.
"""
def __init__(self, resp):
self.resp = resp
self._resp = resp
self._checksum = None
self._size = int(resp.getheader('content-length', 0))
self._end_reached = False
def set_checksum(self, checksum):
"""
Set checksum to check against when iterating over this instance.
:raise: AttributeError if iterator is already consumed.
"""
if self._end_reached:
raise AttributeError("Can't set checksum for an already consumed"
" iterator")
self._checksum = checksum
def __len__(self):
return int(self._size)
def __iter__(self):
md5sum = hashlib.md5()
while True:
yield self.next()
try:
chunk = self.next()
except StopIteration:
self._end_reached = True
# NOTE(mouad): Check image integrity when the end of response
# body is reached.
md5sum = md5sum.hexdigest()
if self._checksum is not None and md5sum != self._checksum:
raise IOError(errno.EPIPE,
'Corrupted image. Checksum was %s '
'expected %s' % (md5sum, self._checksum))
raise
else:
yield chunk
md5sum.update(chunk)
def next(self):
chunk = self.resp.read(CHUNKSIZE)
chunk = self._resp.read(CHUNKSIZE)
if chunk:
return chunk
else: