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

The file handles are left open in those files.
This commit is contained in:
Mads Jensen 2019-12-21 13:01:38 +01:00 committed by Felix Fontein
parent b580f2929d
commit a0e6bf366e
6 changed files with 15 additions and 5 deletions

View file

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

View file

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

View file

@ -111,7 +111,8 @@ class IntersightModule():
self.module.fail_json(msg='cryptography is required for this module')
self.host = self.module.params['api_uri']
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.response_list = []

View file

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

View file

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

View file

@ -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
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:
print('%s not found.' % opts.eip_log)
return