Package up2date_client :: Module rpmUtils
[hide private]
[frames] | no frames]

Source Code for Module up2date_client.rpmUtils

  1  # some high level utility stuff for rpm handling 
  2   
  3  # Client code for Update Agent 
  4  # Copyright (c) 1999--2016 Red Hat, Inc.  Distributed under GPLv2. 
  5  # 
  6  # Author: Preston Brown <pbrown@redhat.com> 
  7  #         Adrian Likins <alikins@redhat.com> 
  8  # 
  9   
 10   
 11  # 
 12  #  FIXME: Some exceptions in here are currently in up2date.py 
 13  #         fix by moving to up2dateErrors.py and importing from there 
 14  # 
 15  # 
 16   
 17  import os 
 18  import rpm 
 19  from rhn.i18n import sstr 
 20  from up2date_client import transaction 
 21   
 22  import gettext 
 23  t = gettext.translation('rhn-client-tools', fallback=True) 
 24  # Python 3 translations don't have a ugettext method 
 25  if not hasattr(t, 'ugettext'): 
 26      t.ugettext = t.gettext 
 27  _ = t.ugettext 
 28   
29 -def installedHeaderByKeyword(**kwargs):
30 """ just cause this is such a potentially useful looking method... """ 31 _ts = transaction.initReadOnlyTransaction() 32 mi = _ts.dbMatch() 33 for keyword in kwargs.keys(): 34 mi.pattern(keyword, rpm.RPMMIRE_GLOB, kwargs[keyword]) 35 # we really shouldnt be getting multiples here, but what the heck 36 headerList = [] 37 for h in mi: 38 headerList.append(h) 39 40 return headerList
41
42 -def verifyPackages(packages):
43 """ given a list of package labels, run rpm -V on them 44 and return a dict keyed off that data 45 """ 46 data = {} 47 missing_packages = [] 48 # data structure is keyed off package 49 # label, with value being an array of each 50 # line of the output from -V 51 52 53 retlist = [] 54 for package in packages: 55 # we have to have at least name... 56 57 # Note: we cant reliable match on epoch, so just 58 # skip it... two packages that only diff by epoch is 59 # way broken anyway 60 keywords = {'name': package[0], 61 'version': package[1], 62 'release': package[2], 63 # we left our epoch 64 'arch': package[4] 65 } 66 for key in (keywords.keys()): 67 if (keywords[key] == None) or (keywords[key] == ""): 68 del(keywords[key]) 69 70 headers = installedHeaderByKeyword(**keywords) 71 if len(headers) == 0: 72 missing_packages.append(package) 73 74 for header in headers: 75 epoch = header['epoch'] 76 if epoch == None: 77 epoch = "" 78 # gpg-pubkey "packages" can have an arch of None, see bz #162701 79 arch = header["arch"] 80 if arch == None: 81 arch = "" 82 83 pkg = (header['name'], header['version'], 84 header['release'], epoch, 85 arch) 86 87 # dont include arch in the label if it's a None arch, #162701 88 if header["arch"] == "": 89 packageLabel = "%s-%s-%s" % (pkg[0], pkg[1], pkg[2]) 90 else: 91 packageLabel = "%s-%s-%s.%s" % (pkg[0], pkg[1], pkg[2], pkg[4]) 92 93 verifystring = "/usr/bin/rpmverify -V %s" % packageLabel 94 95 fd = os.popen(verifystring) 96 res = fd.readlines() 97 fd.close() 98 99 reslist = [] 100 for line in res: 101 reslist.append(line.strip()) 102 retlist.append([pkg, reslist]) 103 104 return retlist, missing_packages
105 106 #FIXME: this looks like a good candidate for caching, since it takes a second 107 # or two to run, and I can call it a couple of times
108 -def getInstalledPackageList(msgCallback = None, progressCallback = None, 109 getArch=None, getInfo = None):
110 """ Return list of packages. Package is hash with keys name, epoch, 111 version, release and optionaly arch and cookie 112 """ 113 pkg_list = [] 114 115 if msgCallback != None: 116 msgCallback(_("Getting list of packages installed on the system")) 117 118 _ts = transaction.initReadOnlyTransaction() 119 count = 0 120 total = 0 121 122 for h in _ts.dbMatch(): 123 if h == None: 124 break 125 count = count + 1 126 127 total = count 128 129 count = 0 130 for h in _ts.dbMatch(): 131 if h == None: 132 break 133 package = { 134 'name': sstr(h['name']), 135 'epoch': h['epoch'], 136 'version': sstr(h['version']), 137 'release': sstr(h['release']), 138 'installtime': h['installtime'] 139 } 140 if package['epoch'] == None: 141 package['epoch'] = "" 142 else: # convert it to string 143 package['epoch'] = "%s" % package['epoch'] 144 if getArch: 145 package['arch'] = h['arch'] 146 # the arch on gpg-pubkeys is "None"... 147 if package['arch']: 148 package['arch'] = sstr(package['arch']) 149 pkg_list.append(package) 150 elif getInfo: 151 if h['arch']: 152 package['arch'] = sstr(h['arch']) 153 if h['cookie']: 154 package['cookie'] = sstr(h['cookie']) 155 pkg_list.append(package) 156 else: 157 pkg_list.append(package) 158 159 if progressCallback != None: 160 progressCallback(count, total) 161 count = count + 1 162 163 pkg_list.sort(key=lambda x:(x['name'], x['epoch'], x['version'], x['release'])) 164 return pkg_list
165
166 -def setDebugVerbosity():
167 """Set rpm's verbosity mode 168 """ 169 try: 170 rpm.setVerbosity(rpm.RPMLOG_DEBUG) 171 except AttributeError: 172 print("extra verbosity not supported in this version of rpm")
173