Use open with context manager (#83337)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/84158/head
Abhijeet Kasurde 1 year ago committed by GitHub
parent f7766cf843
commit 53ceb74e84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -38,6 +38,8 @@ import sys
import traceback import traceback
import shutil import shutil
from pathlib import Path
from ansible.release import __version__ from ansible.release import __version__
import ansible.utils.vars as utils_vars import ansible.utils.vars as utils_vars
from ansible.parsing.dataloader import DataLoader from ansible.parsing.dataloader import DataLoader
@ -89,13 +91,11 @@ def parse():
def write_argsfile(argstring, json=False): def write_argsfile(argstring, json=False):
""" Write args to a file for old-style module's use. """ """ Write args to a file for old-style module's use. """
argspath = os.path.expanduser("~/.ansible_test_module_arguments") argspath = Path("~/.ansible_test_module_arguments").expanduser()
argsfile = open(argspath, 'w')
if json: if json:
args = parse_kv(argstring) args = parse_kv(argstring)
argstring = jsonify(args) argstring = jsonify(args)
argsfile.write(argstring) argspath.write_text(argstring)
argsfile.close()
return argspath return argspath
@ -169,9 +169,8 @@ def boilerplate_module(modfile, args, interpreters, check, destfile):
print("* including generated source, if any, saving to: %s" % modfile2_path) print("* including generated source, if any, saving to: %s" % modfile2_path)
if module_style not in ('ansiballz', 'old'): if module_style not in ('ansiballz', 'old'):
print("* this may offset any line numbers in tracebacks/debuggers!") print("* this may offset any line numbers in tracebacks/debuggers!")
modfile2 = open(modfile2_path, 'wb') with open(modfile2_path, 'wb') as modfile2:
modfile2.write(module_data) modfile2.write(module_data)
modfile2.close()
modfile = modfile2_path modfile = modfile2_path
return (modfile2_path, modname, module_style) return (modfile2_path, modname, module_style)

@ -603,9 +603,8 @@ class CLI(ABC):
else: else:
try: try:
f = open(b_pwd_file, "rb") with open(b_pwd_file, "rb") as f:
secret = f.read().strip() secret = f.read().strip()
f.close()
except (OSError, IOError) as e: except (OSError, IOError) as e:
raise AnsibleError("Could not read password file %s: %s" % (pwd_file, e)) raise AnsibleError("Could not read password file %s: %s" % (pwd_file, e))

@ -254,9 +254,8 @@ def _slurp(path):
if not os.path.exists(path): if not os.path.exists(path):
raise AnsibleError("imported module support code does not exist at %s" raise AnsibleError("imported module support code does not exist at %s"
% os.path.abspath(path)) % os.path.abspath(path))
fd = open(path, 'rb') with open(path, 'rb') as fd:
data = fd.read() data = fd.read()
fd.close()
return data return data

@ -185,13 +185,11 @@ class GalaxyRole(object):
info_path = os.path.join(self.path, self.META_INSTALL) info_path = os.path.join(self.path, self.META_INSTALL)
if os.path.isfile(info_path): if os.path.isfile(info_path):
try: try:
f = open(info_path, 'r') with open(info_path, 'r') as f:
self._install_info = yaml_load(f) self._install_info = yaml_load(f)
except Exception: except Exception:
display.vvvvv("Unable to load Galaxy install info for %s" % self.name) display.vvvvv("Unable to load Galaxy install info for %s" % self.name)
return False return False
finally:
f.close()
return self._install_info return self._install_info
@property @property
@ -470,12 +468,10 @@ class GalaxyRole(object):
meta_path = os.path.join(self.path, meta_requirements) meta_path = os.path.join(self.path, meta_requirements)
if os.path.isfile(meta_path): if os.path.isfile(meta_path):
try: try:
f = open(meta_path, 'r') with open(meta_path, 'r') as f:
self._requirements = yaml_load(f) self._requirements = yaml_load(f)
except Exception: except Exception:
display.vvvvv("Unable to load requirements for %s" % self.name) display.vvvvv("Unable to load requirements for %s" % self.name)
finally:
f.close()
break break

@ -317,9 +317,8 @@ def _load_params():
# We control the args and we pass them as utf8 # We control the args and we pass them as utf8
if len(sys.argv) > 1: if len(sys.argv) > 1:
if os.path.isfile(sys.argv[1]): if os.path.isfile(sys.argv[1]):
fd = open(sys.argv[1], 'rb') with open(sys.argv[1], 'rb') as fd:
buffer = fd.read() buffer = fd.read()
fd.close()
else: else:
buffer = sys.argv[1].encode('utf-8', errors='surrogateescape') buffer = sys.argv[1].encode('utf-8', errors='surrogateescape')
# default case, read from stdin # default case, read from stdin
@ -655,9 +654,8 @@ class AnsibleModule(object):
NFS or other 'special' fs mount point, otherwise the return will be (False, None). NFS or other 'special' fs mount point, otherwise the return will be (False, None).
""" """
try: try:
f = open('/proc/mounts', 'r') with open('/proc/mounts', 'r') as f:
mount_data = f.readlines() mount_data = f.readlines()
f.close()
except Exception: except Exception:
return (False, None) return (False, None)
@ -2028,9 +2026,8 @@ class AnsibleModule(object):
def append_to_file(self, filename, str): def append_to_file(self, filename, str):
filename = os.path.expandvars(os.path.expanduser(filename)) filename = os.path.expandvars(os.path.expanduser(filename))
fh = open(filename, 'a') with open(filename, 'a') as fh:
fh.write(str) fh.write(str)
fh.close()
def bytes_to_human(self, size): def bytes_to_human(self, size):
return bytes_to_human(size) return bytes_to_human(size)

@ -200,9 +200,8 @@ from ansible.module_utils.common.text.converters import to_bytes, to_native
def write_changes(module, contents, path): def write_changes(module, contents, path):
tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir) tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
f = os.fdopen(tmpfd, 'wb') with os.fdopen(tmpfd, 'wb') as tf:
f.write(contents) tf.write(contents)
f.close()
validate = module.params.get('validate', None) validate = module.params.get('validate', None)
valid = not validate valid = not validate

