diff options
author | mouad benchchaoui <m.benchchaoui@cloudbau.de> | 2013-07-08 21:18:16 +0200 |
---|---|---|
committer | mouad benchchaoui <m.benchchaoui@cloudbau.de> | 2013-08-08 15:40:15 +0200 |
commit | 1d7da740b22945c43a9f36affca3c1b3fbad91fa (patch) | |
tree | 5abb69ba9d8f83e26b7fc3c106c72faf8c546aef /glanceclient/common/utils.py | |
parent | 43e71e399372102f8ef4a3b7ad836fe16ace63a3 (diff) | |
download | python-glanceclient-1d7da740b22945c43a9f36affca3c1b3fbad91fa.tar.gz |
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
Diffstat (limited to 'glanceclient/common/utils.py')
-rw-r--r-- | glanceclient/common/utils.py | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index c0e65db..82360df 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -14,7 +14,6 @@ # under the License. import errno -import hashlib import os import sys import uuid @@ -161,23 +160,6 @@ def save_image(data, path): image.close() -def integrity_iter(iter, checksum): - """ - Check image data integrity. - - :raises: IOError - """ - md5sum = hashlib.md5() - for chunk in iter: - yield chunk - md5sum.update(chunk) - md5sum = md5sum.hexdigest() - if md5sum != checksum: - raise IOError(errno.EPIPE, - 'Corrupt image download. Checksum was %s expected %s' % - (md5sum, checksum)) - - def make_size_human_readable(size): suffix = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'] base = 1024.0 @@ -214,3 +196,31 @@ def exception_to_str(exc): error = ("Caught '%(exception)s' exception." % {"exception": exc.__class__.__name__}) return strutils.safe_encode(error, errors='ignore') + + +def get_file_size(file_obj): + """ + Analyze file-like object and attempt to determine its size. + + :param file_obj: file-like object. + :retval The file's size or None if it cannot be determined. + """ + if hasattr(file_obj, 'seek') and hasattr(file_obj, 'tell'): + try: + curr = file_obj.tell() + file_obj.seek(0, os.SEEK_END) + size = file_obj.tell() + file_obj.seek(curr) + return size + except IOError, e: + if e.errno == errno.ESPIPE: + # Illegal seek. This means the file object + # is a pipe (e.g the user is trying + # to pipe image data to the client, + # echo testdata | bin/glance add blah...), or + # that file object is empty, or that a file-like + # object which doesn't support 'seek/tell' has + # been supplied. + return + else: + raise |