From ebaf337321a847e71f673febb79a5c72bda85602 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Wed, 6 Mar 2019 08:50:46 -0500 Subject: [PATCH] utils/swift/download_container: python3 adjustment Plan content from swift may need to be encoded if Python 3. Change-Id: Ia2108f3f64e3ae043cd050b5c3748682d0a816de Co-Authored-By: Michele Baldessari Co-Authored-By: Alex Schultz --- tripleo_common/utils/swift.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tripleo_common/utils/swift.py b/tripleo_common/utils/swift.py index cfb15ed30..603d4f8f5 100644 --- a/tripleo_common/utils/swift.py +++ b/tripleo_common/utils/swift.py @@ -64,6 +64,10 @@ def download_container(swiftclient, container, dest, is_newer = False filename = obj['name'] contents = swiftclient.get_object(container, filename)[1] + try: + contents = contents.encode('utf-8') + except (UnicodeDecodeError, AttributeError): + pass path = os.path.join(dest, filename) dirname = os.path.dirname(path) already_exists = os.path.exists(path) @@ -89,7 +93,9 @@ def download_container(swiftclient, container, dest, if not os.path.exists(dirname): os.makedirs(dirname) - with open(path, 'w') as f: + # open in binary as the swift client returns error + # under python3 if opened as text + with open(path, 'wb') as f: f.write(contents)