From 42bca5398bbc62192c7be5f7f071a5d03f030399 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 11 May 2015 10:29:28 -0500 Subject: [PATCH] Use try/finally with file opening to close the file --- lib/ansible/modules/extras/system/crypttab.py | 18 ++++--- .../modules/extras/system/locale_gen.py | 54 +++++++++++-------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/lib/ansible/modules/extras/system/crypttab.py b/lib/ansible/modules/extras/system/crypttab.py index 5621190244a..52f3e75576a 100644 --- a/lib/ansible/modules/extras/system/crypttab.py +++ b/lib/ansible/modules/extras/system/crypttab.py @@ -155,9 +155,11 @@ def main(): if changed and not module.check_mode: - f = open(path, 'wb') - f.write(str(crypttab)) - f.close() + try: + f = open(path, 'wb') + f.write(str(crypttab)) + finally: + f.close() module.exit_json(changed=changed, msg=reason, **module.params) @@ -173,10 +175,12 @@ class Crypttab(object): os.makedirs(os.path.dirname(path)) open(path,'a').close() - f = open(path, 'r') - for line in f.readlines(): - self._lines.append(Line(line)) - f.close() + try: + f = open(path, 'r') + for line in f.readlines(): + self._lines.append(Line(line)) + finally: + f.close() def add(self, line): self._lines.append(line) diff --git a/lib/ansible/modules/extras/system/locale_gen.py b/lib/ansible/modules/extras/system/locale_gen.py index ea40b598d4e..c4b2af7dc1b 100644 --- a/lib/ansible/modules/extras/system/locale_gen.py +++ b/lib/ansible/modules/extras/system/locale_gen.py @@ -77,12 +77,16 @@ def fix_case(name): def replace_line(existing_line, new_line): """Replaces lines in /etc/locale.gen""" - f = open("/etc/locale.gen", "r") - lines = [line.replace(existing_line, new_line) for line in f] - f.close() - f = open("/etc/locale.gen", "w") - f.write("".join(lines)) - f.close() + try: + f = open("/etc/locale.gen", "r") + lines = [line.replace(existing_line, new_line) for line in f] + finally: + f.close() + try: + f = open("/etc/locale.gen", "w") + f.write("".join(lines)) + finally: + f.close() def set_locale(name, enabled=True): """ Sets the state of the locale. Defaults to enabled. """ @@ -91,12 +95,16 @@ def set_locale(name, enabled=True): new_string = '%s \g' % (name) else: new_string = '# %s \g' % (name) - f = open("/etc/locale.gen", "r") - lines = [re.sub(search_string, new_string, line) for line in f] - f.close() - f = open("/etc/locale.gen", "w") - f.write("".join(lines)) - f.close() + try: + f = open("/etc/locale.gen", "r") + lines = [re.sub(search_string, new_string, line) for line in f] + finally: + f.close() + try: + f = open("/etc/locale.gen", "w") + f.write("".join(lines)) + finally: + f.close() def apply_change(targetState, name): """Create or remove locale. @@ -129,15 +137,19 @@ def apply_change_ubuntu(targetState, name): localeGenExitValue = call(["locale-gen", name]) else: # Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales. - f = open("/var/lib/locales/supported.d/local", "r") - content = f.readlines() - f.close() - f = open("/var/lib/locales/supported.d/local", "w") - for line in content: - locale, charset = line.split(' ') - if locale != name: - f.write(line) - f.close() + try: + f = open("/var/lib/locales/supported.d/local", "r") + content = f.readlines() + finally: + f.close() + try: + f = open("/var/lib/locales/supported.d/local", "w") + for line in content: + locale, charset = line.split(' ') + if locale != name: + f.write(line) + finally: + f.close() # Purge locales and regenerate. # Please provide a patch if you know how to avoid regenerating the locales to keep! localeGenExitValue = call(["locale-gen", "--purge"])