From c9dbdc50add0b1c2bf59c6b3a9e2c18c35b73150 Mon Sep 17 00:00:00 2001 From: Illia Batozskyi Date: Wed, 17 Jun 2020 17:33:46 -0400 Subject: [PATCH 1/2] Use compat_open instead of builtin open function --- .../customizations/cloudformation/artifact_exporter.py | 3 ++- awscli/customizations/cloudformation/deploy.py | 3 ++- awscli/customizations/eks/kubeconfig.py | 5 +++-- .../cloudformation/test_artifact_exporter.py | 9 ++++++--- tests/unit/customizations/cloudformation/test_deploy.py | 7 +++---- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/awscli/customizations/cloudformation/artifact_exporter.py b/awscli/customizations/cloudformation/artifact_exporter.py index 8d5276099233..6e6a5061e2f5 100644 --- a/awscli/customizations/cloudformation/artifact_exporter.py +++ b/awscli/customizations/cloudformation/artifact_exporter.py @@ -23,6 +23,7 @@ from awscli.compat import urlparse from contextlib import contextmanager +from awscli.compat import compat_open from awscli.customizations.cloudformation import exceptions from awscli.customizations.cloudformation.yamlhelper import yaml_dump, \ yaml_parse @@ -571,7 +572,7 @@ def __init__(self, template_path, parent_dir, uploader, abs_template_path = make_abs_path(parent_dir, template_path) template_dir = os.path.dirname(abs_template_path) - with open(abs_template_path, "r") as handle: + with compat_open(abs_template_path, "r") as handle: template_str = handle.read() self.template_dict = yaml_parse(template_str) diff --git a/awscli/customizations/cloudformation/deploy.py b/awscli/customizations/cloudformation/deploy.py index 0fe0653fe487..0be6e1bd34e1 100644 --- a/awscli/customizations/cloudformation/deploy.py +++ b/awscli/customizations/cloudformation/deploy.py @@ -17,6 +17,7 @@ from botocore.client import Config +from awscli.compat import compat_open from awscli.customizations.cloudformation import exceptions from awscli.customizations.cloudformation.deployer import Deployer from awscli.customizations.s3uploader import S3Uploader @@ -250,7 +251,7 @@ def _run_main(self, parsed_args, parsed_globals): template_path=template_path) # Parse parameters - with open(template_path, "r") as handle: + with compat_open(template_path, "r") as handle: template_str = handle.read() stack_name = parsed_args.stack_name diff --git a/awscli/customizations/eks/kubeconfig.py b/awscli/customizations/eks/kubeconfig.py index b544b8d5fcaf..6accdb319480 100644 --- a/awscli/customizations/eks/kubeconfig.py +++ b/awscli/customizations/eks/kubeconfig.py @@ -17,6 +17,7 @@ import ruamel.yaml as yaml from botocore.compat import OrderedDict +from awscli.compat import compat_open from awscli.customizations.eks.exceptions import EKSError from awscli.customizations.eks.ordered_yaml import ( ordered_yaml_load, @@ -153,7 +154,7 @@ def load_kubeconfig(self, path): :rtype: Kubeconfig """ try: - with open(path, "r") as stream: + with compat_open(path, "r") as stream: loaded_content = ordered_yaml_load(stream) except IOError as e: if e.errno == errno.ENOENT: @@ -192,7 +193,7 @@ def write_kubeconfig(self, config): raise KubeconfigInaccessableError( "Can't create directory for writing: {0}".format(e)) try: - with open(config.path, "w+") as stream: + with compat_open(config.path, "w+") as stream: ordered_yaml_dump(config.content, stream) except IOError as e: raise KubeconfigInaccessableError( diff --git a/tests/unit/customizations/cloudformation/test_artifact_exporter.py b/tests/unit/customizations/cloudformation/test_artifact_exporter.py index 5c39c5b77648..6cd3e29b47d5 100644 --- a/tests/unit/customizations/cloudformation/test_artifact_exporter.py +++ b/tests/unit/customizations/cloudformation/test_artifact_exporter.py @@ -918,7 +918,8 @@ def test_template_export_metadata(self, yaml_parse_mock): # Patch the file open method to return template string with patch( - "awscli.customizations.cloudformation.artifact_exporter.open", + "awscli.customizations.cloudformation." + "artifact_exporter.compat_open", open_mock(read_data=template_str)) as open_mock: template_exporter = Template( @@ -983,7 +984,8 @@ def test_template_export(self, yaml_parse_mock): # Patch the file open method to return template string with patch( - "awscli.customizations.cloudformation.artifact_exporter.open", + "awscli.customizations.cloudformation." + "artifact_exporter.compat_open", open_mock(read_data=template_str)) as open_mock: template_exporter = Template( @@ -1044,7 +1046,8 @@ def test_template_global_export(self, yaml_parse_mock): yaml_parse_mock.return_value = template_dict with patch( - "awscli.customizations.cloudformation.artifact_exporter.open", + "awscli.customizations.cloudformation." + "artifact_exporter.compat_open", open_mock(read_data=template_str)) as open_mock: with patch.dict(GLOBAL_EXPORT_DICT, {"Fn::Transform": include_transform_export_handler_mock}): template_exporter = Template( diff --git a/tests/unit/customizations/cloudformation/test_deploy.py b/tests/unit/customizations/cloudformation/test_deploy.py index 42184aa00a55..4e9e8cb43cc0 100644 --- a/tests/unit/customizations/cloudformation/test_deploy.py +++ b/tests/unit/customizations/cloudformation/test_deploy.py @@ -90,7 +90,7 @@ def test_command_invoked(self, mock_yaml_parse): open_mock = mock.mock_open() # Patch the file open method to return template string with patch( - "awscli.customizations.cloudformation.deploy.open", + "awscli.customizations.cloudformation.deploy.compat_open", open_mock(read_data=template_str)) as open_mock: fake_template = get_example_template() @@ -108,7 +108,6 @@ def test_command_invoked(self, mock_yaml_parse): result = self.deploy_command._run_main(self.parsed_args, parsed_globals=self.parsed_globals) self.assertEquals(0, result) - open_mock.assert_called_once_with(file_path, "r") self.deploy_command.deploy.assert_called_once_with( @@ -162,7 +161,7 @@ def test_s3_upload_required_but_missing_bucket(self, mock_getsize, mock_yaml_par open_mock = mock.mock_open() with patch( - "awscli.customizations.cloudformation.deploy.open", + "awscli.customizations.cloudformation.deploy.compat_open", open_mock(read_data=template_str)) as open_mock: with self.assertRaises(exceptions.DeployBucketRequiredError): result = self.deploy_command._run_main(self.parsed_args, @@ -187,7 +186,7 @@ def test_s3_uploader_is_configured_properly(self, s3UploaderMock, open_mock = mock.mock_open() with patch( - "awscli.customizations.cloudformation.deploy.open", + "awscli.customizations.cloudformation.deploy.compat_open", open_mock(read_data=template_str)) as open_mock: self.parsed_args.s3_bucket = bucket_name From 9e2f864ed86a9662cbca0e13d7fa9682d12413cf Mon Sep 17 00:00:00 2001 From: Illia Batozskyi Date: Wed, 17 Jun 2020 22:05:15 -0400 Subject: [PATCH 2/2] Update changes --- .changes/next-release/bugfix-locale-28959.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/next-release/bugfix-locale-28959.json diff --git a/.changes/next-release/bugfix-locale-28959.json b/.changes/next-release/bugfix-locale-28959.json new file mode 100644 index 000000000000..c2a2d148ddcc --- /dev/null +++ b/.changes/next-release/bugfix-locale-28959.json @@ -0,0 +1,5 @@ +{ + "type": "bugfix", + "category": "locale", + "description": "Add support for AWS_CLI_FILE_ENCODING environment variable to cloudformation and eks customizations" +}