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 committed Aug 21, 2014
1 parent 9d311fa commit 0b03970
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 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,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' % (
Expand Down

0 comments on commit 0b03970

Please sign in to comment.