<s> """<STR_LIT>""" <EOL> import sys <EOL> import types <EOL> import time <EOL> import linecache <EOL> from twisted . python import log , reflect <EOL> lastRebuild = time . time ( ) <EOL> class Sensitive : <EOL> """<STR_LIT>""" <EOL> lastRebuild = lastRebuild <EOL> def needRebuildUpdate ( self ) : <EOL> yn = ( self . lastRebuild < lastRebuild ) <EOL> return yn <EOL> def rebuildUpToDate ( self ) : <EOL> self . lastRebuild = time . time ( ) <EOL> def latestVersionOf ( self , anObject ) : <EOL> """<STR_LIT>""" <EOL> t = type ( anObject ) <EOL> if t == types . FunctionType : <EOL> return latestFunction ( anObject ) <EOL> elif t == types . MethodType : <EOL> if anObject . im_self is None : <EOL> return getattr ( anObject . im_class , anObject . __name__ ) <EOL> else : <EOL> return getattr ( anObject . im_self , anObject . __name__ ) <EOL> elif t == types . InstanceType : <EOL> getattr ( anObject , '<STR_LIT>' , None ) <EOL> return anObject <EOL> elif t == types . ClassType : <EOL> return latestClass ( anObject ) <EOL> else : <EOL> log . msg ( '<STR_LIT>' ) <EOL> return anObject <EOL> _modDictIDMap = { } <EOL> def latestFunction ( oldFunc ) : <EOL> """<STR_LIT>""" <EOL> dictID = id ( oldFunc . func_globals ) <EOL> module = _modDictIDMap . get ( dictID ) <EOL> if module is None : <EOL> return oldFunc <EOL> return getattr ( module , oldFunc . __name__ ) <EOL> def latestClass ( oldClass ) : <EOL> """<STR_LIT>""" <EOL> module = reflect . namedModule ( oldClass . __module__ ) <EOL> newClass = getattr ( module , oldClass . __name__ ) <EOL> newBases = [ latestClass ( base ) for base in newClass . __bases__ ] <EOL> try : <EOL> newClass . __bases__ = tuple ( newBases ) <EOL> return newClass <EOL> except TypeError : <EOL> if newClass . __module__ == "<STR_LIT>" : <EOL> return newClass <EOL> ctor = getattr ( newClass , '<STR_LIT>' , type ) <EOL> return ctor ( newClass . __name__ , tuple ( newBases ) , dict ( newClass . __dict__ ) ) <EOL> class RebuildError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def updateInstance ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . __class__ = latestClass ( self . __class__ ) <EOL> except TypeError : <EOL> if hasattr ( self . __class__ , '<STR_LIT>' ) : <EOL> raise RebuildError ( "<STR_LIT>" ) <EOL> else : <EOL> raise <EOL> def __getattr__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> updateInstance ( self ) <EOL> log . msg ( "<STR_LIT>" % ( reflect . qual ( self . __class__ ) , name ) ) <EOL> result = getattr ( self , name ) <EOL> return result <EOL> def rebuild ( module , doLog = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> global lastRebuild <EOL> lastRebuild = time . time ( ) <EOL> if hasattr ( module , '<STR_LIT>' ) : <EOL> if not module . ALLOW_TWISTED_REBUILD : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if doLog : <EOL> log . msg ( '<STR_LIT>' % str ( module . __name__ ) ) <EOL> from twisted . python import components <EOL> components . ALLOW_DUPLICATES = True <EOL> d = module . __dict__ <EOL> _modDictIDMap [ id ( d ) ] = module <EOL> newclasses = { } <EOL> classes = { } <EOL> functions = { } <EOL> values = { } <EOL> if doLog : <EOL> log . msg ( '<STR_LIT>' % str ( module . __name__ ) ) <EOL> for k , v in d . items ( ) : <EOL> if type ( v ) == types . ClassType : <EOL> if v . __module__ == module . __name__ : <EOL> classes [ v ] = <NUM_LIT:1> <EOL> if doLog : <EOL> log . logfile . write ( "<STR_LIT:c>" ) <EOL> log . logfile . flush ( ) <EOL> elif type ( v ) == types . FunctionType : <EOL> if v . func_globals is module . __dict__ : <EOL> functions [ v ] = <NUM_LIT:1> <EOL> if doLog : <EOL> log . logfile . write ( "<STR_LIT:f>" ) <EOL> log . logfile . flush ( ) <EOL> elif isinstance ( v , type ) : <EOL> if v . __module__ == module . __name__ : <EOL> newclasses [ v ] = <NUM_LIT:1> <EOL> if doLog : <EOL> log . logfile . write ( "<STR_LIT:o>" ) <EOL> log . logfile . flush ( ) <EOL> values . update ( classes ) <EOL> values . update ( functions ) <EOL> fromOldModule = values . __contains__ <EOL> newclasses = newclasses . keys ( ) <EOL> classes = classes . keys ( ) <EOL> functions = functions . keys ( ) <EOL> if doLog : <EOL> log . msg ( '<STR_LIT>' ) <EOL> log . msg ( '<STR_LIT>' % str ( module . __name__ ) ) <EOL> reload ( module ) <EOL> linecache . clearcache ( ) <EOL> if doLog : <EOL> log . msg ( '<STR_LIT>' % str ( module . __name__ ) ) <EOL> for clazz in classes : <EOL> if getattr ( module , clazz . __name__ ) is clazz : <EOL> log . msg ( "<STR_LIT>" % reflect . qual ( clazz ) ) <EOL> else : <EOL> if doLog : <EOL> log . logfile . write ( "<STR_LIT:x>" ) <EOL> log . logfile . flush ( ) <EOL> clazz . __bases__ = ( ) <EOL> clazz . __dict__ . clear ( ) <EOL> clazz . __getattr__ = __getattr__ <EOL> clazz . __module__ = module . __name__ <EOL> if newclasses : <EOL> import gc <EOL> for nclass in newclasses : <EOL> ga = getattr ( module , nclass . __name__ ) <EOL> if ga is nclass : <EOL> log . msg ( "<STR_LIT>" % reflect . qual ( nclass ) ) <EOL> else : <EOL> for r in gc . get_referrers ( nclass ) : <EOL> if getattr ( r , '<STR_LIT>' , None ) is nclass : <EOL> r . __class__ = ga <EOL> if doLog : <EOL> log . msg ( '<STR_LIT>' ) <EOL> log . msg ( '<STR_LIT>' % str ( module . __name__ ) ) <EOL> modcount = <NUM_LIT:0> <EOL> for mk , mod in sys . modules . items ( ) : <EOL> modcount = modcount + <NUM_LIT:1> <EOL> if mod == module or mod is None : <EOL> continue <EOL> if not hasattr ( mod , '<STR_LIT>' ) : <EOL> continue <EOL> if hasattr ( mod , '<STR_LIT>' ) : <EOL> continue <EOL> changed = <NUM_LIT:0> <EOL> for k , v in mod . __dict__ . items ( ) : <EOL> try : <EOL> hash ( v ) <EOL> except Exception : <EOL> continue <EOL> if fromOldModule ( v ) : <EOL> if type ( v ) == types . ClassType : <EOL> if doLog : <EOL> log . logfile . write ( "<STR_LIT:c>" ) <EOL> log . logfile . flush ( ) <EOL> nv = latestClass ( v ) <EOL> else : <EOL> if doLog : <EOL> log . logfile . write ( "<STR_LIT:f>" ) <EOL> log . logfile . flush ( ) <EOL> nv = latestFunction ( v ) <EOL> changed = <NUM_LIT:1> <EOL> setattr ( mod , k , nv ) <EOL> else : <EOL> if type ( v ) == types . ClassType : <EOL> for base in v . __bases__ : <EOL> if fromOldModule ( base ) : <EOL> latestClass ( v ) <EOL> if doLog and not changed and ( ( modcount % <NUM_LIT:10> ) == <NUM_LIT:0> ) : <EOL> log . logfile . write ( "<STR_LIT:.>" ) <EOL> log . logfile . flush ( ) <EOL> components . ALLOW_DUPLICATES = False <EOL> if doLog : <EOL> log . msg ( '<STR_LIT>' ) <EOL> log . msg ( '<STR_LIT>' % str ( module . __name__ ) ) <EOL> return module </s>
