#!/usr/bin/python # 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 # take list of pkgs # resolve deps import sys, os import yum # this may well be hurky but it does keep us out of some mess :) # and we should do this with __version_info__ eventually if yum.__version__ < '3.2.17': print >> sys.stderr, "Need yum version 3.2.18 or higher" sys.exit(1) from urlgrabber.progress import format_number import pykickstart import pykickstart.parser import pykickstart.constants from optparse import OptionParser def argparse(): parser = OptionParser() parser.usage = "installed_size [options] ks.cfg" parser.add_option("-q", "--quiet", action="store_true", default=False) parser.add_option("-c","--conf", default=None) opts, args = parser.parse_args() if len(args) < 1: parser.print_usage() print 'You must give a kickstart config to process' sys.exit(1) return opts, args def handle_kickstart(filename): myparser = pykickstart.parser.KickstartParser(pykickstart.version.makeVersion()) myparser.readKickstart(filename) return myparser def do_installed_size_stats(opts, kscfg): myks = handle_kickstart(kscfg) my = yum.YumBase() debuglevel = None if opts.quiet: debuglevel = 0 if opts.conf: my._getConfig(fn=opts.conf, debuglevel=debuglevel) else: my._getConfig(debuglevel=debuglevel) my.repos.disableRepo('*') # disable all existing repos - just in case my.conf.exclude = myks.handler.packages.excludedList my.conf.installroot=yum.misc.getCacheDir() my.conf.cachedir=yum.misc.getCacheDir() # end bit needed so we don't step on repos randomly random_name = yum.misc.getCacheDir().split('-')[-1] # setup repos here for repo in myks.handler.repo.repoList: rand_repo_name = repo.name + '-' + random_name # new way in yum 3.2.18 and beyond my.add_enable_repo(rand_repo_name, baseurls=[repo.baseurl], mirrorlist=repo.mirrorlist) my.repos.setCacheDir(my.conf.cachedir) # group installs groups = myks.handler.packages.groupList group_names = [ group.name.lower() for group in groups ] if 'base' not in group_names and myks.handler.packages.addBase: groups.append(pykickstart.parser.Group(name='base')) if 'core' not in group_names: groups.append(pykickstart.parser.Group(name='core')) for group in groups: # get group back from my.comps.whatever for thisgroup in my.comps.return_groups(group.name): if group.include == pykickstart.constants.GROUP_ALL: pkg_types = ['mandatory', 'optional', 'default'] elif group.include == pykickstart.constants.GROUP_DEFAULT: # normal groups pkg_types = ['mandatory', 'default'] elif group.include == pykickstart.constants.GROUP_REQUIRED: pkg_types = ['mandatory'] try: my.selectGroup(group.name, group_package_types=pkg_types) except yum.Errors.GroupsError, e: if not opts.quiet: print >> sys.stderr, '%s group error: %s' % (thisgroup.groupid, str(e)) # package installs for pkgname in myks.handler.packages.packageList: try: my.install(pattern=pkgname) except yum.Errors.InstallError, e: if not opts.quiet: print >> sys.stderr, 'Package name: %s - %s' % (pkgname, str(e)) # resolve the deps my.resolveDeps() pkgs = [] for pkg in my.tsInfo: pkgs.append(pkg.po) return pkgs def main(): opts, args = argparse() pkgs = do_installed_size_stats(opts, args[0]) numpkgs = len(pkgs) if numpkgs == 0: print >> sys.stderr, 'No packages in chroot, nothing to do' sys.exit(1) for pkg in pkgs: print '%s\t%s.%s' % (pkg.size_archive, pkg.name, pkg.arch) if __name__ == '__main__': main()