From 43c805f7db1b6764e48c3ac3ba995040e967018f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20H=C3=B6tzel?= Date: Wed, 13 May 2020 08:46:05 +0200 Subject: [PATCH] Improve package state detection speed (#326) Don't query for full details of a package. It is sufficient to output the name and version. This also simplifies parsing the output. --- ..._improve_package_state_detection_speed.yml | 2 ++ plugins/modules/packaging/os/pacman.py | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/326-pacman_improve_package_state_detection_speed.yml diff --git a/changelogs/fragments/326-pacman_improve_package_state_detection_speed.yml b/changelogs/fragments/326-pacman_improve_package_state_detection_speed.yml new file mode 100644 index 0000000000..43eb03f0dd --- /dev/null +++ b/changelogs/fragments/326-pacman_improve_package_state_detection_speed.yml @@ -0,0 +1,2 @@ +minor_changes: +- "pacman - Improve package state detection speed: Don't query for full details of a package." diff --git a/plugins/modules/packaging/os/pacman.py b/plugins/modules/packaging/os/pacman.py index 14995a2216..b6498d5269 100644 --- a/plugins/modules/packaging/os/pacman.py +++ b/plugins/modules/packaging/os/pacman.py @@ -153,20 +153,18 @@ from ansible.module_utils.basic import AnsibleModule def get_version(pacman_output): - """Take pacman -Qi or pacman -Si output and get the Version""" - lines = pacman_output.split('\n') - for line in lines: - if line.startswith('Version '): - return line.split(':')[1].strip() + """Take pacman -Q or pacman -S output and get the Version""" + fields = pacman_output.split() + if len(fields) == 2: + return fields[1] return None def get_name(module, pacman_output): - """Take pacman -Qi or pacman -Si output and get the package name""" - lines = pacman_output.split('\n') - for line in lines: - if line.startswith('Name '): - return line.split(':')[1].strip() + """Take pacman -Q or pacman -S output and get the package name""" + fields = pacman_output.split() + if len(fields) == 2: + return fields[0] module.fail_json(msg="get_name: fail to retrieve package name from pacman output") @@ -175,7 +173,7 @@ def query_package(module, pacman_path, name, state="present"): boolean to indicate if the package is up-to-date and a third boolean to indicate whether online information were available """ if state == "present": - lcmd = "%s --query --info %s" % (pacman_path, name) + lcmd = "%s --query %s" % (pacman_path, name) lrc, lstdout, lstderr = module.run_command(lcmd, check_rc=False) if lrc != 0: # package is not installed locally @@ -190,7 +188,7 @@ def query_package(module, pacman_path, name, state="present"): # get the version installed locally (if any) lversion = get_version(lstdout) - rcmd = "%s --sync --info %s" % (pacman_path, name) + rcmd = "%s --sync --print-format \"%%n %%v\" %s" % (pacman_path, name) rrc, rstdout, rstderr = module.run_command(rcmd, check_rc=False) # get the version in the repository rversion = get_version(rstdout)