mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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.
This commit is contained in:
parent
91d4d9d8c6
commit
43c805f7db
2 changed files with 12 additions and 12 deletions
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- "pacman - Improve package state detection speed: Don't query for full details of a package."
|
|
@ -153,20 +153,18 @@ from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
|
||||||
def get_version(pacman_output):
|
def get_version(pacman_output):
|
||||||
"""Take pacman -Qi or pacman -Si output and get the Version"""
|
"""Take pacman -Q or pacman -S output and get the Version"""
|
||||||
lines = pacman_output.split('\n')
|
fields = pacman_output.split()
|
||||||
for line in lines:
|
if len(fields) == 2:
|
||||||
if line.startswith('Version '):
|
return fields[1]
|
||||||
return line.split(':')[1].strip()
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_name(module, pacman_output):
|
def get_name(module, pacman_output):
|
||||||
"""Take pacman -Qi or pacman -Si output and get the package name"""
|
"""Take pacman -Q or pacman -S output and get the package name"""
|
||||||
lines = pacman_output.split('\n')
|
fields = pacman_output.split()
|
||||||
for line in lines:
|
if len(fields) == 2:
|
||||||
if line.startswith('Name '):
|
return fields[0]
|
||||||
return line.split(':')[1].strip()
|
|
||||||
module.fail_json(msg="get_name: fail to retrieve package name from pacman output")
|
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
|
boolean to indicate if the package is up-to-date and a third boolean to indicate whether online information were available
|
||||||
"""
|
"""
|
||||||
if state == "present":
|
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)
|
lrc, lstdout, lstderr = module.run_command(lcmd, check_rc=False)
|
||||||
if lrc != 0:
|
if lrc != 0:
|
||||||
# package is not installed locally
|
# 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)
|
# get the version installed locally (if any)
|
||||||
lversion = get_version(lstdout)
|
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)
|
rrc, rstdout, rstderr = module.run_command(rcmd, check_rc=False)
|
||||||
# get the version in the repository
|
# get the version in the repository
|
||||||
rversion = get_version(rstdout)
|
rversion = get_version(rstdout)
|
||||||
|
|
Loading…
Reference in a new issue