diff --git a/sahara/swift/swift_helper.py b/sahara/swift/swift_helper.py index 89dee667..480ca8f6 100644 --- a/sahara/swift/swift_helper.py +++ b/sahara/swift/swift_helper.py @@ -94,7 +94,8 @@ def _install_ssl_certs(instance, certs): "changeit\"") with instance.remote() as r: for idx, cert in enumerate(certs): - data = open(cert).read() + with open(cert) as cert_fd: + data = cert_fd.read() r.write_file_to("/tmp/cert.pem", data) try: r.execute_command(register_cmd % idx) diff --git a/sahara/tests/scenario/base.py b/sahara/tests/scenario/base.py index 33dc934d..c84fd672 100644 --- a/sahara/tests/scenario/base.py +++ b/sahara/tests/scenario/base.py @@ -318,7 +318,8 @@ class BaseTestCase(base.BaseTestCase): path = utils.rand_name('test') data = None if source: - data = open(source).read() + with open(source) as source_fd: + data = source_fd.read() self.__upload_to_container(container, path, data) @@ -339,7 +340,8 @@ class BaseTestCase(base.BaseTestCase): "sudo su - -c \"hdfs dfs -mkdir -p %(path)s \" %(user)s" % { "path": hdfs_dir, "user": hdfs_username}) hdfs_filepath = utils.rand_name(hdfs_dir + "/file") - data = open(source).read() + with open(source) as source_fd: + data = source_fd.read() self._run_command_on_node( inst_ip, ("echo -e \"%(data)s\" | sudo su - -c \"hdfs dfs" @@ -350,7 +352,8 @@ class BaseTestCase(base.BaseTestCase): return hdfs_filepath def _create_internal_db_data(self, source): - data = open(source).read() + with open(source) as source_fd: + data = source_fd.read() id = self.__create_internal_db_data(utils.rand_name('test'), data) return 'internal-db://%s' % id diff --git a/sahara/utils/crypto.py b/sahara/utils/crypto.py index b20e0a48..f447271f 100644 --- a/sahara/utils/crypto.py +++ b/sahara/utils/crypto.py @@ -49,10 +49,12 @@ def generate_key_pair(key_length=2048): processutils.execute(*args) if not os.path.exists(keyfile): raise ex.SystemError(_("Private key file hasn't been created")) - private_key = open(keyfile).read() + with open(keyfile) as keyfile_fd: + private_key = keyfile_fd.read() public_key_path = keyfile + '.pub' if not os.path.exists(public_key_path): raise ex.SystemError(_("Public key file hasn't been created")) - public_key = open(public_key_path).read() + with open(public_key_path) as public_key_path_fd: + public_key = public_key_path_fd.read() return private_key, public_key