From ab212650aed15df922c876a682f85752b725c1f3 Mon Sep 17 00:00:00 2001 From: Louis Taylor Date: Sat, 1 Nov 2014 20:49:03 +0000 Subject: [PATCH 1/2] 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 Signed-off-by: blkart --- glanceclient/common/utils.py | 15 +++++++++++++++ glanceclient/v1/images.py | 5 +++-- glanceclient/v2/images.py | 8 +++++--- tests/v2/test_shell_v2.py | 13 +++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index fe7a51a50..61c526ee1 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -421,3 +421,18 @@ def safe_header(name, value): return name, "{SHA1}%s" % d else: return name, value + + +class IterableWithLength(object): + def __init__(self, iterable, length): + self.iterable = iterable + self.length = length + + def __iter__(self): + return self.iterable + + def next(self): + return next(self.iterable) + + def __len__(self): + return self.length diff --git a/glanceclient/v1/images.py b/glanceclient/v1/images.py index 87060c239..e3a6e6952 100644 --- a/glanceclient/v1/images.py +++ b/glanceclient/v1/images.py @@ -140,14 +140,15 @@ def data(self, image, do_checksum=True, **kwargs): image_id = base.getid(image) resp, body = self.client.get('/v1/images/%s' % urlparse.quote(str(image_id))) + content_length = int(resp.headers.get('content-length', 0)) checksum = resp.headers.get('x-image-meta-checksum', None) if do_checksum and checksum is not None: - return utils.integrity_iter(body, checksum) + body = utils.integrity_iter(body, checksum) return_request_id = kwargs.get('return_req_id', None) if return_request_id is not None: return_request_id.append(resp.headers.get(OS_REQ_ID_HDR, None)) - return body + return utils.IterableWithLength(body, content_length) def list(self, **kwargs): """Get a list of images. diff --git a/glanceclient/v2/images.py b/glanceclient/v2/images.py index 6ec92507d..b49c5ee0d 100644 --- a/glanceclient/v2/images.py +++ b/glanceclient/v2/images.py @@ -115,10 +115,12 @@ def data(self, image_id, do_checksum=True): 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): """ diff --git a/tests/v2/test_shell_v2.py b/tests/v2/test_shell_v2.py index 23894eabd..3480e5c96 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -341,6 +341,19 @@ def test_image_upload(self): test_shell.do_image_upload(self.gc, args) mocked_upload.assert_called_once_with('IMG-01', 'testfile', 1024) + def test_image_download(self): + args = self._make_args( + {'id': 'IMG-01', 'file': 'test', 'progress': True}) + + with mock.patch.object(self.gc.images, 'data') as mocked_data: + def _data(): + for c in 'abcedf': + yield c + mocked_data.return_value = utils.IterableWithLength(_data(), 5) + + test_shell.do_image_download(self.gc, args) + mocked_data.assert_called_once_with('IMG-01') + def test_do_image_delete(self): args = self._make_args({'id': 'pass', 'file': 'test'}) with mock.patch.object(self.gc.images, 'delete') as mocked_delete: From 49dd24a4a8d64588bd1cc697d45517c38160346e Mon Sep 17 00:00:00 2001 From: Jakub Ruzicka Date: Wed, 14 Aug 2013 16:07:03 +0200 Subject: [PATCH 2/2] Remove runtime dependency on python-pbr. Signed-off-by: blkart --- glanceclient/__init__.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/glanceclient/__init__.py b/glanceclient/__init__.py index f96703129..ca5ac5f69 100644 --- a/glanceclient/__init__.py +++ b/glanceclient/__init__.py @@ -21,11 +21,4 @@ import warnings warnings.warn("Could not import glanceclient.client", ImportWarning) -import pbr.version - -version_info = pbr.version.VersionInfo('python-glanceclient') - -try: - __version__ = version_info.version_string() -except AttributeError: - __version__ = None +__version__ = "REDHATGLANCECLIENTVERSION"