Subdomain Posts
Python | 3 days ago
Python | 3 days ago
SQL | 3 days ago
None | 11 days ago
None | 19 days ago
None | 29 days ago
None | 30 days ago
Python | 337 days ago
Python | 347 days ago
Python | 352 days ago
Recent Posts
None | 3 sec ago
None | 37 sec ago
None | 50 sec ago
None | 1 min ago
None | 1 min ago
JavaScript | 2 min ago
C | 3 min ago
None | 3 min ago
None | 3 min ago
None | 4 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Milton Paiva Neto on the 7th of Apr 2009 07:00:12 PM Download | Raw | Embed | Report
  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. import func_module
  13. import rpm
  14. import yum
  15. from re import split
  16.  
  17. class RpmModule(func_module.FuncModule):
  18.     version = "0.0.1"
  19.     api_version = "0.0.1"
  20.     description = "RPM related commands."
  21.  
  22.     def inventory(self, flatten=True):
  23.         """
  24.        Returns information on all installed packages.
  25.        By default, 'flatten' is passed in as True, which makes printouts very
  26.        clean in diffs for use by func-inventory.  If you are writting another
  27.        software application, using flatten=False will prevent the need to
  28.        parse the returns.
  29.        """
  30.         # I have not been able to get flatten=False to work if there
  31.         # is more than 491 entries in the dict -- ashcrow
  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 of the verification of all installed packages.
  51.        """
  52.         print ""
  53.         #For some reason, if this print is removed, the previous one does not happen until the lines after this have been evaluated. - Greg.
  54.         ts = rpm.TransactionSet()
  55.         mi = (ts.dbMatch() if pattern == '' else self.glob(pattern))
  56.         results = []
  57.         for hdr in mi:
  58.             name = hdr['name'] if pattern == '' else split("\s",hdr)[0]
  59.             if flatten:    
  60.                 yb = yum.YumBase()
  61.                 pkgs = yb.rpmdb.searchNevra(name)
  62.                 for pkg in pkgs:
  63.                     errors = pkg.verify()
  64.                     for fn in errors.keys():
  65.                         for prob in errors[fn]:
  66.                             results.append('%s %s %s' % (name, fn, prob.message))
  67.             else:
  68.                 results.append("%s-%s-%s.%s" % (name, version, release, arch))
  69.         return results
  70.  
  71.     def glob(self, pattern, flatten=True):
  72.         """
  73.        Return a list of installed packages that match a pattern
  74.        """
  75.         ts = rpm.TransactionSet()
  76.         mi = ts.dbMatch()
  77.         results = []
  78.         if not mi:
  79.             return
  80.         mi.pattern('name', rpm.RPMMIRE_GLOB, pattern)
  81.         for hdr in mi:
  82.             name = hdr['name']
  83.             epoch = (hdr['epoch'] or 0)
  84.             version = hdr['version']
  85.             release = hdr['release']
  86.             # gpg-pubkeys have no arch
  87.             arch = (hdr['arch'] or "")
  88.  
  89.             if flatten:
  90.                 results.append("%s %s %s %s %s" % (name, epoch, version,
  91.                                                        release, arch))
  92.             else:
  93.                 results.append([name, epoch, version, release, arch])
  94.         return results
  95.  
  96.     def register_method_args(self):
  97.         """
  98.        Implementing the method argument getter
  99.        """
  100.  
  101.         return {
  102.                 'inventory':{
  103.                     'args':{
  104.                         'flatten':{
  105.                             'type':'boolean',
  106.                             'optional':True,
  107.                             'default':True,
  108.                             'description':"Print clean in difss"
  109.                             }
  110.                         },
  111.                     'description':"Returns information on all installed packages"
  112.                     },
  113.                 'verify':{
  114.                     'args':{
  115.                         'flatten':{
  116.                             'type':'boolean',
  117.                             'optional':True,
  118.                             'default':True,
  119.                             'description':"Print clean in difss"
  120.                             }
  121.                         },
  122.                     'description':"Returns information of the verification on all installed packages"
  123.                     },
  124.                 'glob':{
  125.                     'args':{
  126.                         'pattern':{
  127.                             'type':'string',
  128.                             'optional':False,
  129.                             'description':"The glob packet pattern"
  130.                             },
  131.                         'flatten':{
  132.                             'type':'boolean',
  133.                             'optional':True,
  134.                             'default':True,
  135.                             'description':"Print clean in difss"
  136.                                 }
  137.                         },
  138.                     'description':"Return a list of installed packages that match a pattern"
  139.                     }
  140.                 }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: