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 shutil
from pathlib import Path
from ansible.release import __version__
import ansible.utils.vars as utils_vars
from ansible.parsing.dataloader import DataLoader
@ -89,13 +91,11 @@ def parse():
def write_argsfile(argstring, json=False):
""" Write args to a file for old-style module's use. """
argspath = os.path.expanduser("~/.ansible_test_module_arguments")
argsfile = open(argspath, 'w')
argspath = Path("~/.ansible_test_module_arguments").expanduser()
if json:
args = parse_kv(argstring)
argstring = jsonify(args)
argsfile.write(argstring)
argsfile.close()
argspath.write_text(argstring)
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)
if module_style not in ('ansiballz', 'old'):
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.close()
modfile = modfile2_path
return (modfile2_path, modname, module_style)

@ -603,9 +603,8 @@ class CLI(ABC):
else:
try:
f = open(b_pwd_file, "rb")
with open(b_pwd_file, "rb") as f:
secret = f.read().strip()
f.close()
except (OSError, IOError) as 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):
raise AnsibleError("imported module support code does not exist at %s"
% os.path.abspath(path))
fd = open(path, 'rb')
with open(path, 'rb') as fd:
data = fd.read()
fd.close()
return data

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

@ -317,9 +317,8 @@ def _load_params():
# We control the args and we pass them as utf8
if len(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()
fd.close()
else:
buffer = sys.argv[1].encode('utf-8', errors='surrogateescape')
# 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).
"""
try:
f = open('/proc/mounts', 'r')
with open('/proc/mounts', 'r') as f:
mount_data = f.readlines()
f.close()
except Exception:
return (False, None)
@ -2028,9 +2026,8 @@ class AnsibleModule(object):
def append_to_file(self, filename, str):
filename = os.path.expandvars(os.path.expanduser(filename))
fh = open(filename, 'a')
with open(filename, 'a') as fh:
fh.write(str)
fh.close()
def bytes_to_human(self, 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):
tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
f = os.fdopen(tmpfd, 'wb')
f.write(contents)
f.close()
with os.fdopen(tmpfd, 'wb') as tf:
tf.write(contents)
validate = module.params.get('validate', None)
valid = not validate

@ -262,10 +262,9 @@ class CronTab(object):
if self.cron_file:
# read the cronfile
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.lines = self.n_existing.splitlines()
f.close()
except IOError:
# cron file does not exist
return

@ -373,9 +373,8 @@ def head_splitter(headfile, remote, module=None, fail_on_error=False):
if os.path.exists(headfile):
rawdata = None
try:
f = open(headfile, 'r')
with open(headfile, 'r') as f:
rawdata = f.readline()
f.close()
except Exception:
if fail_on_error and module:
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):
tmpfd, tmpfile = tempfile.mkstemp(dir=module.tmpdir)
f = os.fdopen(tmpfd, 'wb')
with os.fdopen(tmpfd, 'wb') as f:
f.write(contents)
f.close()
validate = module.params.get('validate', None)
valid = not validate

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

@ -695,9 +695,8 @@ class LinuxService(Service):
#
if self.enable_cmd.endswith("initctl"):
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.close()
initpath = '/etc/init'
if self.upstart_version >= LooseVersion('0.6.7'):

@ -391,15 +391,13 @@ class ActionModule(ActionBase):
def _create_content_tempfile(self, content):
""" Create a tempfile containing defined content """
fd, content_tempfile = tempfile.mkstemp(dir=C.DEFAULT_LOCAL_TMP, prefix='.')
f = os.fdopen(fd, 'wb')
content = to_bytes(content)
try:
with os.fdopen(fd, 'wb') as f:
f.write(content)
except Exception as err:
os.remove(content_tempfile)
raise Exception(err)
finally:
f.close()
return 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)
else:
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.close()
except (IOError, OSError) as e:
raise AnsibleActionFail("Failed to fetch the file: %s" % e)
new_checksum = secure_hash(dest)

@ -38,9 +38,8 @@ def createDaemon():
os.chdir(WORKDIR)
os.umask(UMASK)
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.close()
os._exit(0)
else:
os._exit(0)

@ -38,9 +38,8 @@ def createDaemon():
os.chdir(WORKDIR)
os.umask(UMASK)
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.close()
os._exit(0)
else:
os._exit(0)

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

Loading…
Cancel
Save