summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xhacking/test-module.py13
-rw-r--r--lib/ansible/cli/__init__.py5
-rw-r--r--lib/ansible/executor/powershell/module_manifest.py5
-rw-r--r--lib/ansible/galaxy/role.py12
-rw-r--r--lib/ansible/module_utils/basic.py15
-rw-r--r--lib/ansible/modules/blockinfile.py5
-rw-r--r--lib/ansible/modules/cron.py7
-rw-r--r--lib/ansible/modules/git.py5
-rw-r--r--lib/ansible/modules/replace.py5
-rw-r--r--lib/ansible/modules/rpm_key.py5
-rw-r--r--lib/ansible/modules/service.py5
-rw-r--r--lib/ansible/plugins/action/copy.py6
-rw-r--r--lib/ansible/plugins/action/fetch.py5
-rw-r--r--test/integration/targets/service/files/ansible_test_service.py5
-rw-r--r--test/integration/targets/service_facts/files/ansible_test_service.py5
-rw-r--r--test/support/windows-integration/plugins/action/win_copy.py6
16 files changed, 43 insertions, 66 deletions
diff --git a/hacking/test-module.py b/hacking/test-module.py
index 7b39798de5..a9df1a79b8 100755
--- a/hacking/test-module.py
+++ b/hacking/test-module.py
@@ -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')
- modfile2.write(module_data)
- modfile2.close()
+ with open(modfile2_path, 'wb') as modfile2:
+ modfile2.write(module_data)
modfile = modfile2_path
return (modfile2_path, modname, module_style)
diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py
index 81323c451d..3e66b88f0d 100644
--- a/lib/ansible/cli/__init__.py
+++ b/lib/ansible/cli/__init__.py
@@ -603,9 +603,8 @@ class CLI(ABC):
else:
try:
- f = open(b_pwd_file, "rb")
- secret = f.read().strip()
- f.close()
+ with open(b_pwd_file, "rb") as f:
+ secret = f.read().strip()
except (OSError, IOError) as e:
raise AnsibleError("Could not read password file %s: %s" % (pwd_file, e))
diff --git a/lib/ansible/executor/powershell/module_manifest.py b/lib/ansible/executor/powershell/module_manifest.py
index 93c5c8c643..da69c9dacb 100644
--- a/lib/ansible/executor/powershell/module_manifest.py
+++ b/lib/ansible/executor/powershell/module_manifest.py
@@ -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')
- data = fd.read()
- fd.close()
+ with open(path, 'rb') as fd:
+ data = fd.read()
return data
diff --git a/lib/ansible/galaxy/role.py b/lib/ansible/galaxy/role.py
index 806a9996ad..9ee7f3b905 100644
--- a/lib/ansible/galaxy/role.py
+++ b/lib/ansible/galaxy/role.py
@@ -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')
- self._install_info = yaml_load(f)
+ 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')
- self._requirements = yaml_load(f)
+ 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
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
index 6fad0d928e..d3420c0980 100644
--- a/lib/ansible/module_utils/basic.py
+++ b/lib/ansible/module_utils/basic.py
@@ -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')
- buffer = fd.read()
- fd.close()
+ with open(sys.argv[1], 'rb') as fd:
+ buffer = fd.read()
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')
- mount_data = f.readlines()
- f.close()
+ with open('/proc/mounts', 'r') as f:
+ mount_data = f.readlines()
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')
- fh.write(str)
- fh.close()
+ with open(filename, 'a') as fh:
+ fh.write(str)
def bytes_to_human(self, size):
return bytes_to_human(size)
diff --git a/lib/ansible/modules/blockinfile.py b/lib/ansible/modules/blockinfile.py
index eec996796f..e5240a0cc4 100644
--- a/lib/ansible/modules/blockinfile.py
+++ b/lib/ansible/modules/blockinfile.py
@@ -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
diff --git a/lib/ansible/modules/cron.py b/lib/ansible/modules/cron.py
index b9966a0933..0382aa6b26 100644
--- a/lib/ansible/modules/cron.py
+++ b/lib/ansible/modules/cron.py
@@ -262,10 +262,9 @@ class CronTab(object):
if self.cron_file:
# read the cronfile
try:
- f = open(self.b_cron_file, 'rb')
- self.n_existing = to_native(f.read(), errors='surrogate_or_strict')
- self.lines = self.n_existing.splitlines()
- f.close()
+ 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()
except IOError:
# cron file does not exist
return
diff --git a/lib/ansible/modules/git.py b/lib/ansible/modules/git.py
index 288d18d820..14d2619546 100644
--- a/lib/ansible/modules/git.py
+++ b/lib/ansible/modules/git.py
@@ -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')
- rawdata = f.readline()
- f.close()
+ with open(headfile, 'r') as f:
+ rawdata = f.readline()
except Exception:
if fail_on_error and module:
module.fail_json(msg="Unable to read %s" % headfile)
diff --git a/lib/ansible/modules/replace.py b/lib/ansible/modules/replace.py
index f67037af15..61e629b26a 100644
--- a/lib/ansible/modules/replace.py
+++ b/lib/ansible/modules/replace.py
@@ -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')
- f.write(contents)
- f.close()
+ with os.fdopen(tmpfd, 'wb') as f:
+ f.write(contents)
validate = module.params.get('validate', None)
valid = not validate
diff --git a/lib/ansible/modules/rpm_key.py b/lib/ansible/modules/rpm_key.py
index 1cdc8a3a0c..ced7968812 100644
--- a/lib/ansible/modules/rpm_key.py
+++ b/lib/ansible/modules/rpm_key.py
@@ -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")
- tmpfile.write(key)
- tmpfile.close()
+ with os.fdopen(tmpfd, "w+b") as tmpfile:
+ tmpfile.write(key)
return tmpname
def normalize_keyid(self, keyid):
diff --git a/lib/ansible/modules/service.py b/lib/ansible/modules/service.py
index 7db499cd99..a94b8445bd 100644
--- a/lib/ansible/modules/service.py
+++ b/lib/ansible/modules/service.py
@@ -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')
- override_file.write(file_contents)
- override_file.close()
+ with open(file_name, 'w') as override_file:
+ override_file.write(file_contents)
initpath = '/etc/init'
if self.upstart_version >= LooseVersion('0.6.7'):
diff --git a/lib/ansible/plugins/action/copy.py b/lib/ansible/plugins/action/copy.py
index 348def6033..2047671b47 100644
--- a/lib/ansible/plugins/action/copy.py
+++ b/lib/ansible/plugins/action/copy.py
@@ -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:
- f.write(content)
+ 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):
diff --git a/lib/ansible/plugins/action/fetch.py b/lib/ansible/plugins/action/fetch.py
index d099fd357c..533cab93ec 100644
--- a/lib/ansible/plugins/action/fetch.py
+++ b/lib/ansible/plugins/action/fetch.py
@@ -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')
- f.write(remote_data)
- f.close()
+ with open(to_bytes(dest, errors='surrogate_or_strict'), 'wb') as f:
+ f.write(remote_data)
except (IOError, OSError) as e:
raise AnsibleActionFail("Failed to fetch the file: %s" % e)
new_checksum = secure_hash(dest)
diff --git a/test/integration/targets/service/files/ansible_test_service.py b/test/integration/targets/service/files/ansible_test_service.py
index 6bf404cad4..9104572cb7 100644
--- a/test/integration/targets/service/files/ansible_test_service.py
+++ b/test/integration/targets/service/files/ansible_test_service.py
@@ -38,9 +38,8 @@ def createDaemon():
os.chdir(WORKDIR)
os.umask(UMASK)
else:
- f = open('/var/run/ansible_test_service.pid', 'w')
- f.write("%d\n" % pid)
- f.close()
+ with open('/var/run/ansible_test_service.pid', 'w') as f:
+ f.write("%d\n" % pid)
os._exit(0)
else:
os._exit(0)
diff --git a/test/integration/targets/service_facts/files/ansible_test_service.py b/test/integration/targets/service_facts/files/ansible_test_service.py
index 6bf404cad4..9104572cb7 100644
--- a/test/integration/targets/service_facts/files/ansible_test_service.py
+++ b/test/integration/targets/service_facts/files/ansible_test_service.py
@@ -38,9 +38,8 @@ def createDaemon():
os.chdir(WORKDIR)
os.umask(UMASK)
else:
- f = open('/var/run/ansible_test_service.pid', 'w')
- f.write("%d\n" % pid)
- f.close()
+ with open('/var/run/ansible_test_service.pid', 'w') as f:
+ f.write("%d\n" % pid)
os._exit(0)
else:
os._exit(0)
diff --git a/test/support/windows-integration/plugins/action/win_copy.py b/test/support/windows-integration/plugins/action/win_copy.py
index 02702c51fe..8554d7a94d 100644
--- a/test/support/windows-integration/plugins/action/win_copy.py
+++ b/test/support/windows-integration/plugins/action/win_copy.py
@@ -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:
- f.write(content)
+ 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):