Merge "Use safe way through "with" statement to work with files"

This commit is contained in:
Jenkins 2014-05-20 18:22:41 +00:00 committed by Gerrit Code Review
commit f7effe4a4f
4 changed files with 14 additions and 12 deletions

View File

@ -797,7 +797,7 @@ def load_custom_properties():
filename = 'schema-image.json'
match = CONF.find_file(filename)
if match:
schema_file = open(match)
with open(match, 'r') as schema_file:
schema_data = schema_file.read()
return json.loads(schema_data)
else:

View File

@ -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:

View File

@ -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 = "certificate"
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 "

View File

@ -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')
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))
f.close()
def config(self, **kw):
"""