mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
Reformat everything.
This commit is contained in:
@@ -150,50 +150,50 @@ from ansible_collections.community.general.plugins.module_utils.version import L
|
||||
|
||||
|
||||
def diversion_state(module, command, path):
|
||||
diversion = dict(path=path, state='absent', divert=None, holder=None)
|
||||
rc, out, err = module.run_command([command, '--listpackage', path], check_rc=True)
|
||||
diversion = dict(path=path, state="absent", divert=None, holder=None)
|
||||
rc, out, err = module.run_command([command, "--listpackage", path], check_rc=True)
|
||||
if out:
|
||||
diversion['state'] = 'present'
|
||||
diversion['holder'] = out.rstrip()
|
||||
rc, out, err = module.run_command([command, '--truename', path], check_rc=True)
|
||||
diversion['divert'] = out.rstrip()
|
||||
diversion["state"] = "present"
|
||||
diversion["holder"] = out.rstrip()
|
||||
rc, out, err = module.run_command([command, "--truename", path], check_rc=True)
|
||||
diversion["divert"] = out.rstrip()
|
||||
return diversion
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
path=dict(required=True, type='path'),
|
||||
state=dict(type='str', default='present', choices=['absent', 'present']),
|
||||
holder=dict(type='str'),
|
||||
divert=dict(type='path'),
|
||||
rename=dict(type='bool', default=False),
|
||||
force=dict(type='bool', default=False),
|
||||
path=dict(required=True, type="path"),
|
||||
state=dict(type="str", default="present", choices=["absent", "present"]),
|
||||
holder=dict(type="str"),
|
||||
divert=dict(type="path"),
|
||||
rename=dict(type="bool", default=False),
|
||||
force=dict(type="bool", default=False),
|
||||
),
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
path = module.params['path']
|
||||
state = module.params['state']
|
||||
holder = module.params['holder']
|
||||
divert = module.params['divert']
|
||||
rename = module.params['rename']
|
||||
force = module.params['force']
|
||||
path = module.params["path"]
|
||||
state = module.params["state"]
|
||||
holder = module.params["holder"]
|
||||
divert = module.params["divert"]
|
||||
rename = module.params["rename"]
|
||||
force = module.params["force"]
|
||||
|
||||
diversion_wanted = dict(path=path, state=state)
|
||||
changed = False
|
||||
|
||||
DPKG_DIVERT = module.get_bin_path('dpkg-divert', required=True)
|
||||
DPKG_DIVERT = module.get_bin_path("dpkg-divert", required=True)
|
||||
MAINCOMMAND = [DPKG_DIVERT]
|
||||
|
||||
# Option --listpackage is needed and comes with 1.15.0
|
||||
rc, stdout, stderr = module.run_command([DPKG_DIVERT, '--version'], check_rc=True)
|
||||
[current_version] = [x for x in stdout.splitlines()[0].split() if re.match('^[0-9]+[.][0-9]', x)]
|
||||
rc, stdout, stderr = module.run_command([DPKG_DIVERT, "--version"], check_rc=True)
|
||||
[current_version] = [x for x in stdout.splitlines()[0].split() if re.match("^[0-9]+[.][0-9]", x)]
|
||||
if LooseVersion(current_version) < LooseVersion("1.15.0"):
|
||||
module.fail_json(msg="Unsupported dpkg version (<1.15.0).")
|
||||
no_rename_is_supported = (LooseVersion(current_version) >= LooseVersion("1.19.1"))
|
||||
no_rename_is_supported = LooseVersion(current_version) >= LooseVersion("1.19.1")
|
||||
|
||||
b_path = to_bytes(path, errors='surrogate_or_strict')
|
||||
b_path = to_bytes(path, errors="surrogate_or_strict")
|
||||
path_exists = os.path.exists(b_path)
|
||||
# Used for things not doable with a single dpkg-divert command (as forced
|
||||
# renaming of files, and diversion's 'holder' or 'divert' updates).
|
||||
@@ -201,48 +201,48 @@ def main():
|
||||
truename_exists = False
|
||||
|
||||
diversion_before = diversion_state(module, DPKG_DIVERT, path)
|
||||
if diversion_before['state'] == 'present':
|
||||
b_divert = to_bytes(diversion_before['divert'], errors='surrogate_or_strict')
|
||||
if diversion_before["state"] == "present":
|
||||
b_divert = to_bytes(diversion_before["divert"], errors="surrogate_or_strict")
|
||||
truename_exists = os.path.exists(b_divert)
|
||||
|
||||
# Append options as requested in the task parameters, but ignore some of
|
||||
# them when removing the diversion.
|
||||
if rename:
|
||||
MAINCOMMAND.append('--rename')
|
||||
MAINCOMMAND.append("--rename")
|
||||
elif no_rename_is_supported:
|
||||
MAINCOMMAND.append('--no-rename')
|
||||
MAINCOMMAND.append("--no-rename")
|
||||
|
||||
if state == 'present':
|
||||
if holder and holder != 'LOCAL':
|
||||
MAINCOMMAND.extend(['--package', holder])
|
||||
diversion_wanted['holder'] = holder
|
||||
if state == "present":
|
||||
if holder and holder != "LOCAL":
|
||||
MAINCOMMAND.extend(["--package", holder])
|
||||
diversion_wanted["holder"] = holder
|
||||
else:
|
||||
MAINCOMMAND.append('--local')
|
||||
diversion_wanted['holder'] = 'LOCAL'
|
||||
MAINCOMMAND.append("--local")
|
||||
diversion_wanted["holder"] = "LOCAL"
|
||||
|
||||
if divert:
|
||||
MAINCOMMAND.extend(['--divert', divert])
|
||||
MAINCOMMAND.extend(["--divert", divert])
|
||||
target = divert
|
||||
else:
|
||||
target = f'{path}.distrib'
|
||||
target = f"{path}.distrib"
|
||||
|
||||
MAINCOMMAND.extend(['--add', path])
|
||||
diversion_wanted['divert'] = target
|
||||
b_target = to_bytes(target, errors='surrogate_or_strict')
|
||||
MAINCOMMAND.extend(["--add", path])
|
||||
diversion_wanted["divert"] = target
|
||||
b_target = to_bytes(target, errors="surrogate_or_strict")
|
||||
target_exists = os.path.exists(b_target)
|
||||
|
||||
else:
|
||||
MAINCOMMAND.extend(['--remove', path])
|
||||
diversion_wanted['divert'] = None
|
||||
diversion_wanted['holder'] = None
|
||||
MAINCOMMAND.extend(["--remove", path])
|
||||
diversion_wanted["divert"] = None
|
||||
diversion_wanted["holder"] = None
|
||||
|
||||
# Start to populate the returned objects.
|
||||
diversion = diversion_before.copy()
|
||||
maincommand = ' '.join(MAINCOMMAND)
|
||||
maincommand = " ".join(MAINCOMMAND)
|
||||
commands = [maincommand]
|
||||
|
||||
if module.check_mode or diversion_wanted == diversion_before:
|
||||
MAINCOMMAND.insert(1, '--test')
|
||||
MAINCOMMAND.insert(1, "--test")
|
||||
diversion_after = diversion_wanted
|
||||
|
||||
# Just try and see
|
||||
@@ -257,34 +257,39 @@ def main():
|
||||
# - The renaming is forbidden by dpkg-divert (i.e. both the file and the
|
||||
# diverted file exist)
|
||||
|
||||
elif state != diversion_before['state']:
|
||||
elif state != diversion_before["state"]:
|
||||
# There should be no case with 'divert' and 'holder' when creating the
|
||||
# diversion from none, and they're ignored when removing the diversion.
|
||||
# So this is all about renaming...
|
||||
if rename and path_exists and (
|
||||
(state == 'absent' and truename_exists) or
|
||||
(state == 'present' and target_exists)):
|
||||
if (
|
||||
rename
|
||||
and path_exists
|
||||
and ((state == "absent" and truename_exists) or (state == "present" and target_exists))
|
||||
):
|
||||
if not force:
|
||||
msg = "Set 'force' param to True to force renaming of files."
|
||||
module.fail_json(changed=changed, cmd=maincommand, rc=rc, msg=msg,
|
||||
stderr=stderr, stdout=stdout, diversion=diversion)
|
||||
module.fail_json(
|
||||
changed=changed, cmd=maincommand, rc=rc, msg=msg, stderr=stderr, stdout=stdout, diversion=diversion
|
||||
)
|
||||
else:
|
||||
msg = "Unexpected error while changing state of the diversion."
|
||||
module.fail_json(changed=changed, cmd=maincommand, rc=rc, msg=msg,
|
||||
stderr=stderr, stdout=stdout, diversion=diversion)
|
||||
module.fail_json(
|
||||
changed=changed, cmd=maincommand, rc=rc, msg=msg, stderr=stderr, stdout=stdout, diversion=diversion
|
||||
)
|
||||
|
||||
to_remove = path
|
||||
if state == 'present':
|
||||
if state == "present":
|
||||
to_remove = target
|
||||
|
||||
if not module.check_mode:
|
||||
try:
|
||||
b_remove = to_bytes(to_remove, errors='surrogate_or_strict')
|
||||
b_remove = to_bytes(to_remove, errors="surrogate_or_strict")
|
||||
os.unlink(b_remove)
|
||||
except OSError as e:
|
||||
msg = f'Failed to remove {to_remove}: {e}'
|
||||
module.fail_json(changed=changed, cmd=maincommand, rc=rc, msg=msg,
|
||||
stderr=stderr, stdout=stdout, diversion=diversion)
|
||||
msg = f"Failed to remove {to_remove}: {e}"
|
||||
module.fail_json(
|
||||
changed=changed, cmd=maincommand, rc=rc, msg=msg, stderr=stderr, stdout=stdout, diversion=diversion
|
||||
)
|
||||
rc, stdout, stderr = module.run_command(MAINCOMMAND, check_rc=True)
|
||||
|
||||
messages = [stdout.rstrip()]
|
||||
@@ -293,25 +298,25 @@ def main():
|
||||
# of an existing diversion. dpkg-divert does not handle this, and we have
|
||||
# to remove the existing diversion first, and then set a new one.
|
||||
else:
|
||||
RMDIVERSION = [DPKG_DIVERT, '--remove', path]
|
||||
RMDIVERSION = [DPKG_DIVERT, "--remove", path]
|
||||
if no_rename_is_supported:
|
||||
RMDIVERSION.insert(1, '--no-rename')
|
||||
rmdiversion = ' '.join(RMDIVERSION)
|
||||
RMDIVERSION.insert(1, "--no-rename")
|
||||
rmdiversion = " ".join(RMDIVERSION)
|
||||
|
||||
if module.check_mode:
|
||||
RMDIVERSION.insert(1, '--test')
|
||||
RMDIVERSION.insert(1, "--test")
|
||||
|
||||
if rename:
|
||||
MAINCOMMAND.remove('--rename')
|
||||
MAINCOMMAND.remove("--rename")
|
||||
if no_rename_is_supported:
|
||||
MAINCOMMAND.insert(1, '--no-rename')
|
||||
maincommand = ' '.join(MAINCOMMAND)
|
||||
MAINCOMMAND.insert(1, "--no-rename")
|
||||
maincommand = " ".join(MAINCOMMAND)
|
||||
|
||||
commands = [rmdiversion, maincommand]
|
||||
rc, rmdout, rmderr = module.run_command(RMDIVERSION, check_rc=True)
|
||||
|
||||
if module.check_mode:
|
||||
messages = [rmdout.rstrip(), 'Running in check mode']
|
||||
messages = [rmdout.rstrip(), "Running in check mode"]
|
||||
else:
|
||||
rc, stdout, stderr = module.run_command(MAINCOMMAND, check_rc=True)
|
||||
messages = [rmdout.rstrip(), stdout.rstrip()]
|
||||
@@ -319,11 +324,11 @@ def main():
|
||||
# Avoid if possible to orphan files (i.e. to dereference them in diversion
|
||||
# database but let them in place), but do not make renaming issues fatal.
|
||||
# BTW, this module is not about state of files involved in the diversion.
|
||||
old = diversion_before['divert']
|
||||
new = diversion_wanted['divert']
|
||||
old = diversion_before["divert"]
|
||||
new = diversion_wanted["divert"]
|
||||
if new != old:
|
||||
b_old = to_bytes(old, errors='surrogate_or_strict')
|
||||
b_new = to_bytes(new, errors='surrogate_or_strict')
|
||||
b_old = to_bytes(old, errors="surrogate_or_strict")
|
||||
b_new = to_bytes(new, errors="surrogate_or_strict")
|
||||
if os.path.exists(b_old) and not os.path.exists(b_new):
|
||||
try:
|
||||
os.rename(b_old, b_new)
|
||||
@@ -336,20 +341,20 @@ def main():
|
||||
diversion = diversion_after.copy()
|
||||
diff = dict()
|
||||
if module._diff:
|
||||
diff['before'] = diversion_before
|
||||
diff['after'] = diversion_after
|
||||
diff["before"] = diversion_before
|
||||
diff["after"] = diversion_after
|
||||
|
||||
if diversion_after != diversion_before:
|
||||
changed = True
|
||||
|
||||
if diversion_after == diversion_wanted:
|
||||
module.exit_json(changed=changed, diversion=diversion,
|
||||
commands=commands, messages=messages, diff=diff)
|
||||
module.exit_json(changed=changed, diversion=diversion, commands=commands, messages=messages, diff=diff)
|
||||
else:
|
||||
msg = "Unexpected error: see stdout and stderr for details."
|
||||
module.fail_json(changed=changed, cmd=maincommand, rc=rc, msg=msg,
|
||||
stderr=stderr, stdout=stdout, diversion=diversion)
|
||||
module.fail_json(
|
||||
changed=changed, cmd=maincommand, rc=rc, msg=msg, stderr=stderr, stdout=stdout, diversion=diversion
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user