<s> import abc <EOL> from oslo_utils import importutils <EOL> from cloudferry . lib . utils import local_db <EOL> local_db . execute_once ( """<STR_LIT>""" ) <EOL> class Stage ( object ) : <EOL> __metaclass__ = abc . ABCMeta <EOL> dependencies = [ ] <EOL> def __init__ ( self , config ) : <EOL> """<STR_LIT>""" <EOL> self . config = config <EOL> @ abc . abstractmethod <EOL> def signature ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> @ abc . abstractmethod <EOL> def execute ( self ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> @ abc . abstractmethod <EOL> def invalidate ( self , old_signature , new_signature , force = False ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def execute_stage ( class_name , config , force = False ) : <EOL> """<STR_LIT>""" <EOL> cls = importutils . import_class ( class_name ) <EOL> assert issubclass ( cls , Stage ) <EOL> stage = cls ( config ) <EOL> for dependency in stage . dependencies : <EOL> execute_stage ( dependency , config ) <EOL> new_signature = stage . signature ( ) <EOL> old_signature = None <EOL> need_invalidate = False <EOL> need_execute = False <EOL> with local_db . Transaction ( ) as tx : <EOL> row = tx . query_one ( '<STR_LIT>' , <EOL> stage = class_name ) <EOL> if row is None : <EOL> need_execute = True <EOL> else : <EOL> old_signature = row [ '<STR_LIT>' ] . data <EOL> need_invalidate = ( old_signature != new_signature ) <EOL> with local_db . Transaction ( ) as tx : <EOL> if need_invalidate or force : <EOL> stage . invalidate ( old_signature , new_signature , force = force ) <EOL> tx . execute ( '<STR_LIT>' , <EOL> stage = class_name ) <EOL> need_execute = True <EOL> if need_execute : <EOL> stage . execute ( ) <EOL> tx . execute ( '<STR_LIT>' , <EOL> stage = class_name , <EOL> signature = local_db . Json ( new_signature ) ) </s>
