1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fixes duplicate code

Fixed duplicate call for file attributes checking
This commit is contained in:
Michel Blanc 2013-02-22 17:30:19 +01:00
parent 008dc4831e
commit 633fdede38

View file

@ -103,10 +103,11 @@ examples:
""" """
def check_file(module, changed, message=""): def check_file_attrs(module, changed, message):
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
if module.set_file_attributes_if_different(file_args, False): if module.set_file_attributes_if_different(file_args, False):
if changed: if changed:
message += " and " message += " and "
changed = True changed = True
@ -130,6 +131,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
lines = f.readlines() lines = f.readlines()
f.close() f.close()
msg = ""
mre = re.compile(regexp) mre = re.compile(regexp)
if not mre.search(line): if not mre.search(line):
module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp) module.fail_json(msg="usage error: line= doesn't match regexp (%s)" % regexp)
@ -182,9 +185,6 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
msg = 'line added' msg = 'line added'
changed = True changed = True
file_args = module.load_file_common_arguments(module.params)
changed = module.set_file_attributes_if_different(file_args, changed)
if changed and not module.check_mode: if changed and not module.check_mode:
if backup and os.path.exists(dest): if backup and os.path.exists(dest):
module.backup_local(dest) module.backup_local(dest)
@ -192,7 +192,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, backu
f.writelines(lines) f.writelines(lines)
f.close() f.close()
[ msg, changed ] = check_file(module, changed, msg) [ msg, changed ] = check_file_attrs(module, changed, msg)
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg)
def absent(module, dest, regexp, backup): def absent(module, dest, regexp, backup):
@ -203,6 +203,7 @@ def absent(module, dest, regexp, backup):
module.exit_json(changed=False, msg="file not present") module.exit_json(changed=False, msg="file not present")
msg = "" msg = ""
f = open(dest, 'rb') f = open(dest, 'rb')
lines = f.readlines() lines = f.readlines()
f.close() f.close()
@ -228,8 +229,7 @@ def absent(module, dest, regexp, backup):
if changed: if changed:
msg = "%s line(s) removed" % len(found) msg = "%s line(s) removed" % len(found)
[ msg, changed ] = check_file(module, changed, msg) [ msg, changed ] = check_file_attrs(module, changed, msg)
module.exit_json(changed=changed, found=len(found), msg=msg) module.exit_json(changed=changed, found=len(found), msg=msg)
def main(): def main():