From 0b0397067eca9f74991ba171efbefd02020ae026 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Thu, 21 Aug 2014 05:39:35 +0000 Subject: [PATCH] #814 - open compressed files with binary mode --- awscli/paramfile.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/awscli/paramfile.py b/awscli/paramfile.py index bda5e72dae99..718568ea3f57 100644 --- a/awscli/paramfile.py +++ b/awscli/paramfile.py @@ -13,6 +13,7 @@ # language governing permissions and limitations under the License. import logging +import mimetypes import os from botocore.vendored import requests @@ -45,14 +46,34 @@ def get_paramfile(path): return data +def guess_mode_from_file(file_path, mode='r'): + """ + Guess file mode by mime type of the filepath. + + returns mode + 'b' for encoded files and non-text files, + returns mode for text and unknown. + """ + + ctype, encoding = mimetypes.guess_type(file_path) + if encoding is not None: + return mode + 'b' + if ctype is not None: + if ctype.starts_with('text'): + return mode + else: + return mode + 'b' + return mode + + def get_file(prefix, path): file_path = path[len(prefix):] file_path = os.path.expanduser(file_path) file_path = os.path.expandvars(file_path) if not os.path.isfile(file_path): raise ResourceLoadingError("file does not exist: %s" % file_path) + mode = guess_mode_from_file(file_path, mode='r') try: - with compat_open(file_path, 'r') as f: + with compat_open(file_path, mode) as f: return f.read() except (OSError, IOError) as e: raise ResourceLoadingError('Unable to load paramfile %s: %s' % (