#!/usr/bin/python -tt # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # copyright (c) 2008 Red Hat, Inc - written by Seth Vidal """Go through a yum packagesack and return the list of newest pkgs which possibly have conflicting files""" import yum import yum.Errors import yum.misc import sys import os import os.path from optparse import OptionParser # iterate all pkgs, build a filedict of filename = [list of pkg objects] # then go through that list, if any file has more than one pkg then put those # pkgs in another list # print out the file conflict and the pkgs which potentially conflict # later: work on whittling down the list by removing: # pkgs which are multilib sets # pkgs which have fully matching overlapping files (md5sums + timestamps) def parseArgs(): usage = """ Read in the metadata of a series of repositories and check all the dependencies in all packages for resolution. Print out the list of packages with unresolved dependencies %s [-c ] [-a ] [-r ] [-r ] """ % sys.argv[0] parser = OptionParser(usage=usage) parser.add_option("-c", "--config", default='/etc/yum.conf', help='config file to use (defaults to /etc/yum.conf)') parser.add_option("-a", "--arch", default=[], action='append', help='check packages of the given archs, can be specified multiple ' + 'times (default: current arch)') parser.add_option("-b", "--builddeps", default=False, action="store_true", help='check build dependencies only (needs source repos enabled)') parser.add_option("-r", "--repoid", default=[], action='append', help="specify repo ids to query, can be specified multiple times (default is all enabled)") parser.add_option("-t", "--tempcache", default=False, action="store_true", help="Use a temp dir for storing/accessing yum-cache") parser.add_option("-q", "--quiet", default=0, action="store_true", help="quiet (no output to stderr)") parser.add_option("-n", "--newest", default=0, action="store_true", help="check only the newest packages in the repos") parser.add_option("--repofrompath", action="append", help="specify repoid & paths of additional repositories - unique repoid and complete path required, can be specified multiple times. Example. --repofrompath=myrepo,/path/to/repo") (opts, args) = parser.parse_args() return (opts, args) def pkg_names_match(pkglist): p = pkglist[0] for pkg in pkglist[1:]: if p.name != pkg.name: return False return True def sourcerpms_match(pkglist): p = pkglist[0] for pkg in pkglist[1:]: if p.sourcerpm != pkg.sourcerpm: return False return True def obsolete_each_other(pkglist): for pkga in pkglist: for pkgb in pkglist: if pkga == pkgb: continue if pkga.checkPrco('obsoletes', (pkgb.name, '=', (pkgb.epoch, pkgb.ver, pkgb.rel))): return True if pkgb.checkPrco('obsoletes', (pkga.name, '=', (pkga.epoch, pkga.ver, pkga.rel))): return True return False def is_real_conflict(fn, pkglist): checksums = {} # not a great check but it does toss out all the glibc.i686 vs glibc.i386 ones if pkg_names_match(pkglist): return False # if they have the same sourcerpm it is highly doubtful it is a legit conflict if sourcerpms_match(pkglist): return False if obsolete_each_other(pkglist): #print "obsolete each other" return False for pkg in pkglist: #print "Fetching Headers for %s" % pkg #download header # silliness for headers if not os.path.exists(pkg.repo.hdrdir): os.makedirs(pkg.repo.hdrdir) # make this into a proper error func check for urlgrabber routines # so we get the proper retries if not os.path.exists(pkg.localHdr()): try: hdr = pkg.returnLocalHeader() except yum.Errors.RepoError, e: pkg.repo.getHeader(pkg) else: pkg.repo.getHeader(pkg) #read in header try: hdr = pkg.returnLocalHeader() except yum.Errors.RepoError, e: #print "Cannot retrieve header for %s" % pkg return True fi = hdr.fiFromHeader() for fitup in fi: if fitup[0] == fn: checksums[pkg]=fitup[-1] l = yum.misc.unique(checksums.values()) if len(l) > 1: return True return False (opts, cruft) = parseArgs() my = yum.YumBase() my.doConfigSetup(fn = opts.config,init_plugins=False) my.conf.cachedir = yum.misc.getCacheDir() my.repos.setCacheDir(my.conf.cachedir) if opts.repofrompath: # setup the fake repos for repo in opts.repofrompath: repoid,repopath = tuple(repo.split(',')) if repopath[0] == '/': baseurl = 'file://' + repopath else: baseurl = repopath repopath = os.path.normpath(repopath) newrepo = yum.yumRepo.YumRepository(repoid) newrepo.name = repopath newrepo.baseurl = baseurl newrepo.basecachedir = my.conf.cachedir newrepo.metadata_expire = 0 newrepo.timestamp_check = False my.repos.add(newrepo) my.repos.enableRepo(newrepo.id) my.logger.info( "Added %s repo from %s" % (repoid,repopath)) if opts.repoid: for repo in my.repos.repos.values(): if repo.id not in opts.repoid: repo.disable() else: repo.enable() if os.geteuid() != 0 or opts.tempcache: cachedir = yum.misc.getCacheDir() if cachedir is None: my.logger.error("Error: Could not make cachedir, exiting") sys.exit(50) my.repos.setCacheDir(cachedir) filedict = {} #dirdict = {} print 'Finding potential file conflicts in:' for repo in my.repos.listEnabled(): print repo.urls[0] for pkg in my.pkgSack.returnNewestByNameArch(): for fn in pkg.filelist + pkg.ghostlist: if not filedict.has_key(fn): filedict[fn] = [] filedict[fn].append(pkg) # for fn in pkg.dirlist: # if not dirdict.has_key(fn): dirdict[fn] = [] # dirdict[fn].append(pkg) for fn in filedict.keys(): if len(filedict[fn]) < 2: del filedict[fn] print 'Done' print '%s potential file conflicts' % len(filedict.keys()) print 'Doing more advanced checks to see if these are real conflicts (patience is a virtue)' count = 1 for fn in filedict.keys(): #print '%d checking on %s ...' % (count, fn), count += 1 if not is_real_conflict(fn, filedict[fn]): #print ' no.' del filedict[fn] #else: #print ' yes.' #print "conflicts: " #for pkg in filedict[fn]: #print ' %s-%s-%s.%s' % (pkg.name, pkg.ver, pkg.rel, pkg.arch) #print 'Done' print 'Results' for fn in filedict.keys(): print fn for pkg in filedict[fn]: print ' %s-%s-%s.%s' % (pkg.name, pkg.ver, pkg.rel, pkg.arch)