<s> import abc <EOL> import re <EOL> import six <EOL> from nailgun import consts <EOL> from nailgun import errors <EOL> from nailgun . expression import Expression <EOL> from nailgun . logger import logger <EOL> from nailgun . settings import settings <EOL> from nailgun import utils <EOL> from nailgun import yaql_ext <EOL> class Context ( object ) : <EOL> legacy_condition_transformers = [ <EOL> ( <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> r'<STR_LIT>' <EOL> ) , <EOL> ( <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> r'<STR_LIT>' <EOL> ) , <EOL> ( <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> r'<STR_LIT>' <EOL> ) <EOL> ] <EOL> def __init__ ( self , transaction ) : <EOL> self . _transaction = transaction <EOL> self . _yaql_context = yaql_ext . create_context ( <EOL> add_serializers = True , add_datadiff = True <EOL> ) <EOL> self . _yaql_engine = yaql_ext . create_engine ( ) <EOL> self . _yaql_expressions_cache = { } <EOL> def get_new_data ( self , node_id ) : <EOL> return self . _transaction . get_new_data ( node_id ) <EOL> def get_yaql_interpreter ( self , node_id ) : <EOL> context = self . _yaql_context . create_child_context ( ) <EOL> context [ '<STR_LIT>' ] = self . _transaction . get_new_data ( node_id ) <EOL> context [ '<STR_LIT>' ] = self . _transaction . get_old_data ( node_id ) <EOL> cache = self . _yaql_expressions_cache <EOL> def evaluate ( expression ) : <EOL> logger . debug ( "<STR_LIT>" , expression ) <EOL> try : <EOL> parsed_exp = cache [ expression ] <EOL> except KeyError : <EOL> parsed_exp = self . _yaql_engine ( expression ) <EOL> cache [ expression ] = parsed_exp <EOL> return parsed_exp . evaluate ( data = context [ '<STR_LIT>' ] , context = context ) <EOL> return evaluate <EOL> def get_legacy_interpreter ( self , node_id ) : <EOL> deployment_info = self . _transaction . get_new_data ( node_id ) <EOL> context = { <EOL> '<STR_LIT>' : deployment_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : deployment_info <EOL> } <EOL> def evaluate ( condition ) : <EOL> try : <EOL> logger . debug ( "<STR_LIT>" , condition ) <EOL> return Expression ( <EOL> Context . transform_legacy_condition ( condition ) , <EOL> context , <EOL> strict = False <EOL> ) . evaluate ( ) <EOL> except Exception as e : <EOL> logger . error ( <EOL> "<STR_LIT>" , <EOL> condition , e <EOL> ) <EOL> raise <EOL> return evaluate <EOL> def get_formatter_context ( self , node_id ) : <EOL> deployment_info = self . _transaction . get_new_data ( node_id ) <EOL> return { <EOL> '<STR_LIT>' : deployment_info [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : deployment_info [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : settings . MASTER_IP , <EOL> '<STR_LIT>' : deployment_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : settings <EOL> } <EOL> @ classmethod <EOL> def transform_legacy_condition ( cls , condition ) : <EOL> logger . debug ( "<STR_LIT>" , condition ) <EOL> for pattern , replacement in cls . legacy_condition_transformers : <EOL> condition = re . sub ( pattern , replacement , condition ) <EOL> logger . debug ( "<STR_LIT>" , condition ) <EOL> return condition <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class DeploymentTaskSerializer ( object ) : <EOL> fields = frozenset ( ( <EOL> '<STR_LIT:id>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> ) ) <EOL> @ abc . abstractmethod <EOL> def serialize ( self , node_id ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def get_required_fields ( cls , task , fields = None ) : <EOL> """<STR_LIT>""" <EOL> return { k : task . get ( k ) for k in ( fields or cls . fields ) } <EOL> class NoopTaskSerializer ( DeploymentTaskSerializer ) : <EOL> def __init__ ( self , context , task_template ) : <EOL> self . task_template = task_template <EOL> self . context = context <EOL> def serialize ( self , node_id ) : <EOL> task = self . get_required_fields ( <EOL> self . task_template , self . fields - { '<STR_LIT>' } <EOL> ) <EOL> task [ '<STR_LIT:type>' ] = consts . ORCHESTRATOR_TASK_TYPES . skipped <EOL> task [ '<STR_LIT>' ] = False <EOL> return task <EOL> class DefaultTaskSerializer ( NoopTaskSerializer ) : <EOL> def should_execute ( self , task , node_id ) : <EOL> condition = task . get ( '<STR_LIT>' , True ) <EOL> if isinstance ( condition , six . string_types ) : <EOL> interpreter = self . context . get_legacy_interpreter ( node_id ) <EOL> return interpreter ( condition ) <EOL> return condition <EOL> def serialize ( self , node_id ) : <EOL> logger . debug ( <EOL> "<STR_LIT>" , <EOL> self . task_template [ '<STR_LIT:id>' ] , node_id <EOL> ) <EOL> task = utils . traverse ( <EOL> self . task_template , <EOL> utils . text_format_safe , <EOL> self . context . get_formatter_context ( node_id ) , <EOL> { <EOL> '<STR_LIT>' : self . context . get_yaql_interpreter ( node_id ) <EOL> } <EOL> ) <EOL> if not self . should_execute ( task , node_id ) : <EOL> logger . debug ( <EOL> "<STR_LIT>" , self . task_template [ '<STR_LIT:id>' ] <EOL> ) <EOL> return super ( DefaultTaskSerializer , self ) . serialize ( node_id ) <EOL> task . setdefault ( '<STR_LIT>' , { } ) . setdefault ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> task . setdefault ( '<STR_LIT>' , True ) <EOL> return self . get_required_fields ( task ) <EOL> def handle_unsupported ( _ , task_template ) : <EOL> raise errors . SerializerNotSupported ( <EOL> '<STR_LIT>' <EOL> . format ( task_template [ '<STR_LIT:type>' ] ) <EOL> ) <EOL> class TasksSerializersFactory ( object ) : <EOL> known_types = { <EOL> consts . ORCHESTRATOR_TASK_TYPES . skipped : NoopTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . stage : NoopTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . copy_files : DefaultTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . puppet : DefaultTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . reboot : DefaultTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . shell : DefaultTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . sync : DefaultTaskSerializer , <EOL> consts . ORCHESTRATOR_TASK_TYPES . upload_file : DefaultTaskSerializer , <EOL> } <EOL> def __init__ ( self , transaction ) : <EOL> self . context = Context ( transaction ) <EOL> def create_serializer ( self , task_template ) : <EOL> serializer_class = self . known_types . get ( <EOL> task_template [ '<STR_LIT:type>' ] , handle_unsupported <EOL> ) <EOL> return serializer_class ( self . context , task_template ) </s>
