diff --git a/changelogs/fragments/349-pacman_improve_group_expansion_speed.yml b/changelogs/fragments/349-pacman_improve_group_expansion_speed.yml new file mode 100644 index 0000000000..5f5412a5d4 --- /dev/null +++ b/changelogs/fragments/349-pacman_improve_group_expansion_speed.yml @@ -0,0 +1,2 @@ +minor_changes: +- "pacman - improve group expansion speed: query list of pacman groups once (https://github.com/ansible-collections/community.general/pull/349)." diff --git a/plugins/modules/packaging/os/pacman.py b/plugins/modules/packaging/os/pacman.py index 12ea790760..448a3d19b5 100644 --- a/plugins/modules/packaging/os/pacman.py +++ b/plugins/modules/packaging/os/pacman.py @@ -396,17 +396,16 @@ def check_packages(module, pacman_path, packages, state): def expand_package_groups(module, pacman_path, pkgs): expanded = [] + __, stdout, __ = module.run_command([pacman_path, "--sync", "--groups", "--quiet"], check_rc=True) + available_groups = stdout.splitlines() + for pkg in pkgs: if pkg: # avoid empty strings - cmd = "%s --sync --groups --quiet %s" % (pacman_path, pkg) - rc, stdout, stderr = module.run_command(cmd, check_rc=False) - - if rc == 0: - # A group was found matching the name, so expand it - for name in stdout.split('\n'): - name = name.strip() - if name: - expanded.append(name) + if pkg in available_groups: + # A group was found matching the package name: expand it + cmd = [pacman_path, "--sync", "--groups", "--quiet", pkg] + rc, stdout, stderr = module.run_command(cmd, check_rc=True) + expanded.extend([name.strip() for name in stdout.splitlines()]) else: expanded.append(pkg)