<s> import sys <EOL> import contextlib <EOL> from collections import defaultdict <EOL> import logging <EOL> l = logging . getLogger ( '<STR_LIT>' ) <EOL> registered_analyses = { } <EOL> def register_analysis ( analysis , name ) : <EOL> registered_analyses [ name ] = analysis <EOL> class AnalysisLogEntry ( object ) : <EOL> def __init__ ( self , message , exc_info = False ) : <EOL> if exc_info : <EOL> ( e_type , value , traceback ) = sys . exc_info ( ) <EOL> self . exc_type = e_type <EOL> self . exc_value = value <EOL> self . exc_traceback = traceback <EOL> else : <EOL> self . exc_type = None <EOL> self . exc_value = None <EOL> self . exc_traceback = None <EOL> self . message = message <EOL> def __getstate__ ( self ) : <EOL> return str ( self . __dict__ . get ( "<STR_LIT>" ) ) , str ( self . __dict__ . get ( "<STR_LIT>" ) ) , str ( self . __dict__ . get ( "<STR_LIT>" ) ) , self . message <EOL> def __setstate__ ( self , s ) : <EOL> self . exc_type , self . exc_value , self . exc_traceback , self . message = s <EOL> def __repr__ ( self ) : <EOL> if self . exc_type is None : <EOL> msg_str = repr ( self . message ) <EOL> if len ( msg_str ) > <NUM_LIT> : <EOL> msg_str = msg_str [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> if msg_str [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) : <EOL> msg_str += msg_str [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' % msg_str <EOL> else : <EOL> msg_str = repr ( self . message ) <EOL> if len ( msg_str ) > <NUM_LIT> : <EOL> msg_str = msg_str [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> if msg_str [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) : <EOL> msg_str += msg_str [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' % ( msg_str , self . exc_type . __name__ , self . exc_value ) <EOL> class Analyses ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , p ) : <EOL> """<STR_LIT>""" <EOL> self . project = p <EOL> self . _registered_analyses = { } <EOL> self . reload_analyses ( ) <EOL> def reload_analyses ( self ) : <EOL> for analysis_name , analysis in registered_analyses . iteritems ( ) : <EOL> self . _registered_analyses [ analysis_name ] = self . _specialize_analysis ( analysis , analysis_name ) <EOL> def _specialize_analysis ( self , analysis , name ) : <EOL> def make_analysis ( * args , ** kwargs ) : <EOL> fail_fast = kwargs . pop ( '<STR_LIT>' , False ) <EOL> kb = kwargs . pop ( '<STR_LIT>' , self . project . kb ) <EOL> oself = analysis . __new__ ( analysis ) <EOL> oself . named_errors = { } <EOL> oself . errors = [ ] <EOL> oself . log = [ ] <EOL> oself . _fail_fast = fail_fast <EOL> oself . _name = name <EOL> oself . project = self . project <EOL> oself . kb = kb <EOL> oself . __init__ ( * args , ** kwargs ) <EOL> return oself <EOL> make_analysis . __doc__ = analysis . __init__ . __doc__ <EOL> return make_analysis <EOL> def __getstate__ ( self ) : <EOL> return self . project <EOL> def __setstate__ ( self , s ) : <EOL> self . __init__ ( s ) <EOL> def __getattr__ ( self , k ) : <EOL> r = super ( Analyses , self ) . __getattribute__ ( '<STR_LIT>' ) <EOL> if k == '<STR_LIT>' : <EOL> return r <EOL> if k in r : <EOL> return r [ k ] <EOL> return super ( Analyses , self ) . __getattribute__ ( k ) <EOL> def __dir__ ( self ) : <EOL> return dir ( Analyses ) + self . _registered_analyses . keys ( ) <EOL> class Analysis ( object ) : <EOL> """<STR_LIT>""" <EOL> project = None <EOL> kb = None <EOL> _fail_fast = None <EOL> _name = None <EOL> errors = [ ] <EOL> named_errors = defaultdict ( list ) <EOL> @ contextlib . contextmanager <EOL> def _resilience ( self , name = None , exception = Exception ) : <EOL> try : <EOL> yield <EOL> except exception : <EOL> if self . _fail_fast : <EOL> raise <EOL> else : <EOL> error = AnalysisLogEntry ( "<STR_LIT>" , exc_info = True ) <EOL> l . error ( "<STR_LIT>" , error . exc_type . __name__ , error . exc_value ) <EOL> if name is None : <EOL> self . errors . append ( error ) <EOL> else : <EOL> self . named_errors [ name ] . append ( error ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _name , id ( self ) ) </s>
