Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#814 - open compressed files with binary mode #890

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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