<s> """<STR_LIT>""" <EOL> import commands <EOL> import fnmatch <EOL> import optparse <EOL> import os <EOL> import sys <EOL> import checkers <EOL> import scmhandlers <EOL> class CommandError ( Exception ) : <EOL> pass <EOL> class CodeQuality ( object ) : <EOL> def __init__ ( self , options ) : <EOL> self . options = options <EOL> def codequality ( self , paths ) : <EOL> if self . options . list_checkers : <EOL> self . _list_checkers ( ) <EOL> return <EOL> if not paths and not self . options . scmhandler : <EOL> paths = [ '<STR_LIT:.>' ] <EOL> if self . options . scmhandler and self . options . scmhandler not in scmhandlers . scmhandlers : <EOL> raise CommandError ( <EOL> '<STR_LIT>' <EOL> % self . options . scmhandler ) <EOL> paths = self . _resolve_paths ( * paths ) <EOL> scmhandler = scmhandlers . scmhandlers . get ( <EOL> self . options . scmhandler , <EOL> scmhandlers . NoSCMHandler ) ( ) <EOL> errors_exist = False <EOL> checker_to_loc_to_filename = { } <EOL> for filename , location in scmhandler . srcs_to_check ( <EOL> paths , rev = self . options . rev , <EOL> ignore_untracked = self . options . ignore_untracked ) : <EOL> if self . _should_ignore ( filename ) : <EOL> continue <EOL> checker_classes = self . _relevant_checkers ( filename ) <EOL> for checker_class in checker_classes : <EOL> loc_to_filename = checker_to_loc_to_filename . setdefault ( <EOL> checker_class , { } ) <EOL> loc_to_filename [ location ] = filename <EOL> for checker_class , loc_to_filename in checker_to_loc_to_filename . iteritems ( ) : <EOL> locations = loc_to_filename . keys ( ) <EOL> if self . options . verbose : <EOL> for location , filename in loc_to_filename . iteritems ( ) : <EOL> print '<STR_LIT>' % ( <EOL> checker_class . __name__ , <EOL> filename , <EOL> '<STR_LIT>' if location == filename <EOL> else ( '<STR_LIT>' % location ) ) <EOL> try : <EOL> errs = checker_class ( ) . check ( locations ) <EOL> except OSError : <EOL> continue <EOL> for err in errs : <EOL> errors_exist = True <EOL> err [ '<STR_LIT:filename>' ] = loc_to_filename [ err [ '<STR_LIT:filename>' ] ] <EOL> print self . out_fmt % err <EOL> return errors_exist <EOL> out_fmt = '<STR_LIT>' <EOL> def _relevant_checkers ( self , path ) : <EOL> """<STR_LIT>""" <EOL> _ , ext = os . path . splitext ( path ) <EOL> ext = ext . lstrip ( '<STR_LIT:.>' ) <EOL> return checkers . checkers . get ( ext , [ ] ) <EOL> def _resolve_paths ( self , * paths ) : <EOL> """<STR_LIT>""" <EOL> result = set ( ) <EOL> for path in paths : <EOL> if os . path . isdir ( path ) : <EOL> for dirpath , _ , filenames in os . walk ( path ) : <EOL> for filename in filenames : <EOL> path = os . path . join ( dirpath , filename ) <EOL> if path . startswith ( '<STR_LIT:.>' ) : <EOL> path = path [ <NUM_LIT:1> : ] . lstrip ( '<STR_LIT:/>' ) <EOL> if not self . _should_ignore ( path ) : <EOL> result . add ( path ) <EOL> else : <EOL> result . add ( path ) <EOL> return result <EOL> def _list_checkers ( self ) : <EOL> """<STR_LIT>""" <EOL> classes = set ( ) <EOL> for checker_group in checkers . checkers . itervalues ( ) : <EOL> for checker in checker_group : <EOL> classes . add ( checker ) <EOL> max_width = <NUM_LIT:0> <EOL> for clazz in classes : <EOL> max_width = max ( max_width , len ( clazz . tool ) , len ( clazz . __name__ ) ) <EOL> for clazz in sorted ( classes ) : <EOL> status , _ = commands . getstatusoutput ( '<STR_LIT>' % clazz . tool ) <EOL> result = '<STR_LIT>' if status else '<STR_LIT>' <EOL> version = '<STR_LIT>' if status else clazz . get_version ( ) <EOL> print '<STR_LIT>' % ( <EOL> clazz . __name__ . ljust ( max_width + <NUM_LIT:1> ) , <EOL> clazz . tool . ljust ( max_width + <NUM_LIT:1> ) , <EOL> result . ljust ( max_width + <NUM_LIT:1> ) , <EOL> version , <EOL> ) <EOL> def _should_ignore ( self , path ) : <EOL> """<STR_LIT>""" <EOL> for ignore in self . options . ignores : <EOL> if fnmatch . fnmatch ( path , ignore ) : <EOL> return True <EOL> return False <EOL> def main ( ) : <EOL> parser = optparse . OptionParser ( <EOL> usage = "<STR_LIT>" % __doc__ . strip ( ) , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = [ ] , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> options , paths = parser . parse_args ( ) <EOL> try : <EOL> errs = CodeQuality ( options ) . codequality ( paths ) <EOL> if errs : <EOL> return <NUM_LIT:1> <EOL> except CommandError , e : <EOL> print >> sys . stderr , '<STR_LIT>' % e <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
