<s> from django . utils . encoding import smart_str <EOL> from hashlib import sha1 <EOL> from mediagenerator . generators . bundles . base import Filter <EOL> from mediagenerator . utils import get_media_dirs , read_text_file <EOL> from pyjs . translator import import_compiler , Translator , LIBRARY_PATH <EOL> from textwrap import dedent <EOL> import os <EOL> import sys <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> try : <EOL> from pyvascript . grammar import compile <EOL> from pyjs . translator import native_js_func <EOL> @ native_js_func <EOL> def PYVA ( content , unescape , is_statement , ** kwargs ) : <EOL> result = compile ( dedent ( unescape ( content ) ) ) <EOL> if not is_statement : <EOL> return result . strip ( ) . rstrip ( '<STR_LIT>' ) <EOL> return result <EOL> except ImportError : <EOL> pass <EOL> _HANDLE_EXCEPTIONS = """<STR_LIT>""" <EOL> PYJS_INIT_LIB_PATH = os . path . join ( LIBRARY_PATH , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> BUILTIN_PATH = os . path . join ( LIBRARY_PATH , '<STR_LIT>' ) <EOL> STDLIB_PATH = os . path . join ( LIBRARY_PATH , '<STR_LIT>' ) <EOL> EXTRA_LIBS_PATH = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> _LOAD_PYJSLIB = """<STR_LIT>""" <EOL> INIT_CODE = """<STR_LIT>""" . lstrip ( ) <EOL> class Pyjs ( Filter ) : <EOL> takes_input = False <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . config ( kwargs , exclude_main_libs = False , main_module = None , <EOL> debug = None , path = ( ) , only_dependencies = None ) <EOL> if isinstance ( self . path , basestring ) : <EOL> self . path = ( self . path , ) <EOL> self . path += tuple ( get_media_dirs ( ) ) <EOL> if self . only_dependencies is None : <EOL> self . only_dependencies = bool ( self . main_module ) <EOL> if self . only_dependencies : <EOL> self . path += ( STDLIB_PATH , BUILTIN_PATH , EXTRA_LIBS_PATH ) <EOL> super ( Pyjs , self ) . __init__ ( ** kwargs ) <EOL> assert self . filetype == '<STR_LIT>' , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % self . filetype ) <EOL> if self . only_dependencies : <EOL> assert self . main_module , '<STR_LIT>' <EOL> self . _compiled = { } <EOL> self . _collected = { } <EOL> @ classmethod <EOL> def from_default ( cls , name ) : <EOL> return { '<STR_LIT>' : name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) } <EOL> def get_output ( self , variation ) : <EOL> self . _collect_all_modules ( ) <EOL> if not self . exclude_main_libs : <EOL> yield self . _compile_init ( ) <EOL> if self . only_dependencies : <EOL> self . _regenerate ( dev_mode = False ) <EOL> for name in sorted ( self . _compiled . keys ( ) ) : <EOL> yield self . _compiled [ name ] [ <NUM_LIT:1> ] <EOL> else : <EOL> for name in sorted ( self . _collected . keys ( ) ) : <EOL> source = read_text_file ( self . _collected [ name ] ) <EOL> yield self . _compile ( name , source , dev_mode = False ) [ <NUM_LIT:0> ] <EOL> yield self . _compile_main ( dev_mode = False ) <EOL> def get_dev_output ( self , name , variation ) : <EOL> self . _collect_all_modules ( ) <EOL> name = name . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> if name == '<STR_LIT>' : <EOL> return self . _compile_init ( ) <EOL> elif name == '<STR_LIT>' : <EOL> return self . _compile_main ( dev_mode = True ) <EOL> if self . only_dependencies : <EOL> self . _regenerate ( dev_mode = True ) <EOL> return self . _compiled [ name ] [ <NUM_LIT:1> ] <EOL> else : <EOL> source = read_text_file ( self . _collected [ name ] ) <EOL> return self . _compile ( name , source , dev_mode = True ) [ <NUM_LIT:0> ] <EOL> def get_dev_output_names ( self , variation ) : <EOL> self . _collect_all_modules ( ) <EOL> if not self . exclude_main_libs : <EOL> content = self . _compile_init ( ) <EOL> hash = sha1 ( smart_str ( content ) ) . hexdigest ( ) <EOL> yield '<STR_LIT>' , hash <EOL> if self . only_dependencies : <EOL> self . _regenerate ( dev_mode = True ) <EOL> for name in sorted ( self . _compiled . keys ( ) ) : <EOL> yield name , self . _compiled [ name ] [ <NUM_LIT:2> ] <EOL> else : <EOL> for name in sorted ( self . _collected . keys ( ) ) : <EOL> yield name , None <EOL> if self . main_module is not None or not self . exclude_main_libs : <EOL> content = self . _compile_main ( dev_mode = True ) <EOL> hash = sha1 ( smart_str ( content ) ) . hexdigest ( ) <EOL> yield '<STR_LIT>' , hash <EOL> def _regenerate ( self , dev_mode = False ) : <EOL> if self . _compiled : <EOL> for module_name , ( mtime , content , hash ) in self . _compiled . items ( ) : <EOL> if module_name not in self . _collected or not os . path . exists ( self . _collected [ module_name ] ) or os . path . getmtime ( self . _collected [ module_name ] ) != mtime : <EOL> self . _compiled = { } <EOL> break <EOL> else : <EOL> return <EOL> modules = [ self . main_module , '<STR_LIT>' ] <EOL> while True : <EOL> if not modules : <EOL> break <EOL> module_name = modules . pop ( ) <EOL> path = self . _collected [ module_name ] <EOL> mtime = os . path . getmtime ( path ) <EOL> source = read_text_file ( path ) <EOL> try : <EOL> content , py_deps , js_deps = self . _compile ( module_name , source , dev_mode = dev_mode ) <EOL> except : <EOL> self . _compiled = { } <EOL> raise <EOL> hash = sha1 ( smart_str ( content ) ) . hexdigest ( ) <EOL> self . _compiled [ module_name ] = ( mtime , content , hash ) <EOL> for name in py_deps : <EOL> if name not in self . _collected : <EOL> if '<STR_LIT:.>' in name and name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] in self . _collected : <EOL> name = name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> else : <EOL> raise ImportError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( module_name , name ) ) <EOL> if name not in self . _compiled : <EOL> modules . append ( name ) <EOL> def _compile ( self , name , source , dev_mode = False ) : <EOL> if self . debug is None : <EOL> debug = dev_mode <EOL> else : <EOL> debug = self . debug <EOL> compiler = import_compiler ( False ) <EOL> tree = compiler . parse ( source ) <EOL> output = StringIO ( ) <EOL> translator = Translator ( compiler , name , name , source , tree , output , <EOL> debug = debug , source_tracking = debug , line_tracking = debug , <EOL> store_source = debug , <EOL> function_argument_checking = debug , attribute_checking = False , <EOL> inline_code = False , number_classes = False , <EOL> operator_funcs = True , bound_methods = True , descriptors = True , <EOL> ) <EOL> return output . getvalue ( ) , translator . imported_modules , translator . imported_js <EOL> def _compile_init ( self ) : <EOL> return INIT_CODE + read_text_file ( PYJS_INIT_LIB_PATH ) <EOL> def _compile_main ( self , dev_mode = False ) : <EOL> if self . debug is None : <EOL> debug = dev_mode <EOL> else : <EOL> debug = self . debug <EOL> content = '<STR_LIT>' <EOL> if not self . exclude_main_libs : <EOL> content += _LOAD_PYJSLIB <EOL> if self . main_module is not None : <EOL> content += '<STR_LIT>' <EOL> if debug : <EOL> content += '<STR_LIT>' <EOL> content += '<STR_LIT>' <EOL> content += '<STR_LIT>' <EOL> content += '<STR_LIT>' % self . main_module <EOL> if debug : <EOL> content += _HANDLE_EXCEPTIONS <EOL> return content <EOL> def _collect_all_modules ( self ) : <EOL> """<STR_LIT>""" <EOL> for pkgroot in self . path : <EOL> pkgroot = os . path . abspath ( pkgroot ) <EOL> has_followlinks = sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:6> ) <EOL> if has_followlinks : <EOL> allfiles = os . walk ( pkgroot , followlinks = True ) <EOL> else : <EOL> allfiles = os . walk ( pkgroot ) <EOL> for root , dirs , files in allfiles : <EOL> if '<STR_LIT>' in files : <EOL> files . remove ( '<STR_LIT>' ) <EOL> if root != pkgroot : <EOL> files . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> elif root != pkgroot : <EOL> dirs [ : ] = [ ] <EOL> continue <EOL> for filename in files : <EOL> if not filename . endswith ( '<STR_LIT>' ) : <EOL> continue <EOL> path = os . path . join ( root , filename ) <EOL> if not has_followlinks : <EOL> path = os . path . abspath ( path ) <EOL> module_path = path [ len ( pkgroot ) + len ( os . sep ) : ] <EOL> if os . path . basename ( module_path ) == '<STR_LIT>' : <EOL> module_name = os . path . dirname ( module_path ) <EOL> else : <EOL> module_name = module_path [ : - <NUM_LIT:3> ] <EOL> assert '<STR_LIT:.>' not in module_name , '<STR_LIT>' % module_path <EOL> module_name = module_name . replace ( os . sep , '<STR_LIT:.>' ) <EOL> self . _collected . setdefault ( module_name , path ) </s>
