From 8774c4a2a104ca4a89472b44d63edfa439a82d8f Mon Sep 17 00:00:00 2001 From: pran1990 Date: Sun, 18 May 2014 00:45:17 -0700 Subject: [PATCH] Use safe way through "with" statement to work with files It's good practice to use with statement to work with files. This way files get closed automatically at the end of the block. Fixed some cases, including some where file wasn't closed after usage Change-Id: I9399f2a2b7edb1cd5aa64782cd9f980f9233b827 --- glance/api/v2/images.py | 4 ++-- glance/cmd/control.py | 3 ++- glance/common/utils.py | 6 ++++-- glance/tests/utils.py | 13 ++++++------- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/glance/api/v2/images.py b/glance/api/v2/images.py index 2fb4eb98a8..13059a088e 100644 --- a/glance/api/v2/images.py +++ b/glance/api/v2/images.py @@ -797,8 +797,8 @@ def load_custom_properties(): filename = 'schema-image.json' match = CONF.find_file(filename) if match: - schema_file = open(match) - schema_data = schema_file.read() + with open(match, 'r') as schema_file: + schema_data = schema_file.read() return json.loads(schema_data) else: msg = _('Could not find schema properties file %s. Continuing ' diff --git a/glance/cmd/control.py b/glance/cmd/control.py index a7174ee442..d4bb60fe8b 100644 --- a/glance/cmd/control.py +++ b/glance/cmd/control.py @@ -201,7 +201,8 @@ def do_start(verb, pid_file, server, args): def do_check_status(pid_file, server): if os.path.exists(pid_file): - pid = open(pid_file).read().strip() + with open(pid_file, 'r') as pidfile: + pid = pidfile.read().strip() print(_("%(serv)s (pid %(pid)s) is running...") % {'serv': server, 'pid': pid}) else: diff --git a/glance/common/utils.py b/glance/common/utils.py index 287d4c65dc..305bfbbb54 100644 --- a/glance/common/utils.py +++ b/glance/common/utils.py @@ -505,12 +505,14 @@ def validate_key_cert(key_file, cert_file): try: error_key_name = "private key" error_filename = key_file - key_str = open(key_file, "r").read() + with open(key_file, 'r') as keyfile: + key_str = keyfile.read() key = crypto.load_privatekey(crypto.FILETYPE_PEM, key_str) error_key_name = "certficate" error_filename = cert_file - cert_str = open(cert_file, "r").read() + with open(cert_file, 'r') as certfile: + cert_str = certfile.read() cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_str) except IOError as ioe: raise RuntimeError(_("There is a problem with your %(error_key_name)s " diff --git a/glance/tests/utils.py b/glance/tests/utils.py index 9758c04923..ad517d8240 100644 --- a/glance/tests/utils.py +++ b/glance/tests/utils.py @@ -82,13 +82,12 @@ class BaseTestCase(testtools.TestCase): return dst_file_name def set_property_protection_rules(self, rules): - f = open(self.property_file, 'w') - for rule_key in rules.keys(): - f.write('[%s]\n' % rule_key) - for operation in rules[rule_key].keys(): - roles_str = ','.join(rules[rule_key][operation]) - f.write('%s = %s\n' % (operation, roles_str)) - f.close() + with open(self.property_file, 'w') as f: + for rule_key in rules.keys(): + f.write('[%s]\n' % rule_key) + for operation in rules[rule_key].keys(): + roles_str = ','.join(rules[rule_key][operation]) + f.write('%s = %s\n' % (operation, roles_str)) def config(self, **kw): """