From 29c47193df50f1cd9149d481f46b40855a5d7776 Mon Sep 17 00:00:00 2001 From: xiexs Date: Fri, 27 Nov 2015 03:27:27 -0500 Subject: [PATCH] Optimize "open" method with context manager Use opening context manager to open a file. Change-Id: I1d37e2ea93dd364de8df0a790a4bdd617c50420c --- keystone/common/openssl.py | 15 ++++++--------- keystone/token/controllers.py | 10 ++++------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/keystone/common/openssl.py b/keystone/common/openssl.py index 58a142e3b5..153964caa0 100644 --- a/keystone/common/openssl.py +++ b/keystone/common/openssl.py @@ -135,9 +135,8 @@ class BaseCertificateConfigure(object): user=self.use_keystone_user, group=self.use_keystone_group, log=LOG) if not file_exists(self.ssl_config_file_name): - ssl_config_file = open(self.ssl_config_file_name, 'w') - ssl_config_file.write(self.sslconfig % self.ssl_dictionary) - ssl_config_file.close() + with open(self.ssl_config_file_name, 'w') as ssl_config_file: + ssl_config_file.write(self.sslconfig % self.ssl_dictionary) utils.set_permissions(self.ssl_config_file_name, mode=PRIVATE_FILE_PERMS, user=self.use_keystone_user, @@ -145,9 +144,8 @@ class BaseCertificateConfigure(object): index_file_name = os.path.join(self.conf_dir, 'index.txt') if not file_exists(index_file_name): - index_file = open(index_file_name, 'w') - index_file.write('') - index_file.close() + with open(index_file_name, 'w') as index_file: + index_file.write('') utils.set_permissions(index_file_name, mode=PRIVATE_FILE_PERMS, user=self.use_keystone_user, @@ -155,9 +153,8 @@ class BaseCertificateConfigure(object): serial_file_name = os.path.join(self.conf_dir, 'serial') if not file_exists(serial_file_name): - index_file = open(serial_file_name, 'w') - index_file.write('01') - index_file.close() + with open(serial_file_name, 'w') as index_file: + index_file.write('01') utils.set_permissions(serial_file_name, mode=PRIVATE_FILE_PERMS, user=self.use_keystone_user, diff --git a/keystone/token/controllers.py b/keystone/token/controllers.py index 85f7bf2b05..59b56b129a 100644 --- a/keystone/token/controllers.py +++ b/keystone/token/controllers.py @@ -49,16 +49,14 @@ class Auth(controller.V2Controller): @controller.v2_deprecated def ca_cert(self, context, auth=None): - ca_file = open(CONF.signing.ca_certs, 'r') - data = ca_file.read() - ca_file.close() + with open(CONF.signing.ca_certs, 'r') as ca_file: + data = ca_file.read() return data @controller.v2_deprecated def signing_cert(self, context, auth=None): - cert_file = open(CONF.signing.certfile, 'r') - data = cert_file.read() - cert_file.close() + with open(CONF.signing.certfile, 'r') as cert_file: + data = cert_file.read() return data @controller.v2_deprecated