1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-16 08:53:15 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -74,17 +74,16 @@ from ansible.module_utils.basic import AnsibleModule
def query_package(module, slackpkg_path, name):
import platform
import os
import re
machine = platform.machine()
# Exception for kernel-headers package on x86_64
if name == 'kernel-headers' and machine == 'x86_64':
machine = 'x86'
pattern = re.compile(f'^{re.escape(name)}-[^-]+-({re.escape(machine)}|noarch|fw)-[^-]+$')
packages = [f for f in os.listdir('/var/log/packages') if pattern.match(f)]
if name == "kernel-headers" and machine == "x86_64":
machine = "x86"
pattern = re.compile(f"^{re.escape(name)}-[^-]+-({re.escape(machine)}|noarch|fw)-[^-]+$")
packages = [f for f in os.listdir("/var/log/packages") if pattern.match(f)]
if len(packages) > 0:
return True
@ -93,7 +92,6 @@ def query_package(module, slackpkg_path, name):
def remove_packages(module, slackpkg_path, packages):
remove_c = 0
# Using a for loop in case of error, we can report the package that failed
for package in packages:
@ -102,24 +100,20 @@ def remove_packages(module, slackpkg_path, packages):
continue
if not module.check_mode:
rc, out, err = module.run_command(
[slackpkg_path, "-default_answer=y", "-batch=on", "remove", package])
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "remove", package])
if not module.check_mode and query_package(module, slackpkg_path,
package):
if not module.check_mode and query_package(module, slackpkg_path, package):
module.fail_json(msg=f"failed to remove {package}: {out}")
remove_c += 1
if remove_c > 0:
module.exit_json(changed=True, msg=f"removed {remove_c} package(s)")
module.exit_json(changed=False, msg="package(s) already absent")
def install_packages(module, slackpkg_path, packages):
install_c = 0
for package in packages:
@ -127,13 +121,10 @@ def install_packages(module, slackpkg_path, packages):
continue
if not module.check_mode:
rc, out, err = module.run_command(
[slackpkg_path, "-default_answer=y", "-batch=on", "install", package])
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "install", package])
if not module.check_mode and not query_package(module, slackpkg_path,
package):
module.fail_json(msg=f"failed to install {package}: {out}",
stderr=err)
if not module.check_mode and not query_package(module, slackpkg_path, package):
module.fail_json(msg=f"failed to install {package}: {out}", stderr=err)
install_c += 1
@ -148,13 +139,10 @@ def upgrade_packages(module, slackpkg_path, packages):
for package in packages:
if not module.check_mode:
rc, out, err = module.run_command(
[slackpkg_path, "-default_answer=y", "-batch=on", "upgrade", package])
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "upgrade", package])
if not module.check_mode and not query_package(module, slackpkg_path,
package):
module.fail_json(msg=f"failed to install {package}: {out}",
stderr=err)
if not module.check_mode and not query_package(module, slackpkg_path, package):
module.fail_json(msg=f"failed to install {package}: {out}", stderr=err)
install_c += 1
@ -165,8 +153,7 @@ def upgrade_packages(module, slackpkg_path, packages):
def update_cache(module, slackpkg_path):
rc, out, err = module.run_command(
[slackpkg_path, "-batch=on", "update"])
rc, out, err = module.run_command([slackpkg_path, "-batch=on", "update"])
if rc != 0:
module.fail_json(msg="Could not update package cache")
@ -174,30 +161,31 @@ def update_cache(module, slackpkg_path):
def main():
module = AnsibleModule(
argument_spec=dict(
state=dict(default="present", choices=['installed', 'removed', 'absent', 'present', 'latest']),
name=dict(aliases=["pkg"], required=True, type='list', elements='str'),
update_cache=dict(default=False, type='bool'),
state=dict(default="present", choices=["installed", "removed", "absent", "present", "latest"]),
name=dict(aliases=["pkg"], required=True, type="list", elements="str"),
update_cache=dict(default=False, type="bool"),
),
supports_check_mode=True)
supports_check_mode=True,
)
slackpkg_path = module.get_bin_path('slackpkg', True)
slackpkg_path = module.get_bin_path("slackpkg", True)
p = module.params
pkgs = p['name']
pkgs = p["name"]
if p["update_cache"]:
update_cache(module, slackpkg_path)
if p['state'] == 'latest':
if p["state"] == "latest":
upgrade_packages(module, slackpkg_path, pkgs)
elif p['state'] in ['present', 'installed']:
elif p["state"] in ["present", "installed"]:
install_packages(module, slackpkg_path, pkgs)
elif p["state"] in ['removed', 'absent']:
elif p["state"] in ["removed", "absent"]:
remove_packages(module, slackpkg_path, pkgs)
if __name__ == '__main__':
if __name__ == "__main__":
main()