Set prev_state in the state functions

This seems a little like duplicating code since all of the called
functions need it but prev_state isn't part of argument parsing so it
doesn't belong in the toplevel main() function.
pull/39986/head
Toshio Kuratomi 7 years ago
parent 9be31fc79b
commit 03194880c1

@ -164,7 +164,7 @@ def additional_parameter_handling(params):
# state should default to file, but since that creates many conflicts, # state should default to file, but since that creates many conflicts,
# default state to 'current' when it exists. # default state to 'current' when it exists.
prev_state = get_state(params['path']) prev_state = get_state(to_bytes(params['path'], errors='surrogate_or_strict'))
if params['state'] is None: if params['state'] is None:
if prev_state != 'absent': if prev_state != 'absent':
@ -262,8 +262,9 @@ def execute_diff_peek(path):
return appears_binary return appears_binary
def ensure_absent(path, prev_state): def ensure_absent(path):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
result = {} result = {}
if prev_state != 'absent': if prev_state != 'absent':
@ -289,8 +290,10 @@ def ensure_absent(path, prev_state):
return result return result
def execute_touch(path, prev_state, follow): def execute_touch(path, follow):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
if not module.check_mode: if not module.check_mode:
if prev_state == 'absent': if prev_state == 'absent':
# Create an empty file if the filename did not already exist # Create an empty file if the filename did not already exist
@ -341,9 +344,11 @@ def execute_touch(path, prev_state, follow):
return {'dest': path, 'changed': True} return {'dest': path, 'changed': True}
def ensure_file_attributes(path, prev_state, follow): def ensure_file_attributes(path, follow):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
if prev_state != 'file': if prev_state != 'file':
if follow and prev_state == 'link': if follow and prev_state == 'link':
# follow symlink and operate on original # follow symlink and operate on original
@ -362,8 +367,10 @@ def ensure_file_attributes(path, prev_state, follow):
return {'path': path, 'changed': changed, 'diff': diff} return {'path': path, 'changed': changed, 'diff': diff}
def ensure_directory(path, prev_state, follow, recurse): def ensure_directory(path, follow, recurse):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
if follow and prev_state == 'link': if follow and prev_state == 'link':
b_path = os.path.realpath(b_path) b_path = os.path.realpath(b_path)
path = to_native(b_path, errors='strict') path = to_native(b_path, errors='strict')
@ -419,9 +426,11 @@ def ensure_directory(path, prev_state, follow, recurse):
return {'path': path, 'changed': changed, 'diff': diff} return {'path': path, 'changed': changed, 'diff': diff}
def ensure_symlink(path, src, b_src, prev_state, follow, force): def ensure_symlink(path, src, b_src, follow, force):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
# source is both the source of a symlink or an informational passing of the src for a template module # source is both the source of a symlink or an informational passing of the src for a template module
# or copy module, even if this module never uses it, it is needed to key off some things # or copy module, even if this module never uses it, it is needed to key off some things
if src is None: if src is None:
@ -528,9 +537,11 @@ def ensure_symlink(path, src, b_src, prev_state, follow, force):
return {'dest': path, 'src': src, 'changed': changed, 'diff': diff} return {'dest': path, 'src': src, 'changed': changed, 'diff': diff}
def ensure_hardlink(path, src, b_src, prev_state, follow, force): def ensure_hardlink(path, src, b_src, follow, force):
b_path = to_bytes(path, errors='surrogate_or_strict') b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
file_args = module.load_file_common_arguments(module.params) file_args = module.load_file_common_arguments(module.params)
# source is both the source of a symlink or an informational passing of the src for a template module # source is both the source of a symlink or an informational passing of the src for a template module
# or copy module, even if this module never uses it, it is needed to key off some things # or copy module, even if this module never uses it, it is needed to key off some things
if src is None: if src is None:
@ -656,9 +667,6 @@ def main():
src = params['src'] src = params['src']
b_src = params['b_src'] b_src = params['b_src']
b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
# short-circuit for diff_peek # short-circuit for diff_peek
if params['_diff_peek'] is not None: if params['_diff_peek'] is not None:
appears_binary = execute_diff_peek(to_bytes(path, errors='surrogate_or_strict')) appears_binary = execute_diff_peek(to_bytes(path, errors='surrogate_or_strict'))
@ -673,21 +681,19 @@ def main():
basename = os.path.basename(b_src) basename = os.path.basename(b_src)
if basename: if basename:
params['path'] = path = os.path.join(path, basename) params['path'] = path = os.path.join(path, basename)
b_path = to_bytes(path, errors='surrogate_or_strict')
prev_state = get_state(b_path)
if state == 'file': if state == 'file':
result = ensure_file_attributes(path, prev_state, follow) result = ensure_file_attributes(path, follow)
elif state == 'directory': elif state == 'directory':
result = ensure_directory(path, prev_state, follow, recurse) result = ensure_directory(path, follow, recurse)
elif state == 'link': elif state == 'link':
result = ensure_symlink(path, src, b_src, prev_state, follow, force) result = ensure_symlink(path, src, b_src, follow, force)
elif state == 'hard': elif state == 'hard':
result = ensure_hardlink(path, src, b_src, prev_state, follow, force) result = ensure_hardlink(path, src, b_src, follow, force)
elif state == 'touch': elif state == 'touch':
result = execute_touch(path, prev_state, follow) result = execute_touch(path, follow)
elif state == 'absent': elif state == 'absent':
result = ensure_absent(path, prev_state) result = ensure_absent(path)
module.exit_json(**result) module.exit_json(**result)

Loading…
Cancel
Save