<s> from __future__ import ( absolute_import , division , generators , nested_scopes , print_function , <EOL> unicode_literals , with_statement ) <EOL> import os <EOL> import pkgutil <EOL> from pants . backend . python . targets . python_binary import PythonBinary <EOL> from pants . backend . python . targets . python_library import PythonLibrary <EOL> from pants . backend . python . tasks . python_task import PythonTask <EOL> from pants . base . exceptions import TaskError <EOL> from pants . base . generator import Generator , TemplateData <EOL> from pants . base . workunit import WorkUnit , WorkUnitLabel <EOL> class PythonEval ( PythonTask ) : <EOL> class Error ( TaskError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> compiled = kwargs . pop ( '<STR_LIT>' ) <EOL> failed = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( PythonEval . Error , self ) . __init__ ( * args , ** kwargs ) <EOL> self . compiled = compiled <EOL> self . failed = failed <EOL> _EVAL_TEMPLATE_PATH = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def _is_evalable ( target ) : <EOL> return isinstance ( target , ( PythonLibrary , PythonBinary ) ) <EOL> @ classmethod <EOL> def register_options ( cls , register ) : <EOL> super ( PythonEval , cls ) . register_options ( register ) <EOL> register ( '<STR_LIT>' , type = bool , <EOL> help = '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = bool , <EOL> help = '<STR_LIT>' ) <EOL> register ( '<STR_LIT>' , type = bool , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def execute ( self ) : <EOL> if self . get_options ( ) . skip : <EOL> return <EOL> targets = self . context . targets ( ) if self . get_options ( ) . closure else self . context . target_roots <EOL> with self . invalidated ( filter ( self . _is_evalable , targets ) , <EOL> topological_order = True ) as invalidation_check : <EOL> compiled = self . _compile_targets ( invalidation_check . invalid_vts ) <EOL> return compiled <EOL> def _compile_targets ( self , invalid_vts ) : <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , labels = [ WorkUnitLabel . MULTITOOL ] ) : <EOL> compiled = [ ] <EOL> failed = [ ] <EOL> for vt in invalid_vts : <EOL> target = vt . target <EOL> return_code = self . _compile_target ( target ) <EOL> if return_code == <NUM_LIT:0> : <EOL> vt . update ( ) <EOL> compiled . append ( target ) <EOL> else : <EOL> if self . get_options ( ) . fail_slow : <EOL> failed . append ( target ) <EOL> else : <EOL> raise self . Error ( '<STR_LIT>' . format ( target . address . spec ) , <EOL> compiled = compiled , <EOL> failed = [ target ] ) <EOL> if failed : <EOL> msg = '<STR_LIT>' . format ( <EOL> len ( failed ) , <EOL> '<STR_LIT>' . join ( t . address . spec for t in failed ) ) <EOL> raise self . Error ( msg , compiled = compiled , failed = failed ) <EOL> return compiled <EOL> def _compile_target ( self , target ) : <EOL> with self . context . new_workunit ( name = target . address . spec ) : <EOL> modules = [ ] <EOL> if isinstance ( target , PythonBinary ) : <EOL> source = '<STR_LIT>' . format ( target . entry_point ) <EOL> components = target . entry_point . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> module = components [ <NUM_LIT:0> ] <EOL> if len ( components ) == <NUM_LIT:2> : <EOL> function = components [ <NUM_LIT:1> ] <EOL> data = TemplateData ( source = source , <EOL> import_statement = '<STR_LIT>' . format ( module , function ) ) <EOL> else : <EOL> data = TemplateData ( source = source , import_statement = '<STR_LIT>' . format ( module ) ) <EOL> modules . append ( data ) <EOL> else : <EOL> for path in target . sources_relative_to_source_root ( ) : <EOL> if path . endswith ( '<STR_LIT>' ) : <EOL> if os . path . basename ( path ) == '<STR_LIT>' : <EOL> module_path = os . path . dirname ( path ) <EOL> else : <EOL> module_path , _ = os . path . splitext ( path ) <EOL> source = '<STR_LIT>' . format ( os . path . join ( target . target_base , path ) ) <EOL> module = module_path . replace ( os . path . sep , '<STR_LIT:.>' ) <EOL> data = TemplateData ( source = source , import_statement = '<STR_LIT>' . format ( module ) ) <EOL> modules . append ( data ) <EOL> if not modules : <EOL> return <NUM_LIT:0> <EOL> interpreter = self . select_interpreter_for_targets ( [ target ] ) <EOL> if isinstance ( target , PythonBinary ) : <EOL> pexinfo , platforms = target . pexinfo , target . platforms <EOL> else : <EOL> pexinfo , platforms = None , None <EOL> generator = Generator ( pkgutil . get_data ( __name__ , self . _EVAL_TEMPLATE_PATH ) , <EOL> chroot_parent = self . chroot_cache_dir , modules = modules ) <EOL> executable_file_content = generator . render ( ) <EOL> chroot = self . cached_chroot ( interpreter = interpreter , <EOL> pex_info = pexinfo , <EOL> targets = [ target ] , <EOL> platforms = platforms , <EOL> executable_file_content = executable_file_content ) <EOL> pex = chroot . pex ( ) <EOL> with self . context . new_workunit ( name = '<STR_LIT>' , <EOL> labels = [ WorkUnitLabel . COMPILER , WorkUnitLabel . RUN , <EOL> WorkUnitLabel . TOOL ] , <EOL> cmd = '<STR_LIT:U+0020>' . join ( pex . cmdline ( ) ) ) as workunit : <EOL> returncode = pex . run ( stdout = workunit . output ( '<STR_LIT>' ) , stderr = workunit . output ( '<STR_LIT>' ) ) <EOL> workunit . set_outcome ( WorkUnit . SUCCESS if returncode == <NUM_LIT:0> else WorkUnit . FAILURE ) <EOL> if returncode != <NUM_LIT:0> : <EOL> self . context . log . error ( '<STR_LIT>' . format ( target . address . spec ) ) <EOL> return returncode </s>
