mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use try/finally with file opening to close the file
This commit is contained in:
parent
bdf9623f56
commit
42bca5398b
2 changed files with 44 additions and 28 deletions
|
@ -155,8 +155,10 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if changed and not module.check_mode:
|
if changed and not module.check_mode:
|
||||||
|
try:
|
||||||
f = open(path, 'wb')
|
f = open(path, 'wb')
|
||||||
f.write(str(crypttab))
|
f.write(str(crypttab))
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
module.exit_json(changed=changed, msg=reason, **module.params)
|
module.exit_json(changed=changed, msg=reason, **module.params)
|
||||||
|
@ -173,9 +175,11 @@ class Crypttab(object):
|
||||||
os.makedirs(os.path.dirname(path))
|
os.makedirs(os.path.dirname(path))
|
||||||
open(path,'a').close()
|
open(path,'a').close()
|
||||||
|
|
||||||
|
try:
|
||||||
f = open(path, 'r')
|
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()
|
f.close()
|
||||||
|
|
||||||
def add(self, line):
|
def add(self, line):
|
||||||
|
|
|
@ -77,11 +77,15 @@ def fix_case(name):
|
||||||
|
|
||||||
def replace_line(existing_line, new_line):
|
def replace_line(existing_line, new_line):
|
||||||
"""Replaces lines in /etc/locale.gen"""
|
"""Replaces lines in /etc/locale.gen"""
|
||||||
|
try:
|
||||||
f = open("/etc/locale.gen", "r")
|
f = open("/etc/locale.gen", "r")
|
||||||
lines = [line.replace(existing_line, new_line) for line in f]
|
lines = [line.replace(existing_line, new_line) for line in f]
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
try:
|
||||||
f = open("/etc/locale.gen", "w")
|
f = open("/etc/locale.gen", "w")
|
||||||
f.write("".join(lines))
|
f.write("".join(lines))
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def set_locale(name, enabled=True):
|
def set_locale(name, enabled=True):
|
||||||
|
@ -91,11 +95,15 @@ def set_locale(name, enabled=True):
|
||||||
new_string = '%s \g<charset>' % (name)
|
new_string = '%s \g<charset>' % (name)
|
||||||
else:
|
else:
|
||||||
new_string = '# %s \g<charset>' % (name)
|
new_string = '# %s \g<charset>' % (name)
|
||||||
|
try:
|
||||||
f = open("/etc/locale.gen", "r")
|
f = open("/etc/locale.gen", "r")
|
||||||
lines = [re.sub(search_string, new_string, line) for line in f]
|
lines = [re.sub(search_string, new_string, line) for line in f]
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
try:
|
||||||
f = open("/etc/locale.gen", "w")
|
f = open("/etc/locale.gen", "w")
|
||||||
f.write("".join(lines))
|
f.write("".join(lines))
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def apply_change(targetState, name):
|
def apply_change(targetState, name):
|
||||||
|
@ -129,14 +137,18 @@ def apply_change_ubuntu(targetState, name):
|
||||||
localeGenExitValue = call(["locale-gen", name])
|
localeGenExitValue = call(["locale-gen", name])
|
||||||
else:
|
else:
|
||||||
# Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales.
|
# Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales.
|
||||||
|
try:
|
||||||
f = open("/var/lib/locales/supported.d/local", "r")
|
f = open("/var/lib/locales/supported.d/local", "r")
|
||||||
content = f.readlines()
|
content = f.readlines()
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
try:
|
||||||
f = open("/var/lib/locales/supported.d/local", "w")
|
f = open("/var/lib/locales/supported.d/local", "w")
|
||||||
for line in content:
|
for line in content:
|
||||||
locale, charset = line.split(' ')
|
locale, charset = line.split(' ')
|
||||||
if locale != name:
|
if locale != name:
|
||||||
f.write(line)
|
f.write(line)
|
||||||
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
# Purge locales and regenerate.
|
# Purge locales and regenerate.
|
||||||
# Please provide a patch if you know how to avoid regenerating the locales to keep!
|
# Please provide a patch if you know how to avoid regenerating the locales to keep!
|
||||||
|
|
Loading…
Reference in a new issue