Skip to content

Commit

Permalink
Implement getting general packages information
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Maryniuk committed Sep 9, 2015
1 parent b94ee3c commit 1aa8a85
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions salt/modules/dpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Import python libs
import logging
import os
import re

# Import salt libs
import salt.utils
Expand Down Expand Up @@ -241,3 +242,46 @@ def file_dict(*packages):
files.append(line)
ret[pkg] = files
return {'errors': errors, 'packages': ret}


def _get_pkg_info(*packages):
'''
Return list of package informations. If 'packages' parameter is empty,
then data about all installed packages will be returned.
:param packages: Specified packages.
:return:
'''

ret = list()
cmd = "dpkg-query -W -f='package:${binary:Package}\\n" \
"revision:${binary:Revision}\\n" \
"architecture:${Architecture}\\n" \
"maintainer:${Maintainer}\\n" \
"summary:${Summary}\\n" \
"source:${source:Package}\\n" \
"version:${Version}\\n" \
"section:${Section}\\n" \
"size:${Installed-size}\\n" \
"origin:${Origin}\\n" \
"======\\n" \
"description:${Description}\\n" \
"------\\n'"
cmd += ' {0}'.format(' '.join(packages))
cmd = cmd.strip()

call = __salt__['cmd.run_all'](cmd, python_chell=False)
if call['retcode']:
raise CommandExecutionError("Error getting packages information: {0}".format(call['stderr']))

for pkg_info in [elm for elm in re.split(r"----*", call['stdout']) if elm.strip()]:
pkg_data = dict()
pkg_info, pkg_descr = re.split(r"====*", pkg_info)
for pkg_info_line in [el.strip() for el in pkg_info.split(os.linesep) if el.strip()]:
key, value = pkg_info_line.split(":", 1)
pkg_data[key] = value or "N/A"
pkg_data['description'] = pkg_descr.split(":", 1)[-1]
ret.append(pkg_data)

return ret

0 comments on commit 1aa8a85

Please sign in to comment.