use open() as context manager (#9579)
* use open() as context manager * add changelog fragpull/9603/head
parent
c5cc949492
commit
0de39a6f47
|
@ -0,0 +1,11 @@
|
||||||
|
minor_changes:
|
||||||
|
- known_hosts - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- cloud_init_data_facts - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- cronvar - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- crypttab - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- parted - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- pulp_repo - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- redhat_subscription - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- solaris_zone - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- sorcery - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
||||||
|
- timezone - open file using ``open()`` as a context manager (https://github.com/ansible-collections/community.general/pull/9579).
|
|
@ -103,13 +103,11 @@ def not_in_host_file(self, host):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
host_fh = open(hf)
|
with open(hf) as host_fh:
|
||||||
|
data = host_fh.read()
|
||||||
except IOError:
|
except IOError:
|
||||||
hfiles_not_found += 1
|
hfiles_not_found += 1
|
||||||
continue
|
continue
|
||||||
else:
|
|
||||||
data = host_fh.read()
|
|
||||||
host_fh.close()
|
|
||||||
|
|
||||||
for line in data.split("\n"):
|
for line in data.split("\n"):
|
||||||
if line is None or " " not in line:
|
if line is None or " " not in line:
|
||||||
|
|
|
@ -105,9 +105,8 @@ def gather_cloud_init_data_facts(module):
|
||||||
json_file = os.path.join(CLOUD_INIT_PATH, i + '.json')
|
json_file = os.path.join(CLOUD_INIT_PATH, i + '.json')
|
||||||
|
|
||||||
if os.path.exists(json_file):
|
if os.path.exists(json_file):
|
||||||
f = open(json_file, 'rb')
|
with open(json_file, 'rb') as f:
|
||||||
contents = to_text(f.read(), errors='surrogate_or_strict')
|
contents = to_text(f.read(), errors='surrogate_or_strict')
|
||||||
f.close()
|
|
||||||
|
|
||||||
if contents:
|
if contents:
|
||||||
res['cloud_init_data_facts'][i] = module.from_json(contents)
|
res['cloud_init_data_facts'][i] = module.from_json(contents)
|
||||||
|
|
|
@ -146,9 +146,8 @@ class CronVar(object):
|
||||||
if self.cron_file:
|
if self.cron_file:
|
||||||
# read the cronfile
|
# read the cronfile
|
||||||
try:
|
try:
|
||||||
f = open(self.cron_file, 'r')
|
with open(self.cron_file, 'r') as f:
|
||||||
self.lines = f.read().splitlines()
|
self.lines = f.read().splitlines()
|
||||||
f.close()
|
|
||||||
except IOError:
|
except IOError:
|
||||||
# cron file does not exist
|
# cron file does not exist
|
||||||
return
|
return
|
||||||
|
|
|
@ -154,11 +154,8 @@ def main():
|
||||||
changed, reason = existing_line.opts.remove(opts)
|
changed, reason = existing_line.opts.remove(opts)
|
||||||
|
|
||||||
if changed and not module.check_mode:
|
if changed and not module.check_mode:
|
||||||
try:
|
with open(path, 'wb') as f:
|
||||||
f = open(path, 'wb')
|
|
||||||
f.write(to_bytes(crypttab, errors='surrogate_or_strict'))
|
f.write(to_bytes(crypttab, errors='surrogate_or_strict'))
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
module.exit_json(changed=changed, msg=reason, **module.params)
|
module.exit_json(changed=changed, msg=reason, **module.params)
|
||||||
|
|
||||||
|
@ -173,12 +170,9 @@ class Crypttab(object):
|
||||||
os.makedirs(os.path.dirname(path))
|
os.makedirs(os.path.dirname(path))
|
||||||
open(path, 'a').close()
|
open(path, 'a').close()
|
||||||
|
|
||||||
try:
|
with open(path, 'r') as f:
|
||||||
f = open(path, 'r')
|
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
self._lines.append(Line(line))
|
self._lines.append(Line(line))
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
def add(self, line):
|
def add(self, line):
|
||||||
self._lines.append(line)
|
self._lines.append(line)
|
||||||
|
|
|
@ -588,11 +588,8 @@ def read_record(file_path, default=None):
|
||||||
Reads the first line of a file and returns it.
|
Reads the first line of a file and returns it.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
f = open(file_path, 'r')
|
with open(file_path, 'r') as f:
|
||||||
try:
|
|
||||||
return f.readline().strip()
|
return f.readline().strip()
|
||||||
finally:
|
|
||||||
f.close()
|
|
||||||
except IOError:
|
except IOError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
|
@ -583,29 +583,20 @@ def main():
|
||||||
if importer_ssl_ca_cert is not None:
|
if importer_ssl_ca_cert is not None:
|
||||||
importer_ssl_ca_cert_file_path = os.path.abspath(importer_ssl_ca_cert)
|
importer_ssl_ca_cert_file_path = os.path.abspath(importer_ssl_ca_cert)
|
||||||
if os.path.isfile(importer_ssl_ca_cert_file_path):
|
if os.path.isfile(importer_ssl_ca_cert_file_path):
|
||||||
importer_ssl_ca_cert_file_object = open(importer_ssl_ca_cert_file_path, 'r')
|
with open(importer_ssl_ca_cert_file_path, 'r') as importer_ssl_ca_cert_file_object:
|
||||||
try:
|
|
||||||
importer_ssl_ca_cert = importer_ssl_ca_cert_file_object.read()
|
importer_ssl_ca_cert = importer_ssl_ca_cert_file_object.read()
|
||||||
finally:
|
|
||||||
importer_ssl_ca_cert_file_object.close()
|
|
||||||
|
|
||||||
if importer_ssl_client_cert is not None:
|
if importer_ssl_client_cert is not None:
|
||||||
importer_ssl_client_cert_file_path = os.path.abspath(importer_ssl_client_cert)
|
importer_ssl_client_cert_file_path = os.path.abspath(importer_ssl_client_cert)
|
||||||
if os.path.isfile(importer_ssl_client_cert_file_path):
|
if os.path.isfile(importer_ssl_client_cert_file_path):
|
||||||
importer_ssl_client_cert_file_object = open(importer_ssl_client_cert_file_path, 'r')
|
with open(importer_ssl_client_cert_file_path, 'r') as importer_ssl_client_cert_file_object:
|
||||||
try:
|
|
||||||
importer_ssl_client_cert = importer_ssl_client_cert_file_object.read()
|
importer_ssl_client_cert = importer_ssl_client_cert_file_object.read()
|
||||||
finally:
|
|
||||||
importer_ssl_client_cert_file_object.close()
|
|
||||||
|
|
||||||
if importer_ssl_client_key is not None:
|
if importer_ssl_client_key is not None:
|
||||||
importer_ssl_client_key_file_path = os.path.abspath(importer_ssl_client_key)
|
importer_ssl_client_key_file_path = os.path.abspath(importer_ssl_client_key)
|
||||||
if os.path.isfile(importer_ssl_client_key_file_path):
|
if os.path.isfile(importer_ssl_client_key_file_path):
|
||||||
importer_ssl_client_key_file_object = open(importer_ssl_client_key_file_path, 'r')
|
with open(importer_ssl_client_key_file_path, 'r') as importer_ssl_client_key_file_object:
|
||||||
try:
|
|
||||||
importer_ssl_client_key = importer_ssl_client_key_file_object.read()
|
importer_ssl_client_key = importer_ssl_client_key_file_object.read()
|
||||||
finally:
|
|
||||||
importer_ssl_client_key_file_object.close()
|
|
||||||
|
|
||||||
server = pulp_server(module, pulp_host, repo_type, wait_for_completion=wait_for_completion)
|
server = pulp_server(module, pulp_host, repo_type, wait_for_completion=wait_for_completion)
|
||||||
server.set_repo_list()
|
server.set_repo_list()
|
||||||
|
|
|
@ -308,9 +308,8 @@ class Rhsm(object):
|
||||||
else:
|
else:
|
||||||
cfg.set('main', 'enabled', '0')
|
cfg.set('main', 'enabled', '0')
|
||||||
|
|
||||||
fd = open(tmpfile, 'w+')
|
with open(tmpfile, 'w+') as fd:
|
||||||
cfg.write(fd)
|
cfg.write(fd)
|
||||||
fd.close()
|
|
||||||
self.module.atomic_move(tmpfile, plugin_conf)
|
self.module.atomic_move(tmpfile, plugin_conf)
|
||||||
|
|
||||||
def enable(self):
|
def enable(self):
|
||||||
|
|
|
@ -246,24 +246,22 @@ class Zone(object):
|
||||||
|
|
||||||
open('%s/root/noautoshutdown' % self.path, 'w').close()
|
open('%s/root/noautoshutdown' % self.path, 'w').close()
|
||||||
|
|
||||||
node = open('%s/root/etc/nodename' % self.path, 'w')
|
with open('%s/root/etc/nodename' % self.path, 'w') as node:
|
||||||
node.write(self.name)
|
node.write(self.name)
|
||||||
node.close()
|
|
||||||
|
|
||||||
id = open('%s/root/etc/.sysIDtool.state' % self.path, 'w')
|
with open('%s/root/etc/.sysIDtool.state' % self.path, 'w') as id:
|
||||||
id.write('1 # System previously configured?\n')
|
id.write('1 # System previously configured?\n')
|
||||||
id.write('1 # Bootparams succeeded?\n')
|
id.write('1 # Bootparams succeeded?\n')
|
||||||
id.write('1 # System is on a network?\n')
|
id.write('1 # System is on a network?\n')
|
||||||
id.write('1 # Extended network information gathered?\n')
|
id.write('1 # Extended network information gathered?\n')
|
||||||
id.write('0 # Autobinder succeeded?\n')
|
id.write('0 # Autobinder succeeded?\n')
|
||||||
id.write('1 # Network has subnets?\n')
|
id.write('1 # Network has subnets?\n')
|
||||||
id.write('1 # root password prompted for?\n')
|
id.write('1 # root password prompted for?\n')
|
||||||
id.write('1 # locale and term prompted for?\n')
|
id.write('1 # locale and term prompted for?\n')
|
||||||
id.write('1 # security policy in place\n')
|
id.write('1 # security policy in place\n')
|
||||||
id.write('1 # NFSv4 domain configured\n')
|
id.write('1 # NFSv4 domain configured\n')
|
||||||
id.write('0 # Auto Registration Configured\n')
|
id.write('0 # Auto Registration Configured\n')
|
||||||
id.write('vt100')
|
id.write('vt100')
|
||||||
id.close()
|
|
||||||
|
|
||||||
def configure_ssh_keys(self):
|
def configure_ssh_keys(self):
|
||||||
rsa_key_file = '%s/root/etc/ssh/ssh_host_rsa_key' % self.path
|
rsa_key_file = '%s/root/etc/ssh/ssh_host_rsa_key' % self.path
|
||||||
|
@ -284,9 +282,8 @@ class Zone(object):
|
||||||
def configure_password(self):
|
def configure_password(self):
|
||||||
shadow = '%s/root/etc/shadow' % self.path
|
shadow = '%s/root/etc/shadow' % self.path
|
||||||
if self.root_password:
|
if self.root_password:
|
||||||
f = open(shadow, 'r')
|
with open(shadow, 'r') as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
f.close()
|
|
||||||
|
|
||||||
for i in range(0, len(lines)):
|
for i in range(0, len(lines)):
|
||||||
fields = lines[i].split(':')
|
fields = lines[i].split(':')
|
||||||
|
@ -294,10 +291,9 @@ class Zone(object):
|
||||||
fields[1] = self.root_password
|
fields[1] = self.root_password
|
||||||
lines[i] = ':'.join(fields)
|
lines[i] = ':'.join(fields)
|
||||||
|
|
||||||
f = open(shadow, 'w')
|
with open(shadow, 'w') as f:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
f.write(line)
|
f.write(line)
|
||||||
f.close()
|
|
||||||
|
|
||||||
def boot(self):
|
def boot(self):
|
||||||
if not self.module.check_mode:
|
if not self.module.check_mode:
|
||||||
|
|
|
@ -460,15 +460,11 @@ def match_depends(module):
|
||||||
|
|
||||||
if depends_new:
|
if depends_new:
|
||||||
try:
|
try:
|
||||||
try:
|
with open(sorcery_depends, 'a') as fl:
|
||||||
fl = open(sorcery_depends, 'a')
|
|
||||||
|
|
||||||
for k in depends_new:
|
for k in depends_new:
|
||||||
fl.write("%s:%s:%s:optional::\n" % (spell, k, depends[k]))
|
fl.write("%s:%s:%s:optional::\n" % (spell, k, depends[k]))
|
||||||
except IOError:
|
except IOError:
|
||||||
module.fail_json(msg="I/O error on the depends file")
|
module.fail_json(msg="I/O error on the depends file")
|
||||||
finally:
|
|
||||||
fl.close()
|
|
||||||
|
|
||||||
depends_ok = False
|
depends_ok = False
|
||||||
|
|
||||||
|
|
|
@ -396,7 +396,8 @@ class NosystemdTimezone(Timezone):
|
||||||
self.conf_files['name'] = '/etc/sysconfig/clock'
|
self.conf_files['name'] = '/etc/sysconfig/clock'
|
||||||
self.conf_files['hwclock'] = '/etc/sysconfig/clock'
|
self.conf_files['hwclock'] = '/etc/sysconfig/clock'
|
||||||
try:
|
try:
|
||||||
f = open(self.conf_files['name'], 'r')
|
with open(self.conf_files['name'], 'r') as f:
|
||||||
|
sysconfig_clock = f.read()
|
||||||
except IOError as err:
|
except IOError as err:
|
||||||
if self._allow_ioerror(err, 'name'):
|
if self._allow_ioerror(err, 'name'):
|
||||||
# If the config file doesn't exist detect the distribution and set regexps.
|
# If the config file doesn't exist detect the distribution and set regexps.
|
||||||
|
@ -414,8 +415,6 @@ class NosystemdTimezone(Timezone):
|
||||||
# The key for timezone might be `ZONE` or `TIMEZONE`
|
# The key for timezone might be `ZONE` or `TIMEZONE`
|
||||||
# (the former is used in RHEL/CentOS and the latter is used in SUSE linux).
|
# (the former is used in RHEL/CentOS and the latter is used in SUSE linux).
|
||||||
# So check the content of /etc/sysconfig/clock and decide which key to use.
|
# So check the content of /etc/sysconfig/clock and decide which key to use.
|
||||||
sysconfig_clock = f.read()
|
|
||||||
f.close()
|
|
||||||
if re.search(r'^TIMEZONE\s*=', sysconfig_clock, re.MULTILINE):
|
if re.search(r'^TIMEZONE\s*=', sysconfig_clock, re.MULTILINE):
|
||||||
# For SUSE
|
# For SUSE
|
||||||
self.regexps['name'] = self.dist_regexps['SuSE']
|
self.regexps['name'] = self.dist_regexps['SuSE']
|
||||||
|
@ -448,15 +447,13 @@ class NosystemdTimezone(Timezone):
|
||||||
"""
|
"""
|
||||||
# Read the file
|
# Read the file
|
||||||
try:
|
try:
|
||||||
file = open(filename, 'r')
|
with open(filename, 'r') as file:
|
||||||
|
lines = file.readlines()
|
||||||
except IOError as err:
|
except IOError as err:
|
||||||
if self._allow_ioerror(err, key):
|
if self._allow_ioerror(err, key):
|
||||||
lines = []
|
lines = []
|
||||||
else:
|
else:
|
||||||
self.abort('tried to configure %s using a file "%s", but could not read it' % (key, filename))
|
self.abort('tried to configure %s using a file "%s", but could not read it' % (key, filename))
|
||||||
else:
|
|
||||||
lines = file.readlines()
|
|
||||||
file.close()
|
|
||||||
# Find the all matched lines
|
# Find the all matched lines
|
||||||
matched_indices = []
|
matched_indices = []
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
|
@ -473,18 +470,17 @@ class NosystemdTimezone(Timezone):
|
||||||
lines.insert(insert_line, value)
|
lines.insert(insert_line, value)
|
||||||
# Write the changes
|
# Write the changes
|
||||||
try:
|
try:
|
||||||
file = open(filename, 'w')
|
with open(filename, 'w') as file:
|
||||||
|
file.writelines(lines)
|
||||||
except IOError:
|
except IOError:
|
||||||
self.abort('tried to configure %s using a file "%s", but could not write to it' % (key, filename))
|
self.abort('tried to configure %s using a file "%s", but could not write to it' % (key, filename))
|
||||||
else:
|
|
||||||
file.writelines(lines)
|
|
||||||
file.close()
|
|
||||||
self.msg.append('Added 1 line and deleted %s line(s) on %s' % (len(matched_indices), filename))
|
self.msg.append('Added 1 line and deleted %s line(s) on %s' % (len(matched_indices), filename))
|
||||||
|
|
||||||
def _get_value_from_config(self, key, phase):
|
def _get_value_from_config(self, key, phase):
|
||||||
filename = self.conf_files[key]
|
filename = self.conf_files[key]
|
||||||
try:
|
try:
|
||||||
file = open(filename, mode='r')
|
with open(filename, mode='r') as file:
|
||||||
|
status = file.read()
|
||||||
except IOError as err:
|
except IOError as err:
|
||||||
if self._allow_ioerror(err, key):
|
if self._allow_ioerror(err, key):
|
||||||
if key == 'hwclock':
|
if key == 'hwclock':
|
||||||
|
@ -496,8 +492,6 @@ class NosystemdTimezone(Timezone):
|
||||||
else:
|
else:
|
||||||
self.abort('tried to configure %s using a file "%s", but could not read it' % (key, filename))
|
self.abort('tried to configure %s using a file "%s", but could not read it' % (key, filename))
|
||||||
else:
|
else:
|
||||||
status = file.read()
|
|
||||||
file.close()
|
|
||||||
try:
|
try:
|
||||||
value = self.regexps[key].search(status).group(1)
|
value = self.regexps[key].search(status).group(1)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -628,11 +622,11 @@ class SmartOSTimezone(Timezone):
|
||||||
"""
|
"""
|
||||||
if key == 'name':
|
if key == 'name':
|
||||||
try:
|
try:
|
||||||
f = open('/etc/default/init', 'r')
|
with open('/etc/default/init', 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
m = re.match('^TZ=(.*)$', line.strip())
|
m = re.match('^TZ=(.*)$', line.strip())
|
||||||
if m:
|
if m:
|
||||||
return m.groups()[0]
|
return m.groups()[0]
|
||||||
except Exception:
|
except Exception:
|
||||||
self.module.fail_json(msg='Failed to read /etc/default/init')
|
self.module.fail_json(msg='Failed to read /etc/default/init')
|
||||||
else:
|
else:
|
||||||
|
@ -811,9 +805,8 @@ class AIXTimezone(Timezone):
|
||||||
def __get_timezone(self):
|
def __get_timezone(self):
|
||||||
""" Return the current value of TZ= in /etc/environment """
|
""" Return the current value of TZ= in /etc/environment """
|
||||||
try:
|
try:
|
||||||
f = open('/etc/environment', 'r')
|
with open('/etc/environment', 'r') as f:
|
||||||
etcenvironment = f.read()
|
etcenvironment = f.read()
|
||||||
f.close()
|
|
||||||
except Exception:
|
except Exception:
|
||||||
self.module.fail_json(msg='Issue reading contents of /etc/environment')
|
self.module.fail_json(msg='Issue reading contents of /etc/environment')
|
||||||
|
|
||||||
|
|
|
@ -36,16 +36,14 @@ def inventory():
|
||||||
|
|
||||||
|
|
||||||
def load_txt_data(path):
|
def load_txt_data(path):
|
||||||
f = open(path, 'r')
|
with open(path, 'r') as f:
|
||||||
s = f.read()
|
s = f.read()
|
||||||
f.close()
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
def load_yml_data(path):
|
def load_yml_data(path):
|
||||||
f = open(path, 'r')
|
with open(path, 'r') as f:
|
||||||
d = yaml.safe_load(f)
|
d = yaml.safe_load(f)
|
||||||
f.close()
|
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -896,15 +896,14 @@ class TestWdcRedfishCommand(unittest.TestCase):
|
||||||
bundle_tarfile = tarfile.open(os.path.join(self.tempdir, tar_name), "w")
|
bundle_tarfile = tarfile.open(os.path.join(self.tempdir, tar_name), "w")
|
||||||
package_filename = "oobm-{0}.pkg".format(mock_firmware_version)
|
package_filename = "oobm-{0}.pkg".format(mock_firmware_version)
|
||||||
package_filename_path = os.path.join(self.tempdir, package_filename)
|
package_filename_path = os.path.join(self.tempdir, package_filename)
|
||||||
package_file = open(package_filename_path, "w")
|
with open(package_filename_path, "w"):
|
||||||
package_file.close()
|
pass
|
||||||
bundle_tarfile.add(os.path.join(self.tempdir, package_filename), arcname=package_filename)
|
bundle_tarfile.add(os.path.join(self.tempdir, package_filename), arcname=package_filename)
|
||||||
bin_filename = "firmware.bin"
|
bin_filename = "firmware.bin"
|
||||||
bin_filename_path = os.path.join(self.tempdir, bin_filename)
|
bin_filename_path = os.path.join(self.tempdir, bin_filename)
|
||||||
bin_file = open(bin_filename_path, "wb")
|
with open(bin_filename_path, "wb") as bin_file:
|
||||||
byte_to_write = b'\x80' if is_multi_tenant else b'\xFF'
|
byte_to_write = b'\x80' if is_multi_tenant else b'\xFF'
|
||||||
bin_file.write(byte_to_write * 12)
|
bin_file.write(byte_to_write * 12)
|
||||||
bin_file.close()
|
|
||||||
for filename in [package_filename, bin_filename]:
|
for filename in [package_filename, bin_filename]:
|
||||||
bundle_tarfile.add(os.path.join(self.tempdir, filename), arcname=filename)
|
bundle_tarfile.add(os.path.join(self.tempdir, filename), arcname=filename)
|
||||||
bundle_tarfile.close()
|
bundle_tarfile.close()
|
||||||
|
|
Loading…
Reference in New Issue