add failhard argument to various apt pkg functions

currently, it's not possible to query for info on packages that may not
be installed. with an added failhard argument, the user can specify if
they just want an empty result set back instead of an exception being
raised.
This commit is contained in:
Andreas Lutro 2017-02-15 14:18:01 +01:00
parent 99554d9d72
commit 246bf1e938
2 changed files with 20 additions and 6 deletions

View file

@ -2464,7 +2464,7 @@ def owner(*paths):
return ret
def info_installed(*names):
def info_installed(failhard=True, *names):
'''
Return the information of the named package(s) installed on the system.
@ -2473,15 +2473,20 @@ def info_installed(*names):
names
The names of the packages for which to return information.
failhard
Whether to throw an exception if none of the packages are installed.
Defaults to True.
CLI example:
.. code-block:: bash
salt '*' pkg.info_installed <package1>
salt '*' pkg.info_installed <package1> <package2> <package3> ...
salt '*' pkg.info_installed <package1> failhard=false
'''
ret = dict()
for pkg_name, pkg_nfo in __salt__['lowpkg.info'](*names).items():
for pkg_name, pkg_nfo in __salt__['lowpkg.info'](*names, failhard=failhard).items():
t_nfo = dict()
# Translate dpkg-specific keys to a common structure
for key, value in pkg_nfo.items():

View file

@ -248,12 +248,13 @@ def file_dict(*packages):
return {'errors': errors, 'packages': ret}
def _get_pkg_info(*packages):
def _get_pkg_info(failhard=True, *packages):
'''
Return list of package information. If 'packages' parameter is empty,
then data about all installed packages will be returned.
:param packages: Specified packages.
:param failhard: Throw an exception if no packages found.
:return:
'''
@ -286,7 +287,10 @@ def _get_pkg_info(*packages):
call = __salt__['cmd.run_all'](cmd, python_chell=False)
if call['retcode']:
raise CommandExecutionError("Error getting packages information: {0}".format(call['stderr']))
if failhard:
raise CommandExecutionError("Error getting packages information: {0}".format(call['stderr']))
else:
return ret
for pkg_info in [elm for elm in re.split(r"------", call['stdout']) if elm.strip()]:
pkg_data = {}
@ -369,7 +373,7 @@ def _get_pkg_ds_avail():
return ret
def info(*packages):
def info(failhard=True, *packages):
'''
Returns a detailed summary of package information for provided package names.
If no packages are specified, all packages will be returned.
@ -379,19 +383,24 @@ def info(*packages):
packages
The names of the packages for which to return information.
failhard
Whether to throw an exception if none of the packages are installed.
Defaults to True.
CLI example:
.. code-block:: bash
salt '*' lowpkg.info
salt '*' lowpkg.info apache2 bash
salt '*' lowpkg.info 'php5*' failhard=false
'''
# Get the missing information from the /var/lib/dpkg/available, if it is there.
# However, this file is operated by dselect which has to be installed.
dselect_pkg_avail = _get_pkg_ds_avail()
ret = dict()
for pkg in _get_pkg_info(*packages):
for pkg in _get_pkg_info(*packages, failhard=failhard):
# Merge extra information from the dselect, if available
for pkg_ext_k, pkg_ext_v in dselect_pkg_avail.get(pkg['package'], {}).items():
if pkg_ext_k not in pkg: