Skip to content

Commit

Permalink
#814 - open compressed files with binary mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Grangaard authored and spazm committed Aug 21, 2014
1 parent 9d311fa commit 1d7090e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion awscli/paramfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# language governing permissions and limitations under the License.

import logging
import mimetypes
import os

from botocore.vendored import requests
Expand Down Expand Up @@ -45,14 +46,28 @@ 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, else mode
"""

ctype, encoding = mimetypes.guess_type(file_path)
if encoding is not None:
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' % (
Expand Down

0 comments on commit 1d7090e

Please sign in to comment.