summaryrefslogtreecommitdiff
path: root/glanceclient/common/utils.py
diff options
context:
space:
mode:
authorLars Gellrich <lars.gellrich@hp.com>2012-08-01 16:04:37 +0000
committerLars Gellrich <lars.gellrich@hp.com>2012-08-09 17:14:22 +0000
commit137b3cf975d73437943e100065c76b83acfa7dd3 (patch)
tree3727ddc75139847b175143c9d69e6b095c82c513 /glanceclient/common/utils.py
parent354c98b087515dc4303a07d1ff0d9a9d7b4dd48b (diff)
downloadpython-glanceclient-137b3cf975d73437943e100065c76b83acfa7dd3.tar.gz
Enable client V2 to download images
Added the CLI option image-download to download an image via API V2. Added utility function to save an image. Added common iterator to validate the checksum. Related to bp glance-client-v2 Change-Id: I0247f5a3462142dc5e9f3dc16cbe00c8e3d42f42
Diffstat (limited to 'glanceclient/common/utils.py')
-rw-r--r--glanceclient/common/utils.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py
index 957f713..31b8530 100644
--- a/glanceclient/common/utils.py
+++ b/glanceclient/common/utils.py
@@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+import errno
+import hashlib
import os
import sys
import uuid
@@ -130,3 +132,39 @@ def exit(msg=''):
if msg:
print >> sys.stderr, msg
sys.exit(1)
+
+
+def save_image(data, path):
+ """
+ Save an image to the specified path.
+
+ :param data: binary data of the image
+ :param path: path to save the image to
+ """
+ if path is None:
+ image = sys.stdout
+ else:
+ image = open(path, 'wb')
+ try:
+ for chunk in data:
+ image.write(chunk)
+ finally:
+ if path is not None:
+ 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))