Use context manager for file handling in misc files. (#65372)

The file handles are left open in those files.
pull/66018/head
Mads Jensen 5 years ago committed by Felix Fontein
parent b580f2929d
commit a0e6bf366e

@ -0,0 +1,5 @@
bugfixes:
- spacewalk inventory - improve file handling by using a context manager.
- intersight_rest_api, intersight_info - improve file handling by using a context manager.
- one_vm - improve file handling by using a context manager.
- postgresql_query - improve file handling by using a context manager.

@ -89,7 +89,8 @@ def spacewalk_report(name):
p.wait() p.wait()
fh.close() fh.close()
lines = open(cache_filename, 'r').readlines() with open(cache_filename, 'r') as f:
lines = f.readlines()
keys = lines[0].strip().split(',') keys = lines[0].strip().split(',')
# add 'spacewalk_' prefix to the keys # add 'spacewalk_' prefix to the keys
keys = ['spacewalk_' + key for key in keys] keys = ['spacewalk_' + key for key in keys]

@ -111,7 +111,8 @@ class IntersightModule():
self.module.fail_json(msg='cryptography is required for this module') self.module.fail_json(msg='cryptography is required for this module')
self.host = self.module.params['api_uri'] self.host = self.module.params['api_uri']
self.public_key = self.module.params['api_key_id'] self.public_key = self.module.params['api_key_id']
self.private_key = open(self.module.params['api_private_key'], 'r').read() with open(self.module.params['api_private_key'], 'r') as f:
self.private_key = f.read()
self.digest_algorithm = 'rsa-sha256' self.digest_algorithm = 'rsa-sha256'
self.response_list = [] self.response_list = []

@ -1303,7 +1303,8 @@ def get_connection_info(module):
if authfile is None: if authfile is None:
authfile = os.path.join(os.environ.get("HOME"), ".one", "one_auth") authfile = os.path.join(os.environ.get("HOME"), ".one", "one_auth")
try: try:
authstring = open(authfile, "r").read().rstrip() with open(authfile, "r") as fp:
authstring = fp.read().rstrip()
username = authstring.split(":")[0] username = authstring.split(":")[0]
password = authstring.split(":")[1] password = authstring.split(":")[1]
except (OSError, IOError): except (OSError, IOError):

@ -270,7 +270,8 @@ def main():
if path_to_script: if path_to_script:
try: try:
query = open(path_to_script, 'r').read() with open(path_to_script, 'r') as f:
query = f.read()
except Exception as e: except Exception as e:
module.fail_json(msg="Cannot read file '%s' : %s" % (path_to_script, to_native(e))) module.fail_json(msg="Cannot read file '%s' : %s" % (path_to_script, to_native(e)))

@ -63,7 +63,8 @@ def delete_aws_eips(get_func, attr, opts):
# the file might not be there if the integration test wasn't run # the file might not be there if the integration test wasn't run
try: try:
eip_log = open(opts.eip_log, 'r').read().splitlines() with open(opts.eip_log, 'r') as f:
eip_log = f.read().splitlines()
except IOError: except IOError:
print('%s not found.' % opts.eip_log) print('%s not found.' % opts.eip_log)
return return

Loading…
Cancel
Save