#!/usr/bin/python -tt # # Copyright 2009 Red Hat # written 2009 - Seth Vidal # # 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. # # simple wrapper around yum # arguments: # --install pkg, --update pkg, --remove pkg # --dry-run # --list-updates # --enablerepo, --disablerepo # -c config # exit codes to think about: # 1 - you're an idiot # 0 - normal complete # 2 - moral failure/completely botched # 3 - transaction failure # 4 - partial transaction failure # 5 - config/setup failure (repoerror, configerror, urlgrabber error, etc # 6 - non-fatal error of some kind # 7 - usercancel/exit # 200 - can't unlock # normal import sys sys.path.insert(0,'/usr/share/yum-cli') from yum.misc import setup_locale from yum import Errors, plugins from utils import YumUtilBase import logging import os import re from rpmUtils import miscutils, arch from optparse import OptionGroup def pkg_obj_list_output(pkg): if pkg.repoid == 'installed' and 'from_repo' in pkg.yumdb_info: rid = '@' + pkg.yumdb_info.from_repo else: rid = pkg.repoid return '%s-%s-%s.%s|%s' % (pkg.name, pkg.ver, pkg.rel, pkg.arch, rid) class YumPipeable(YumUtilBase): NAME = 'YumPipeable' VERSION = '1.0' USAGE = """ YumPipeable: a pipeable interface to yum complete with return codes for all the shell and perl junkies out there unwilling to use python. helps find problems in the rpmdb of system and correct them """ def __init__(self): YumUtilBase.__init__(self, YumPipeable.NAME, YumPipeable.VERSION, YumPipeable.USAGE) self.logger = logging.getLogger("yum.verbose.cli.pipeable") # Add util commandline options to the yum-cli ones self.optparser = self.getOptionParser() self.optparser_grp = self.getOptionGroup() self.addCmdOptions() try: self.main() except Errors.YumBaseError, e: print >> sys.stderr, 'Error: %s' % str(e) sys.exit(2) def addCmdOptions(self): self.optparser_grp.add_option("--update", default=[], dest="update", action="append", help='add a pkg name/glob to be updated') self.optparser_grp.add_option("--install", default=[], dest="install", action="append", help='add a pkg name/glob to be installed') self.optparser_grp.add_option("--remove", default=[], dest="remove", action="append", help='add a pkg name/glob to be removed') self.optparser_grp.add_option("--dry-run", default=False, dest="dryrun", action="store_true", help='Do not perform any action, just print out what it would do') self.optparser_grp.add_option("--list", dest="list", default=None, help='List pkgs [updates|installed|available]') self.optparser_grp.add_option("--report-only", dest="report", default=False, action="store_true", help="only report what the transaction would do") def doPipeTransaction(self): def exUserCancel(): self.logger.critical(_('\n\nExiting on user cancel')) if unlock(): return 200 return 7 def exIOError(e): if e.errno == 32: self.logger.critical(_('\n\nExiting on Broken Pipe')) else: self.logger.critical(_('\n\n%s') % str(e)) if unlock(): return 200 return 1 def exPluginExit(e): '''Called when a plugin raises PluginYumExit. Log the plugin's exit message if one was supplied. ''' # ' xemacs hack exitmsg = str(e) if exitmsg: self.logger.warn('\n\n%s', exitmsg) if unlock(): return 200 return 2 def exFatal(e): self.logger.critical('\n\n%s', to_unicode(e.value)) if unlock(): return 200 return 2 def unlock(): try: self.closeRpmDB() self.doUnlock() except Errors.LockError, e: return 200 return 0 try: return_code = self.doTransaction() except plugins.PluginYumExit, e: return exPluginExit(e) except Errors.YumBaseError, e: return exFatal(e) except KeyboardInterrupt: return exUserCancel() except IOError, e: return exIOError(e) if unlock(): return 200 return return_code def listTransaction(self): """returns a string rep of the transaction""" self.tsInfo.makelists(True, True) obsoleted = [] results = [] for (action, pkglist) in [('install', self.tsInfo.installed), ('update', self.tsInfo.updated), ('remove', self.tsInfo.removed), ('reinstall', self.tsInfo.reinstalled), ('downgrade', self.tsInfo.downgraded), ('install-deps', self.tsInfo.depinstalled), ('update-deps', self.tsInfo.depupdated), ('remove-deps', self.tsInfo.depremoved)]: for txmbr in pkglist: obsoleted.extend(txmbr.obsoletes) out = '%s|%s\n' % (action, pkg_obj_list_output(txmbr.po)) results.append(out) for (action, pkglist) in [('obsoleted', obsoleted), ('skipped', self.skipped_packages),]: lines = [] for po in pkglist: out = '%s|%s\n' % (action, pkg_obj_list_output(po)) results.append(out) return ''.join(results) def main(self): opts = self.doUtilConfigSetup() self.conf.debuglevel=1 if opts.dryrun: self.conf.tsflags.append('test') if not opts.update and not opts.install and not opts.remove and not opts.list: self.optparser.print_help() sys.exit(1) if opts.list: ypl = self.doPackageLists(pkgnarrow=opts.list) if opts.list == 'updates': thislist = ypl.updates elif opts.list == 'installed': thislist = ypl.installed elif opts.list == 'available': thislist = ypl.available elif opts.list == 'obsoletes': thislist = ypl.obsoletes for pkg in sorted(thislist): print pkg_obj_list_output(pkg) sys.exit(0) for item in opts.install: self.install(pattern=item) for item in opts.update: if item == 'all': self.update() else: self.update(pattern=item) for item in opts.remove: self.remove(pattern=item) if os.getuid() != 0: self.logger.critical('\n\nMust be root to perform transactions') sys.exit(1) self.buildTransaction() if len(self.tsInfo) < 1: self.logger.warn('\n\nNothing to do') sys.exit(0) if opts.report: print self.listTransaction() sys.exit(0) sys.exit(self.doPipeTransaction()) if __name__ == '__main__': setup_locale() util = YumPipeable()