Logo Search packages:      
Sourcecode: yum-utils version File versions  Download package

repo-check.py

#!/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.

import sys
sys.path.insert(0,'/usr/share/yum-cli')

import logging
from utils import YumUtilBase
from yum.misc import getCacheDir, setup_locale

import yum.Errors


00026 class UtilCheckBase:
    
    def __init__(self):
        pass

00031     def getNames(self):
        '''
        @return the names of the check used at the command line
        '''
        return []

00037     def getUsage(self):
        """
        @return: A usage string for the command, including arguments.
        """
        raise NotImplementedError

00043     def getSummary(self):
        """
        @return: A one line summary of what the command does.
        """
        raise NotImplementedError
    
00049     def doSetupParser(self, parser):
        '''
        Setup the check's parser options
        @param parser: a OptionParser instance
        '''
        pass

00056     def doPreSetup(self, base, args, opts):
        '''
        Setup the check before yum is setup
        @param base: yum base class
        @param args: command line args
        @param opts: command line options
        '''
        pass

00065     def runCheck(self, base,args, opts):
        '''
        Run the check
        @param base: yum base class
        @param args: command line args
        @param opts: command line options
        '''
        raise NotImplementedError
    
    
00075 class TestCheck(UtilCheckBase):
    
    def __init__(self):
        pass

00080     def getNames(self):
        '''
        @return the names of the check used at the command line
        '''
        return ['test']

00086     def getUsage(self):
        '''
        @return: A usage string for the command, including arguments.
        '''
        
        return 'test <package> [-all]'

00093     def getSummary(self):
        '''
        @return: A one line summary of what the command does.
        '''
        return 'Just a test check'
    
00099     def doSetupParser(self,  parser):
        '''
        Setup the tool and add tool cmd options
        @param parser: OptionParser instance so the tool can add options
        '''
        parser.add_option("--test", default="", dest="test",
          help='test option')

00107     def doPreSetup(self, base, args, opts):
        '''
        Setup the check before yum is setup
        @param base: yum base class
        @param args: command line args
        @param opts: command line options
        '''
        for repo in base.repos.findRepos('*-source'):
            base.logger.info("Sourcerepo : %s" % repo.id)

00117     def runCheck(self, base, args, opts):
        '''
        Run the check
        @param args: command line args
        @param opts: command line options
        '''
        if opts.test:
            base.logger.info('OPTION: --test=%s is used' % opts.test)
        for pkg in base.pkgSack:
            if pkg.name.startswith('yum'):
                print pkg


00130 class RepoCheck(YumUtilBase):
    NAME = 'repo-check'
    VERSION = '1.0'
    
    def __init__(self):
        self._checks = {}
        
        # Register checks
        self.registerCheck(TestCheck())
        
        # setup the base
        YumUtilBase.__init__(self,
                             RepoCheck.NAME,
                             RepoCheck.VERSION,
                             self._makeUsage())
        

        self.logger = logging.getLogger("yum.verbose.cli.repo-check")  
        
        
        # get the parser
        self.optparser = self.getOptionParser()
        self.main()
        
    def do_parser_setup(self):    
        if hasattr(self,'getOptionGroup'): # check if the group option API is available
            parser = self.getOptionGroup()
        else:
            parser = self.optparser
            
        # Call the checks parser setup methods 
        for check in self._checks.values():
            check.doSetupParser(parser)
            
        
    def main(self):
        self.do_parser_setup()
        try:
            opts = self.doUtilConfigSetup()
        except yum.Errors.RepoError, e:
            self.logger.error(str(e))
            sys.exit(50)

        # Check if there is anything to do.
        if len(self.cmds) < 1: 
            print self.optparser.format_help()
            sys.exit(0)

        if not self.cmds[0] in self._checks:
            print "\nUnknown Command : %s \n" % self.cmds[0]
            print self.optparser.format_help()
            sys.exit(0)
                
        # Make it work as non root
        if self.conf.uid != 0:
            cachedir = getCacheDir()
            self.logger.debug('Running as non-root, using %s as cachedir' % cachedir)
            if cachedir is None:
                self.logger.error("Error: Could not make cachedir, exiting")
                sys.exit(50)
            self.repos.setCacheDir(cachedir)

            # Turn off cache
            self.conf.cache = 0
            # make sure the repos know about it, too
            self.repos.setCache(0)

        # Run the checks preSetup methods
        if self.cmds[0] in self._checks:
            check = self._checks[self.cmds[0]]
            args = self.cmds[1:]
            check.doPreSetup(self,args,opts)
            
        # Setup yum (Ts, RPM db, Repo & Sack)
        self.doUtilYumSetup()
        
        # Run the check
        if self.cmds[0] in self._checks:
            check = self._checks[self.cmds[0]]
            args = self.cmds[1:]
            self.logger.info("Running the %s check" % self.cmds[0])
            check.runCheck(self,args,opts)
            
    def registerCheck(self, command):
        names = command.getNames()
        for name in names:
            if name in self._checks:
                self.logger.error('Command "%s" already defined' % name)
            self._checks[name] = command

00220     def _makeUsage(self):
        """
        Format an attractive usage string for yum, listing subcommand
        names and summary usages.
        """
        usage = 'repo-check [options] COMMAND\n\nList of Commands:\n\n'
        commands = yum.misc.unique(self._checks.values())
        commands.sort(cmp=lambda x,y : cmp(x.getNames()[0], y.getNames()[0]))
        for command in commands:
            # XXX Remove this when getSummary is common in plugins
            try:
                summary = command.getSummary()
                usage += "%-14s %s\n" % (command.getNames()[0], summary)
            except (AttributeError, NotImplementedError):
                usage += "%s\n" % command.getNames()[0]

        return usage


if __name__ == '__main__':
    setup_locale()
    util = RepoCheck()
    

Generated by  Doxygen 1.6.0   Back to index