Guest

Milton Paiva Neto

By: a guest on Apr 17th, 2009  |  syntax: Python  |  size: 5.19 KB  |  hits: 591  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. # Copyright 2007, Red Hat, Inc
  2. # Michael DeHaan <mdehaan@redhat.com>
  3. # Copyright 2009
  4. # Milton Paiva Neto <milton.paiva@gmail.com>
  5. #
  6. # This software may be freely redistributed under the terms of the GNU
  7. # general public license.
  8. #
  9. # You should have received a copy of the GNU General Public License
  10. # along with this program; if not, write to the Free Software
  11. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  12.  
  13. import func_module
  14.  
  15. class RpmModule(func_module.FuncModule):
  16.  
  17.     version = "0.0.1"
  18.     api_version = "0.0.1"
  19.     description = "RPM related commands."
  20.  
  21.     def inventory(self, flatten=True):
  22.         """
  23.        Returns information on all installed packages.
  24.        By default, 'flatten' is passed in as True, which makes printouts very
  25.        clean in diffs for use by func-inventory.  If you are writting another
  26.        software application, using flatten=False will prevent the need to
  27.        parse the returns.
  28.        """
  29.         # I have not been able to get flatten=False to work if there
  30.         # is more than 491 entries in the dict -- ashcrow
  31.         import rpm
  32.         ts = rpm.TransactionSet()
  33.         mi = ts.dbMatch()
  34.         results = []
  35.         for hdr in mi:
  36.             name = hdr['name']
  37.             epoch = (hdr['epoch'] or 0)
  38.             version = hdr['version']
  39.             release = hdr['release']
  40.             arch = hdr['arch']
  41.             if flatten:
  42.                 results.append("%s %s %s %s %s" % (name, epoch, version,
  43.                                                    release, arch))
  44.             else:
  45.                 results.append([name, epoch, version, release, arch])
  46.         return results
  47.  
  48.     def verify(self, pattern='', flatten=True):
  49.         """
  50.        Returns information on the verified package(s).
  51.        """        
  52.         import rpm
  53.         import yum
  54.         from re import split
  55.         ts = rpm.TransactionSet()
  56.         mi = (ts.dbMatch() if pattern == '' else self.glob(pattern))
  57.         results = []
  58.         for hdr in mi:
  59.             name = hdr['name'] if pattern == '' else split("\s",hdr)[0]
  60.             if flatten:                
  61.                 yb = yum.YumBase()
  62.                 pkgs = yb.rpmdb.searchNevra(name)
  63.                 for pkg in pkgs:
  64.                     errors = pkg.verify()
  65.                     for fn in errors.keys():
  66.                         for prob in errors[fn]:
  67.                             results.append('%s %s %s' % (name, fn, prob.message))
  68.             else:
  69.                 results.append("%s-%s-%s.%s" % (name, version, release, arch))
  70.         return results
  71.  
  72.     def glob(self, pattern, flatten=True):
  73.         """
  74.        Return a list of installed packages that match a pattern
  75.        """
  76.         import rpm
  77.         ts = rpm.TransactionSet()
  78.         mi = ts.dbMatch()
  79.         results = []
  80.         if not mi:
  81.             return
  82.         mi.pattern('name', rpm.RPMMIRE_GLOB, pattern)
  83.         for hdr in mi:
  84.             name = hdr['name']
  85.             epoch = (hdr['epoch'] or 0)
  86.             version = hdr['version']
  87.             release = hdr['release']
  88.             # gpg-pubkeys have no arch
  89.             arch = (hdr['arch'] or "")
  90.  
  91.             if flatten:
  92.                 results.append("%s %s %s %s %s" % (name, epoch, version,
  93.                                                        release, arch))
  94.             else:
  95.                 results.append([name, epoch, version, release, arch])
  96.         return results
  97.  
  98.     def register_method_args(self):
  99.         """
  100.        Implementing the method argument getter
  101.        """
  102.         return {
  103.                 'inventory':{
  104.                     'args':{
  105.                         'flatten':{
  106.                             'type':'boolean',
  107.                             'optional':True,
  108.                             'default':True,
  109.                             'description':"Print clean in difss"
  110.                             }
  111.                         },
  112.                     'description':"Returns information on all installed packages"
  113.                     },
  114.                 'verify':{
  115.                     'args':{
  116.                         'flatten':{
  117.                             'type':'boolean',
  118.                             'optional':True,
  119.                             'default':True,
  120.                             'description':"Print clean in difss"
  121.                             }
  122.                         },
  123.                     'description':"Returns information on the verified package(s)"
  124.                     },
  125.                 'glob':{
  126.                     'args':{
  127.                         'pattern':{
  128.                             'type':'string',
  129.                             'optional':False,
  130.                             'description':"The glob packet pattern"
  131.                             },
  132.                         'flatten':{
  133.                             'type':'boolean',
  134.                             'optional':True,
  135.                             'default':True,
  136.                             'description':"Print clean in difss"
  137.                                 }
  138.                         },
  139.                     'description':"Return a list of installed packages that match a pattern"
  140.                     }
  141.                 }