From a0e6bf366e107915a256e8f79c0a15ad7f2be228 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Sat, 21 Dec 2019 13:01:38 +0100 Subject: [PATCH] Use context manager for file handling in misc files. (#65372) The file handles are left open in those files. --- changelogs/fragments/65372-misc-context-manager.yml | 5 +++++ contrib/inventory/spacewalk.py | 3 ++- lib/ansible/module_utils/remote_management/intersight.py | 3 ++- lib/ansible/modules/cloud/opennebula/one_vm.py | 3 ++- lib/ansible/modules/database/postgresql/postgresql_query.py | 3 ++- test/legacy/cleanup_ec2.py | 3 ++- 6 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/65372-misc-context-manager.yml diff --git a/changelogs/fragments/65372-misc-context-manager.yml b/changelogs/fragments/65372-misc-context-manager.yml new file mode 100644 index 0000000000..94fca3baa6 --- /dev/null +++ b/changelogs/fragments/65372-misc-context-manager.yml @@ -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. diff --git a/contrib/inventory/spacewalk.py b/contrib/inventory/spacewalk.py index e94298c308..dc96b1fe3b 100755 --- a/contrib/inventory/spacewalk.py +++ b/contrib/inventory/spacewalk.py @@ -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] diff --git a/lib/ansible/module_utils/remote_management/intersight.py b/lib/ansible/module_utils/remote_management/intersight.py index fa50f815a4..714a8e70cf 100644 --- a/lib/ansible/module_utils/remote_management/intersight.py +++ b/lib/ansible/module_utils/remote_management/intersight.py @@ -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 = [] diff --git a/lib/ansible/modules/cloud/opennebula/one_vm.py b/lib/ansible/modules/cloud/opennebula/one_vm.py index 41532467df..19155f9b97 100644 --- a/lib/ansible/modules/cloud/opennebula/one_vm.py +++ b/lib/ansible/modules/cloud/opennebula/one_vm.py @@ -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): diff --git a/lib/ansible/modules/database/postgresql/postgresql_query.py b/lib/ansible/modules/database/postgresql/postgresql_query.py index d7b1e375d2..b0ac546df8 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_query.py +++ b/lib/ansible/modules/database/postgresql/postgresql_query.py @@ -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))) diff --git a/test/legacy/cleanup_ec2.py b/test/legacy/cleanup_ec2.py index 65a8cc2264..826a720b1f 100644 --- a/test/legacy/cleanup_ec2.py +++ b/test/legacy/cleanup_ec2.py @@ -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