ae26cb422bacb6817c55e3adb3d7dc563d907b23
[certmaster.git] / certmaster / minion / modules / rpms.py
1 # Copyright 2007, Red Hat, Inc
2 # Michael DeHaan <mdehaan@redhat.com>
3 #
4 # This software may be freely redistributed under the terms of the GNU
5 # general public license.
6 #
7 # You should have received a copy of the GNU General Public License
8 # along with this program; if not, write to the Free Software
9 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10
11 import func_module
12 import rpm
13
14 class RpmModule(func_module.FuncModule):
15
16 version = "0.0.1"
17 api_version = "0.0.1"
18 description = "RPM related commands."
19
20 def inventory(self, flatten=True):
21 """
22 Returns information on all installed packages.
23 By default, 'flatten' is passed in as True, which makes printouts very
24 clean in diffs for use by func-inventory. If you are writting another
25 software application, using flatten=False will prevent the need to
26 parse the returns.
27 """
28 # I have not been able to get flatten=False to work if there
29 # is more than 491 entries in the dict -- ashcrow
30 ts = rpm.TransactionSet()
31 mi = ts.dbMatch()
32 results = []
33 for hdr in mi:
34 name = hdr['name']
35 epoch = (hdr['epoch'] or 0)
36 version = hdr['version']
37 release = hdr['release']
38 arch = hdr['arch']
39 if flatten:
40 results.append("%s %s %s %s %s" % (name, epoch, version,
41 release, arch))
42 else:
43 results.append([name, epoch, version, release, arch])
44 return results