<s> import os <EOL> import sys <EOL> import util <EOL> import logging <EOL> import pyjs <EOL> import subprocess <EOL> from pyjs import translator <EOL> if translator . name == '<STR_LIT>' : <EOL> builtin_module = '<STR_LIT>' <EOL> elif translator . name == '<STR_LIT>' : <EOL> builtin_module = '<STR_LIT>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % translator . name ) <EOL> translate_cmd = '<STR_LIT>' <EOL> translate_cmd_opts = [ '<STR_LIT>' % translator . name ] <EOL> if pyjs . pyjspth is None : <EOL> PYLIB_PATH = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> BUILTIN_PATH = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> PYJAMASLIB_PATH = os . path . split ( os . path . dirname ( __file__ ) ) [ <NUM_LIT:0> ] <EOL> PYJAMASLIB_PATH = os . path . split ( PYJAMASLIB_PATH ) [ <NUM_LIT:0> ] <EOL> PYJAMASLIB_PATH = os . path . join ( os . path . split ( PYJAMASLIB_PATH ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> else : <EOL> PYLIB_PATH = os . path . join ( pyjs . pyjspth , "<STR_LIT>" , "<STR_LIT:src>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> BUILTIN_PATH = os . path . join ( pyjs . pyjspth , "<STR_LIT>" , "<STR_LIT:src>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> PYJAMASLIB_PATH = os . path . join ( pyjs . pyjspth , "<STR_LIT>" ) <EOL> translator_opts = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def is_modified ( in_file , out_file ) : <EOL> modified = False <EOL> in_mtime = os . path . getmtime ( in_file ) <EOL> try : <EOL> out_mtime = os . path . getmtime ( out_file ) <EOL> if in_mtime > out_mtime : <EOL> modified = True <EOL> except : <EOL> modified = True <EOL> return modified <EOL> def get_translator_opts ( args ) : <EOL> opts = [ ] <EOL> for k in translator_opts : <EOL> if args . has_key ( k ) : <EOL> nk = k . replace ( "<STR_LIT:_>" , "<STR_LIT:->" ) <EOL> if args [ k ] : <EOL> opts . append ( "<STR_LIT>" % nk ) <EOL> elif k != '<STR_LIT>' : <EOL> opts . append ( "<STR_LIT>" % nk ) <EOL> return opts <EOL> def parse_outfile ( out_file ) : <EOL> deps = [ ] <EOL> jslibs = [ ] <EOL> f = open ( out_file ) <EOL> spos = os . path . getsize ( out_file ) - <NUM_LIT:200> <EOL> if spos < <NUM_LIT:0> : <EOL> spos = <NUM_LIT:0> <EOL> while True : <EOL> f . seek ( spos ) <EOL> txt = f . read ( <NUM_LIT:200> ) <EOL> p = txt . find ( '<STR_LIT>' ) <EOL> if p >= <NUM_LIT:0> : <EOL> f . seek ( spos + p ) <EOL> txt = f . read ( ) <EOL> break <EOL> spos -= <NUM_LIT:100> <EOL> if spos < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % out_file ) <EOL> for l in txt . split ( "<STR_LIT:\n>" ) : <EOL> if l . startswith ( "<STR_LIT>" ) : <EOL> deps = eval ( l [ <NUM_LIT:10> : ] ) <EOL> for l in txt . split ( "<STR_LIT:\n>" ) : <EOL> if l . startswith ( "<STR_LIT>" ) : <EOL> jslibs = eval ( l [ <NUM_LIT:8> : ] ) <EOL> f . close ( ) <EOL> return deps , jslibs <EOL> def out_translate ( platform , file_names , out_file , module_name , <EOL> translator_args , incremental ) : <EOL> do_translate = False <EOL> if translator_args . get ( '<STR_LIT>' , None ) : <EOL> do_translate = True <EOL> elif incremental : <EOL> for file_name in file_names : <EOL> if is_modified ( file_name , out_file ) : <EOL> if platform is not None : <EOL> platform = "<STR_LIT>" % platform <EOL> else : <EOL> platform = '<STR_LIT>' <EOL> print "<STR_LIT>" % platform , file_name <EOL> do_translate = True <EOL> break <EOL> if not incremental or do_translate : <EOL> pydir = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> os . environ [ '<STR_LIT>' ] = sys . path [ <NUM_LIT:0> ] <EOL> opts = [ "<STR_LIT>" , module_name , "<STR_LIT>" ] <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> opts . append ( out_file ) <EOL> shell = False <EOL> else : <EOL> file_names = map ( lambda x : x . replace ( "<STR_LIT:U+0020>" , r"<STR_LIT>" ) , file_names ) <EOL> opts . append ( out_file . replace ( "<STR_LIT:U+0020>" , r"<STR_LIT>" ) ) <EOL> shell = True <EOL> opts += get_translator_opts ( translator_args ) + file_names <EOL> opts = [ pyjs . PYTHON ] + [ os . path . join ( pydir , translate_cmd ) ] + translate_cmd_opts + opts <EOL> pyjscompile_cmd = '<STR_LIT>' % '<STR_LIT>' . join ( opts ) <EOL> proc = subprocess . Popen ( pyjscompile_cmd , <EOL> stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> shell = shell , <EOL> cwd = os . path . dirname ( file_names [ <NUM_LIT:0> ] ) , <EOL> env = os . environ <EOL> ) <EOL> stdout_value , stderr_value = proc . communicate ( '<STR_LIT>' ) <EOL> if stderr_value : <EOL> raise translator . TranslationError ( stderr_value , None ) <EOL> if translator_args . get ( '<STR_LIT>' , None ) : <EOL> print "<STR_LIT>" % platform , file_names <EOL> print stdout_value <EOL> return [ ] , [ ] <EOL> deps , js_libs = parse_outfile ( out_file ) <EOL> return deps , js_libs <EOL> _path_cache = { } <EOL> def module_path ( name , path ) : <EOL> global _path_cache <EOL> candidates = [ ] <EOL> packages = { } <EOL> modules = { } <EOL> if name . endswith ( '<STR_LIT>' ) : <EOL> parts = [ name ] <EOL> else : <EOL> parts = name . split ( '<STR_LIT:.>' ) <EOL> if not name in _path_cache : <EOL> _path_cache [ name ] = { } <EOL> for p in path : <EOL> if p in _path_cache [ name ] : <EOL> if _path_cache [ name ] [ p ] is None : <EOL> continue <EOL> return _path_cache [ name ] [ p ] <EOL> tail = [ ] <EOL> for pn in parts : <EOL> tail . append ( pn ) <EOL> mn = '<STR_LIT:.>' . join ( tail ) <EOL> cp = os . path . join ( * ( [ p ] + tail ) ) <EOL> if mn in _path_cache : <EOL> cache = _path_cache [ mn ] <EOL> else : <EOL> cache = { } <EOL> _path_cache [ mn ] = cache <EOL> if p in cache : <EOL> if cache [ p ] is None : <EOL> break <EOL> elif os . path . isdir ( cp ) and os . path . exists ( <EOL> os . path . join ( cp , '<STR_LIT>' ) ) : <EOL> cache [ p ] = os . path . join ( cp , '<STR_LIT>' ) <EOL> elif os . path . exists ( cp + '<STR_LIT>' ) : <EOL> cache [ p ] = cp + '<STR_LIT>' <EOL> elif pn . endswith ( '<STR_LIT>' ) and os . path . exists ( cp ) : <EOL> cache [ p ] = cp <EOL> else : <EOL> cache [ p ] = None <EOL> if p in _path_cache [ name ] and not _path_cache [ name ] [ p ] is None : <EOL> return _path_cache [ name ] [ p ] <EOL> _path_cache [ name ] [ p ] = None <EOL> return None <EOL> raise RuntimeError , "<STR_LIT>" % name <EOL> class BaseLinker ( object ) : <EOL> platform_parents = { } <EOL> def __init__ ( self , modules , output = '<STR_LIT>' , <EOL> compiler = None , <EOL> debug = False , <EOL> js_libs = [ ] , static_js_libs = [ ] , early_static_js_libs = [ ] , late_static_js_libs = [ ] , dynamic_js_libs = [ ] , <EOL> early_static_app_libs = [ ] , unlinked_modules = [ ] , keep_lib_files = False , <EOL> platforms = [ ] , path = [ ] , <EOL> translator_arguments = { } , <EOL> compile_inplace = False , <EOL> list_imports = False , <EOL> translator_func = out_translate ) : <EOL> modules = [ mod . replace ( os . sep , '<STR_LIT:.>' ) for mod in modules ] <EOL> self . compiler = compiler <EOL> self . js_path = os . path . abspath ( output ) <EOL> self . top_module = modules [ <NUM_LIT:0> ] <EOL> self . modules = modules <EOL> self . output = os . path . abspath ( output ) <EOL> self . js_libs = list ( js_libs ) <EOL> self . static_js_libs = list ( static_js_libs ) <EOL> self . early_static_js_libs = list ( early_static_js_libs ) <EOL> self . late_static_js_libs = list ( late_static_js_libs ) <EOL> self . dynamic_js_libs = list ( dynamic_js_libs ) <EOL> self . early_static_app_libs = list ( early_static_app_libs ) <EOL> self . unlinked_modules = unlinked_modules <EOL> self . keep_lib_files = keep_lib_files <EOL> self . platforms = platforms <EOL> self . path = path + [ PYLIB_PATH ] <EOL> self . translator_arguments = translator_arguments <EOL> self . translator_func = translator_func <EOL> self . compile_inplace = compile_inplace <EOL> self . top_module_path = None <EOL> self . remove_files = { } <EOL> self . list_imports = list_imports <EOL> def __call__ ( self ) : <EOL> try : <EOL> self . visited_modules = { } <EOL> self . done = { } <EOL> self . dependencies = { } <EOL> self . visit_start ( ) <EOL> for platform in [ None ] + self . platforms : <EOL> self . visit_start_platform ( platform ) <EOL> old_path = self . path <EOL> self . path = [ BUILTIN_PATH , PYLIB_PATH , PYJAMASLIB_PATH ] <EOL> self . visit_modules ( [ builtin_module ] , platform ) <EOL> self . path = old_path <EOL> self . visit_modules ( self . modules , platform ) <EOL> if not self . list_imports : <EOL> self . visit_end_platform ( platform ) <EOL> if not self . list_imports : <EOL> self . visit_end ( ) <EOL> except translator . TranslationError , e : <EOL> raise <EOL> def visit_modules ( self , module_names , platform = None , parent_file = None ) : <EOL> prefix = '<STR_LIT>' <EOL> all_names = [ ] <EOL> for mn in module_names : <EOL> if not mn . endswith ( "<STR_LIT>" ) : <EOL> prefix = '<STR_LIT>' <EOL> for part in mn . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] : <EOL> pn = prefix + part <EOL> prefix = pn + '<STR_LIT:.>' <EOL> if pn not in all_names : <EOL> all_names . append ( pn ) <EOL> all_names . append ( mn ) <EOL> paths = self . path <EOL> parent_base = None <EOL> abs_name = None <EOL> if not parent_file is None : <EOL> for p in paths : <EOL> if parent_file . find ( p ) == <NUM_LIT:0> and p != parent_file : <EOL> parent_base = p <EOL> abs_name = os . path . split ( parent_file ) [ <NUM_LIT:0> ] <EOL> abs_name = '<STR_LIT:.>' . join ( abs_name [ len ( parent_base ) + <NUM_LIT:1> : ] . split ( os . sep ) ) <EOL> for mn in all_names : <EOL> p = None <EOL> if abs_name : <EOL> p = module_path ( abs_name + '<STR_LIT:.>' + mn , [ parent_base ] ) <EOL> if p : <EOL> mn = abs_name + '<STR_LIT:.>' + mn <EOL> if not p : <EOL> p = module_path ( mn , paths ) <EOL> if not p : <EOL> continue <EOL> raise RuntimeError , "<STR_LIT>" % ( <EOL> mn , self . dependencies ) <EOL> if mn == self . top_module : <EOL> self . top_module_path = p <EOL> override_paths = [ ] <EOL> if platform : <EOL> for pl in self . platform_parents . get ( platform , [ ] ) + [ platform ] : <EOL> override_path = module_path ( '<STR_LIT>' % ( pl , mn ) , <EOL> paths ) <EOL> if override_path and not override_path . endswith ( '<STR_LIT>' ) : <EOL> override_paths . append ( override_path ) <EOL> self . visit_module ( p , override_paths , platform , module_name = mn ) <EOL> def visit_module ( self , file_path , overrides , platform , <EOL> module_name ) : <EOL> dir_name , file_name = os . path . split ( file_path ) <EOL> if ( not file_name . endswith ( '<STR_LIT>' ) <EOL> and file_name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] != module_name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> ) : <EOL> if file_name == "<STR_LIT>" : <EOL> if os . path . basename ( dir_name ) != module_name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] : <EOL> return <EOL> else : <EOL> return <EOL> self . merge_resources ( dir_name ) <EOL> if platform and overrides : <EOL> plat_suffix = '<STR_LIT>' % platform <EOL> else : <EOL> plat_suffix = '<STR_LIT>' <EOL> if self . compile_inplace : <EOL> mod_part , extension = os . path . splitext ( file_path ) <EOL> out_file = mod_part + plat_suffix + pyjs . MOD_SUFFIX <EOL> else : <EOL> out_file = os . path . join ( self . output , '<STR_LIT>' , <EOL> module_name + plat_suffix + pyjs . MOD_SUFFIX ) <EOL> if out_file in self . done . get ( platform , [ ] ) : <EOL> return <EOL> if ( platform is None <EOL> or ( platform and overrides ) <EOL> or ( out_file not in self . done . get ( None , [ ] ) ) <EOL> ) : <EOL> if file_name . endswith ( '<STR_LIT>' ) : <EOL> if not self . list_imports : <EOL> fp = open ( out_file , '<STR_LIT:w>' ) <EOL> fp . write ( "<STR_LIT>" % file_name ) <EOL> fp . write ( open ( file_path , '<STR_LIT:r>' ) . read ( ) ) <EOL> fp . write ( "<STR_LIT>" % file_name ) <EOL> fp . write ( "<STR_LIT>" ) <EOL> fp . write ( "<STR_LIT>" % file_name ) <EOL> deps = [ ] <EOL> self . dependencies [ out_file ] = deps <EOL> else : <EOL> logging . info ( '<STR_LIT>' % ( <EOL> module_name , platform or '<STR_LIT:->' , out_file ) ) <EOL> deps , js_libs = self . translator_func ( platform , <EOL> [ file_path ] + overrides , <EOL> out_file , <EOL> module_name , <EOL> self . translator_arguments , <EOL> self . keep_lib_files ) <EOL> self . dependencies [ out_file ] = deps <EOL> for path , mode , location in js_libs : <EOL> if mode == '<STR_LIT:default>' : <EOL> if self . multi_file : <EOL> mode = '<STR_LIT>' <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> if mode == '<STR_LIT>' : <EOL> self . dynamic_js_libs . append ( path ) <EOL> elif mode == '<STR_LIT>' : <EOL> if location == '<STR_LIT>' : <EOL> self . early_static_js_libs . append ( path ) <EOL> elif location == '<STR_LIT>' : <EOL> self . static_js_libs . append ( path ) <EOL> elif location == '<STR_LIT>' : <EOL> self . late_static_js_libs . append ( path ) <EOL> else : <EOL> raise RuntimeError , "<STR_LIT>" % location <EOL> else : <EOL> raise RuntimeError , "<STR_LIT>" % mode <EOL> if '<STR_LIT:.>' in module_name : <EOL> for i , dep in enumerate ( deps ) : <EOL> if module_path ( dep , path = [ dir_name ] ) : <EOL> deps [ i ] = '<STR_LIT:.>' . join ( module_name . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] + [ dep ] ) <EOL> else : <EOL> deps = self . dependencies [ out_file ] <EOL> if out_file not in self . done . setdefault ( platform , [ ] ) : <EOL> self . done [ platform ] . append ( out_file ) <EOL> if module_name not in self . visited_modules . setdefault ( platform , [ ] ) : <EOL> self . visited_modules [ platform ] . append ( module_name ) <EOL> if deps : <EOL> self . visit_modules ( deps , platform , file_path ) <EOL> def merge_resources ( self , dir_name ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def visit_start ( self ) : <EOL> if not os . path . exists ( self . output ) : <EOL> os . mkdir ( self . output ) <EOL> if not self . compile_inplace : <EOL> lib_dir = os . path . join ( self . output , '<STR_LIT>' ) <EOL> if not os . path . exists ( lib_dir ) : <EOL> os . mkdir ( lib_dir ) <EOL> def visit_start_platform ( self , platform ) : <EOL> pass <EOL> def visit_end_platform ( self , platform ) : <EOL> pass <EOL> def visit_end ( self ) : <EOL> pass <EOL> def add_linker_options ( parser ) : <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> action = "<STR_LIT>" , default = [ ] , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> default = [ ] , <EOL> action = "<STR_LIT>" , help = "<STR_LIT>" ) </s>