@ -262,10 +262,9 @@ class CronTab(object):
if self.cron_file: if self.cron_file:
# read the cronfile # read the cronfile
try: try:
f = open(self.b_cron_file, 'rb') with open(self.b_cron_file, 'rb') as f:
self.n_existing = to_native(f.read(), errors='surrogate_or_strict') self.n_existing = to_native(f.read(), errors='surrogate_or_strict')
self.lines = self.n_existing.splitlines() self.lines = self.n_existing.splitlines()
f.close()
except IOError: except IOError:
# cron file does not exist # cron file does not exist
return return

@ -373,9 +373,8 @@ def head_splitter(headfile, remote, module=None, fail_on_error=False):
if os.path.exists(headfile): if os.path.exists(headfile):
rawdata = None rawdata = None
try: try:
f = open(headfile, 'r') with open(headfile, 'r') as f:
rawdata = f.readline() rawdata = f.readline()
f.close()
except Exception: except Exception:
if fail_on_error and module: if fail_on_error and module:
module.fail_json(msg="Unable to read %s" % headfile) module.fail_json(msg="Unable to read %s" % headfile)

@ -191,9 +191,8 @@ from ansible.module_utils.basic import AnsibleModule
def write_changes(module, contents, path): def write_changes(module, contents, path):
tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir) tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
f = os.fdopen(tmpfd, 'wb') with os.fdopen(tmpfd, 'wb') as f:
f.write(contents) f.write(contents)
f.close()
validate = module.params.get('validate', None) validate = module.params.get('validate', None)
valid = not validate valid = not validate

@ -172,9 +172,8 @@ class RpmKey(object):
self.module.fail_json(msg="Not a public key: %s" % url) self.module.fail_json(msg="Not a public key: %s" % url)
tmpfd, tmpname = tempfile.mkstemp() tmpfd, tmpname = tempfile.mkstemp()
self.module.add_cleanup_file(tmpname) self.module.add_cleanup_file(tmpname)
tmpfile = os.fdopen(tmpfd, "w+b") with os.fdopen(tmpfd, "w+b") as tmpfile:
tmpfile.write(key) tmpfile.write(key)
tmpfile.close()
return tmpname return tmpname
def normalize_keyid(self, keyid): def normalize_keyid(self, keyid):

