<s> from turbulenz_tools . utils . dependencies import find_dependencies <EOL> from turbulenz_tools . utils . subproc import SubProc <EOL> from turbulenz_tools . utils . profiler import Profiler <EOL> from turbulenz_tools . tools . templates import read_file_utf8 <EOL> from turbulenz_tools . tools . templates import env_create <EOL> from turbulenz_tools . tools . templates import env_load_templates <EOL> from turbulenz_tools . tools . appcodegen import render_js <EOL> from turbulenz_tools . tools . appcodegen import render_js_extract_includes <EOL> from turbulenz_tools . tools . appcodegen import inject_js_from_options <EOL> from turbulenz_tools . tools . appcodegen import context_from_options <EOL> from turbulenz_tools . tools . appcodegen import default_parser_options <EOL> from turbulenz_tools . tools . appcodegen import output_dependency_info <EOL> from turbulenz_tools . tools . toolsexception import ToolsException <EOL> from turbulenz_tools . tools . stdtool import simple_options <EOL> from logging import getLogger <EOL> from os import remove <EOL> from os . path import relpath , abspath , normpath <EOL> from tempfile import NamedTemporaryFile <EOL> from optparse import OptionParser , TitledHelpFormatter <EOL> import subprocess <EOL> __version__ = '<STR_LIT>' <EOL> __dependencies__ = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> LOG = getLogger ( __name__ ) <EOL> def _parser ( ) : <EOL> parser = OptionParser ( description = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> usage = "<STR_LIT>" , <EOL> formatter = TitledHelpFormatter ( ) ) <EOL> default_parser_options ( parser ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> default = '<STR_LIT>' , help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> default = None , help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> default = None , help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , <EOL> default = None , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , <EOL> dest = "<STR_LIT>" , default = True , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , <EOL> dest = "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , default = [ ] , <EOL> dest = "<STR_LIT>" , help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" , default = [ ] ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , <EOL> dest = "<STR_LIT>" , default = <NUM_LIT:1000> , help = "<STR_LIT>" ) <EOL> return parser <EOL> def tzjs_dump_dependencies ( env , options , input_js ) : <EOL> """<STR_LIT>""" <EOL> injects = inject_js_from_options ( options ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> _ = [ LOG . info ( "<STR_LIT>" , i ) for i in injects ] <EOL> context = context_from_options ( options , input_js [ <NUM_LIT:0> ] ) <EOL> deps = render_js_extract_includes ( context , options , <EOL> env_load_templates ( env , input_js ) , <EOL> injects ) <EOL> for i in input_js : <EOL> deps += find_dependencies ( i , options . templatedirs , env ) <EOL> output_dependency_info ( options . dependency_file , options . output , deps ) <EOL> return <NUM_LIT:0> <EOL> def tzjs_compact ( options , infile , outfile ) : <EOL> LOG . info ( "<STR_LIT>" , infile , outfile ) <EOL> if options . yui is not None : <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , options . yui , <EOL> '<STR_LIT>' , str ( options . length ) , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , outfile , infile ] <EOL> elif options . closure is not None : <EOL> command = [ '<STR_LIT>' , '<STR_LIT>' , options . closure , <EOL> '<STR_LIT>' + outfile , <EOL> '<STR_LIT>' + infile ] <EOL> elif options . uglifyjs is not None : <EOL> uglify_rel_path = relpath ( options . uglifyjs ) . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> command = [ '<STR_LIT>' , uglify_rel_path , '<STR_LIT>' , outfile , infile ] <EOL> LOG . info ( "<STR_LIT>" , command ) <EOL> subproc = SubProc ( command ) <EOL> error_code = subproc . time_popen ( ) <EOL> if <NUM_LIT:0> != error_code : <EOL> raise ToolsException ( "<STR_LIT>" % ( error_code , "<STR_LIT:U+0020>" . join ( command ) ) ) <EOL> def tzjs_generate ( env , options , input_js ) : <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> inject_js = inject_js_from_options ( options ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> if <NUM_LIT:0> < len ( inject_js ) : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> for i in inject_js : <EOL> LOG . info ( "<STR_LIT>" , i ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> context = context_from_options ( options , input_js [ <NUM_LIT:0> ] ) <EOL> templates_js = env_load_templates ( env , input_js ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> ( rendered_js , inc_js ) = render_js ( context , options , templates_js , <EOL> inject_js ) <EOL> rendered_js = rendered_js . encode ( '<STR_LIT:utf-8>' ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> if <NUM_LIT:0> != len ( inc_js ) : <EOL> raise ToolsException ( "<STR_LIT>" ) <EOL> if options . stripdebug : <EOL> strip_path = "<STR_LIT>" <EOL> if options . stripdebugpath : <EOL> strip_path = normpath ( abspath ( options . stripdebugpath ) ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> p = subprocess . Popen ( '<STR_LIT>' % strip_path , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . STDOUT , <EOL> shell = True ) <EOL> p . communicate ( ) <EOL> if p . returncode != <NUM_LIT:0> : <EOL> raise ToolsException ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> strip_debug_flags = "<STR_LIT>" <EOL> if options . verbose : <EOL> strip_debug_flags += "<STR_LIT>" <EOL> for s in options . stripnamespaces : <EOL> strip_debug_flags += "<STR_LIT>" % s <EOL> for v in options . stripvars : <EOL> strip_debug_flags += "<STR_LIT>" % v <EOL> if options . ignoreerrors : <EOL> strip_debug_flags += "<STR_LIT>" <EOL> with NamedTemporaryFile ( delete = False ) as t : <EOL> LOG . info ( "<STR_LIT>" , t . name ) <EOL> t . write ( rendered_js ) <EOL> with NamedTemporaryFile ( delete = False ) as tmp_out : <EOL> pass <EOL> strip_cmd = "<STR_LIT>" % ( strip_path , strip_debug_flags , <EOL> tmp_out . name , t . name ) <EOL> LOG . info ( "<STR_LIT>" , strip_cmd ) <EOL> strip_retval = subprocess . call ( strip_cmd , shell = True ) <EOL> if <NUM_LIT:0> != strip_retval : <EOL> raise ToolsException ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( strip_retval , t . name ) ) <EOL> rendered_js = read_file_utf8 ( tmp_out . name ) . encode ( '<STR_LIT:utf-8>' ) <EOL> remove ( tmp_out . name ) <EOL> remove ( t . name ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> if options . mode != '<STR_LIT>' and ( options . yui or options . closure or options . uglifyjs ) : <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> with NamedTemporaryFile ( delete = False ) as t : <EOL> LOG . info ( "<STR_LIT>" , t . name ) <EOL> t . write ( rendered_js ) <EOL> LOG . info ( "<STR_LIT>" , options . output ) <EOL> tzjs_compact ( options , t . name , options . output ) <EOL> remove ( t . name ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> else : <EOL> LOG . info ( "<STR_LIT>" , options . output ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> try : <EOL> with open ( options . output , '<STR_LIT:wb>' ) as f : <EOL> f . write ( rendered_js ) <EOL> LOG . info ( "<STR_LIT>" ) <EOL> except IOError : <EOL> raise ToolsException ( "<STR_LIT>" % options . output ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> <EOL> def main ( ) : <EOL> ( options , args , parser ) = simple_options ( _parser , __version__ , <EOL> __dependencies__ ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> if <NUM_LIT:0> == len ( args ) : <EOL> LOG . error ( "<STR_LIT>" ) <EOL> parser . print_help ( ) <EOL> exit ( <NUM_LIT:1> ) <EOL> if options . mode not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> LOG . error ( "<STR_LIT>" , options . mode ) <EOL> parser . print_help ( ) <EOL> exit ( <NUM_LIT:1> ) <EOL> if options . output is None : <EOL> LOG . error ( "<STR_LIT>" ) <EOL> parser . print_help ( ) <EOL> exit ( <NUM_LIT:1> ) <EOL> env = env_create ( options ) <EOL> input_js = args <EOL> LOG . info ( "<STR_LIT>" , input_js ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> Profiler . start ( '<STR_LIT>' ) <EOL> retval = <NUM_LIT:1> <EOL> try : <EOL> if options . dependency : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> retval = tzjs_dump_dependencies ( env , options , input_js ) <EOL> else : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> retval = tzjs_generate ( env , options , input_js ) <EOL> except ToolsException , e : <EOL> LOG . error ( str ( e ) ) <EOL> exit ( <NUM_LIT:1> ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> Profiler . stop ( '<STR_LIT>' ) <EOL> Profiler . dump_data ( ) <EOL> return retval <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> exit ( main ( ) ) </s>
