<s> from . config import Config <EOL> from . context import Context <EOL> from . parser import ParserContext <EOL> from . util import debug <EOL> from . tasks import Call , Task <EOL> from . vendor import six <EOL> class Executor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , collection , config = None , core = None ) : <EOL> """<STR_LIT>""" <EOL> self . collection = collection <EOL> self . config = config if config is not None else Config ( ) <EOL> self . core = core <EOL> def execute ( self , * tasks ) : <EOL> """<STR_LIT>""" <EOL> debug ( "<STR_LIT>" . format ( [ x for x in tasks ] ) ) <EOL> calls = self . normalize ( tasks ) <EOL> debug ( "<STR_LIT>" . format ( calls ) ) <EOL> direct = list ( calls ) <EOL> config = self . config . clone ( ) <EOL> expanded = self . expand_calls ( calls , config ) <EOL> try : <EOL> dedupe = config . tasks . dedupe <EOL> except AttributeError : <EOL> dedupe = True <EOL> calls = self . dedupe ( expanded ) if dedupe else expanded <EOL> results = { } <EOL> for call in calls : <EOL> autoprint = call in direct and call . autoprint <EOL> args = call . args <EOL> debug ( "<STR_LIT>" . format ( call ) ) <EOL> if call . contextualized : <EOL> args = ( call . context , ) + args <EOL> result = call . task ( * args , ** call . kwargs ) <EOL> if autoprint : <EOL> print ( result ) <EOL> results [ call . task ] = result <EOL> return results <EOL> def normalize ( self , tasks ) : <EOL> """<STR_LIT>""" <EOL> calls = [ ] <EOL> for task in tasks : <EOL> name , kwargs = None , { } <EOL> if isinstance ( task , six . string_types ) : <EOL> name = task <EOL> elif isinstance ( task , ParserContext ) : <EOL> name = task . name <EOL> kwargs = task . as_kwargs <EOL> else : <EOL> name , kwargs = task <EOL> c = Call ( task = self . collection [ name ] , kwargs = kwargs , called_as = name ) <EOL> calls . append ( c ) <EOL> if not tasks and self . collection . default is not None : <EOL> calls = [ Call ( task = self . collection [ self . collection . default ] ) ] <EOL> return calls <EOL> def dedupe ( self , calls ) : <EOL> """<STR_LIT>""" <EOL> deduped = [ ] <EOL> debug ( "<STR_LIT>" ) <EOL> for call in calls : <EOL> if call not in deduped : <EOL> debug ( "<STR_LIT>" . format ( call ) ) <EOL> deduped . append ( call ) <EOL> else : <EOL> debug ( "<STR_LIT>" . format ( call ) ) <EOL> return deduped <EOL> def expand_calls ( self , calls , config ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> for call in calls : <EOL> if isinstance ( call , Task ) : <EOL> call = Call ( task = call ) <EOL> debug ( "<STR_LIT>" . format ( call ) ) <EOL> if call . contextualized : <EOL> debug ( "<STR_LIT>" ) <EOL> call . context = Context ( config = self . config_for ( call , config ) ) <EOL> else : <EOL> debug ( "<STR_LIT>" ) <EOL> ret . extend ( self . expand_calls ( call . pre , config ) ) <EOL> ret . append ( call ) <EOL> ret . extend ( self . expand_calls ( call . post , config ) ) <EOL> return ret <EOL> def config_for ( self , call , config , anonymous = False ) : <EOL> """<STR_LIT>""" <EOL> task_config = config . clone ( ) <EOL> if not anonymous : <EOL> task_config . load_collection ( <EOL> self . collection . configuration ( call . called_as ) <EOL> ) <EOL> task_config . load_shell_env ( ) <EOL> debug ( "<STR_LIT>" ) <EOL> return task_config </s>