@ -695,9 +695,8 @@ class LinuxService(Service):
# #
if self.enable_cmd.endswith("initctl"): if self.enable_cmd.endswith("initctl"):
def write_to_override_file(file_name, file_contents, ): def write_to_override_file(file_name, file_contents, ):
override_file = open(file_name, 'w') with open(file_name, 'w') as override_file:
override_file.write(file_contents) override_file.write(file_contents)
override_file.close()
initpath = '/etc/init' initpath = '/etc/init'
if self.upstart_version >= LooseVersion('0.6.7'): if self.upstart_version >= LooseVersion('0.6.7'):

@ -391,15 +391,13 @@ class ActionModule(ActionBase):
def _create_content_tempfile(self, content): def _create_content_tempfile(self, content):
""" Create a tempfile containing defined content """ """ Create a tempfile containing defined content """
fd, content_tempfile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP, prefix='.') fd, content_tempfile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP, prefix='.')
f = os.fdopen(fd, 'wb')
content = to_bytes(content) content = to_bytes(content)
try: try:
f.write(content) with os.fdopen(fd, 'wb') as f:
f.write(content)
except Exception as err: except Exception as err:
os.remove(content_tempfile) os.remove(content_tempfile)
raise Exception(err) raise Exception(err)
finally:
f.close()
return content_tempfile return content_tempfile
def _remove_tempfile_if_content_defined(self, content, content_tempfile): def _remove_tempfile_if_content_defined(self, content, content_tempfile):

@ -178,9 +178,8 @@ class ActionModule(ActionBase):
self._connection.fetch_file(source, dest) self._connection.fetch_file(source, dest)
else: else:
try: try:
f = open(to_bytes(dest, errors='surrogate_or_strict'), 'wb') with open(to_bytes(dest, errors='surrogate_or_strict'), 'wb') as f:
f.write(remote_data) f.write(remote_data)
f.close()
except (IOError, OSError) as e: except (IOError, OSError) as e:
raise AnsibleActionFail("Failed to fetch the file: %s" % e) raise AnsibleActionFail("Failed to fetch the file: %s" % e)
new_checksum = secure_hash(dest) new_checksum = secure_hash(dest)

@ -38,9 +38,8 @@ def createDaemon():
os.chdir(WORKDIR) os.chdir(WORKDIR)
os.umask(UMASK) os.umask(UMASK)
else: else:
f = open('/var/run/ansible_test_service.pid', 'w') with open('/var/run/ansible_test_service.pid', 'w') as f:
f.write("%d\n" % pid) f.write("%d\n" % pid)
f.close()
os._exit(0) os._exit(0)
else: else:
os._exit(0) os._exit(0)

@ -38,9 +38,8 @@ def createDaemon():
os.chdir(WORKDIR) os.chdir(WORKDIR)
os.umask(UMASK) os.umask(UMASK)
else: else:
f = open('/var/run/ansible_test_service.pid', 'w') with open('/var/run/ansible_test_service.pid', 'w') as f:
f.write("%d\n" % pid) f.write("%d\n" % pid)
f.close()
os._exit(0) os._exit(0)
else: else:
os._exit(0) os._exit(0)

@ -219,15 +219,13 @@ class ActionModule(ActionBase):
def _create_content_tempfile(self, content): def _create_content_tempfile(self, content):
""" Create a tempfile containing defined content """ """ Create a tempfile containing defined content """
fd, content_tempfile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP) fd, content_tempfile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP)
f = os.fdopen(fd, 'wb')
content = to_bytes(content) content = to_bytes(content)
try: try:
f.write(content) with os.fdopen(fd, 'wb') as f:
f.write(content)
except Exception as err: except Exception as err:
os.remove(content_tempfile) os.remove(content_tempfile)
raise Exception(err) raise Exception(err)
finally:
f.close()
return content_tempfile return content_tempfile
def _create_zip_tempfile(self, files, directories): def _create_zip_tempfile(self, files, directories):

Loading…
Cancel
Save