#!/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. # # by James Antill import yum from yum.plugins import TYPE_INTERACTIVE import sys import time from yum.misc import get_my_lang_code sys.path.insert(0, '/usr/share/yum-cli') from yumcommands import checkRootUID, checkGPGKey import cli requires_api_version = '2.1' plugin_type = (TYPE_INTERACTIVE,) def checkCatArg(base, basecmd, extcmds): if len(extcmds) == 0: base.logger.critical('Error: Need a category or list of categories') base.usage() raise cli.CliError def return_categories(base, pattern): return base.comps.return_categories(pattern) class CatCommand: def __init__(self, cmd): self.cmd = cmd def getNames(self): return [self.cmd] def getUsage(self): return "[%s] [categories]" % self.cmd def doCheck(self, base, basecmd, extcmds): pass class CatInstCommand(CatCommand): def __init__(self): CatCommand.__init__(self, "category-install") def getSummary(self): return "Installs all the groups in a category" def doCheck(self, base, basecmd, extcmds): checkRootUID(base) checkGPGKey(base) checkCatArg(base, basecmd, extcmds) def doCommand(self, base, basecmd, extcmds): pkgs_used = [] for cmd in extcmds: cats = base.comps.return_categories(cmd) if not cats: self.logger.error('Warning: Category %s does not exist.', cmd) for cat in cats: for grpname in cat.groups: try: txmbrs = base.selectGroup(grpname) except yum.Errors.GroupsError: continue else: pkgs_used.extend(txmbrs) if not pkgs_used: return 0, ['No packages to install or update'] else: return 2, ['%d Package(s) to Install' % len(pkgs_used)] class CatRmCommand(CatCommand): def __init__(self): CatCommand.__init__(self, "category-remove") def getSummary(self): return "Installs all the groups in a category" def doCheck(self, base, basecmd, extcmds): checkRootUID(base) checkCatArg(base, basecmd, extcmds) def doCommand(self, base, basecmd, extcmds): pkgs_used = [] for cmd in extcmds: cats = base.comps.return_categories(cmd) if not cats: self.logger.error('Warning: Category %s does not exist.', cmd) for cat in cats: for grpname in cat.groups: try: txmbrs = base.groupRemove(grpname) except yum.Errors.GroupsError: continue else: pkgs_used.extend(txmbrs) if not pkgs_used: return 0, ['No packages to remove'] else: return 2, ['%d Package(s) to Remove' % len(pkgs_used)] def needTs(self, base, basecmd, extcmds): return False def needTsRemove(self, base, basecmd, extcmds): return True def _cat_installed(base, cat): done = False for grpname in cat.groups: for grp in base.comps.return_groups(grpname): done = True if not grp.installed: return False return done def _cat_partial(base, cat): for grpname in cat.groups: for grp in base.comps.return_groups(grpname): if grp.installed: return True return False class CatListCommand(CatCommand): def __init__(self): CatCommand.__init__(self, "category-list") def getSummary(self): return "Lists the categories" def show_hdr(self, base, hdr): print base.fmtSection(hdr) def show_cat(self, base, cat): print " %s" % cat.ui_name def doCommand(self, base, basecmd, extcmds): if not extcmds: extcmds = ['*'] out = set() done = False for cmd in extcmds: cats = base.comps.return_categories(cmd) if not cats: self.logger.error('Warning: Category %s does not exist.', cmd) for cat in cats: if cat.categoryid in out: continue if _cat_installed(base, cat): if not done: done = True self.show_hdr(base, "Installed") out.add(cat.categoryid) self.show_cat(base, cat) done = False for cmd in extcmds: for cat in base.comps.return_categories(cmd): if cat.categoryid in out: continue if not _cat_partial(base, cat): if not done: done = True self.show_hdr(base, "Partially installed") out.add(cat.categoryid) self.show_cat(base, cat) done = False for cmd in extcmds: for cat in base.comps.return_categories(cmd): if cat.categoryid not in out: if not done: done = True self.show_hdr(base, "Available") out.add(cat.categoryid) self.show_cat(base, cat) return 0, ["%s done" % self.cmd] def needTs(self, base, basecmd, extcmds): return False class CatInfoCommand(CatListCommand): def __init__(self): CatCommand.__init__(self, "category-info") def getSummary(self): return "Lists the info. for the categories" def show_cat(self, base, cat): print "Category: %s" % cat.ui_name print " Description: %s" % cat.ui_description done = False for grpname in cat.groups: for grp in base.comps.return_groups(grpname): if not done: done = True print " Groups:" print " %s" % grp.ui_name def config_hook(conduit): conduit.registerCommand(CatInstCommand()) conduit.registerCommand(CatRmCommand()) conduit.registerCommand(CatListCommand()) conduit.registerCommand(CatInfoCommand())