#!/usr/bin/python -tt import sys sys.path.insert(0,'/usr/share/yum-cli') import cli def findDupes(my): """takes a yum base object prints out a list of package duplicates. These typically happen when an update transaction is left half-completed""" # iterate rpmdb.pkglist # put each package into name.arch dicts with lists as the po # look for any keys with a > 1 length list of pos where the name # of the package is not kernel and/or does not provide a kernel-module pkgdict = {} refined = {} dupes = [] for (n,a,e,v,r) in my.rpmdb.simplePkgList(): if not pkgdict.has_key((n,a)): pkgdict[(n,a)] = [] pkgdict[(n,a)].append((e,v,r)) for (n,a) in pkgdict.keys(): if len(pkgdict[(n,a)]) > 1: refined[(n,a)] = pkgdict[(n,a)] del pkgdict return refined def setupOldDupes(my): """remove all the older duplicates""" dupedict = findDupes(my) removedupes = [] for (n,a) in dupedict.keys(): if n.startswith('kernel'): continue if n.startswith('gpg-pubkey'): continue (e,v,r) = dupedict[(n,a)][0] lowpo = my.getInstalledPackageObject((n,a,e,v,r)) for (e,v,r) in dupedict[(n,a)][1:]: po = my.getInstalledPackageObject((n,a,e,v,r)) if po.EVR < lowpo.EVR: lowpo = po removedupes.append(lowpo) for po in removedupes: my.remove(po) return len(removedupes) def main(): my = cli.YumBaseCli() my.doGenericSetup() #something here to set -y or not num = setupOldDupes(my) if num < 1: print 'no dupes to remove' sys.exit(0) my.buildTransaction() my.doTransaction() sys.exit(0) if __name__ == "__main__": main()