<s> import os <EOL> import distutils . core <EOL> from string import Template <EOL> import lux <EOL> from lux . core import LuxCommand , Setting <EOL> from lux . utils . files import skipfile <EOL> SKIPDIRS = set ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class Command ( LuxCommand ) : <EOL> option_list = ( <EOL> Setting ( '<STR_LIT>' , ( '<STR_LIT>' , ) , <EOL> default = '<STR_LIT>' , <EOL> desc = ( '<STR_LIT>' ) ) , <EOL> Setting ( '<STR_LIT>' , ( '<STR_LIT>' , ) , <EOL> default = '<STR_LIT>' , <EOL> desc = ( '<STR_LIT>' ) ) , <EOL> ) <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def run ( self , options ) : <EOL> build = MediaBuilder ( self , options . js_src , options . scss_src ) <EOL> build ( ) <EOL> class MediaBuilder : <EOL> def __init__ ( self , cmd , js_src , scss_src ) : <EOL> self . write = cmd . write <EOL> self . app = cmd . app <EOL> self . js_src = js_src <EOL> self . scss_src = scss_src <EOL> def __call__ ( self ) : <EOL> self . media ( '<STR_LIT>' , self . scss_target ) <EOL> for name , src in self . media ( '<STR_LIT>' , self . js_target ) : <EOL> self . process_js ( name , src ) <EOL> self . process_js ( '<STR_LIT>' , self . app_base ( ) ) <EOL> def media ( self , media , get_target ) : <EOL> sources = [ ( '<STR_LIT>' , os . path . join ( lux . PACKAGE_DIR , '<STR_LIT>' , media ) ) ] <EOL> for ext in self . app . extensions . values ( ) : <EOL> if ext . meta . name != self . app . meta . name and ext . meta . media_dir : <EOL> src = os . path . join ( ext . meta . media_dir , media ) <EOL> if os . path . isdir ( src ) : <EOL> name = ext . meta . name . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> sources . append ( ( name , src ) ) <EOL> targets = [ ] <EOL> for name , src in sources : <EOL> target = get_target ( name ) <EOL> self . _copy ( src , target ) <EOL> targets . append ( ( name , target ) ) <EOL> sources = targets <EOL> return sources <EOL> def process_js ( self , prefix , src ) : <EOL> """<STR_LIT>""" <EOL> self . js_templates ( prefix , src ) <EOL> for name in os . listdir ( src ) : <EOL> src_name = os . path . join ( src , name ) <EOL> if not skipdir ( src_name ) : <EOL> self . js_templates ( prefix , src_name , name ) <EOL> def js_templates ( self , prefix , src , name = None ) : <EOL> """<STR_LIT>""" <EOL> templates = os . path . join ( src , '<STR_LIT>' ) <EOL> if not os . path . isdir ( templates ) : <EOL> return <EOL> tmpl = '<STR_LIT>' <EOL> bits = [ prefix ] <EOL> if name : <EOL> bits . append ( name ) <EOL> target_dir = self . js_target ( * bits ) <EOL> bits . append ( tmpl ) <EOL> file_name = '<STR_LIT:/>' . join ( bits ) <EOL> module_name = '<STR_LIT:.>' . join ( bits ) <EOL> cache = [ ] <EOL> cache_template = self . template ( '<STR_LIT>' ) <EOL> for filename in os . listdir ( templates ) : <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> with open ( os . path . join ( templates , filename ) , '<STR_LIT:r>' ) as fp : <EOL> text = fp . read ( ) <EOL> file_module_name = '<STR_LIT>' % ( file_name , filename ) <EOL> text = '<STR_LIT>' . join ( self . lines ( text ) ) <EOL> cache . append ( cache_template . safe_substitute ( <EOL> dict ( text = text , file_name = file_module_name ) ) ) <EOL> if cache : <EOL> if not os . path . isdir ( target_dir ) : <EOL> os . makedirs ( target_dir ) <EOL> target = os . path . join ( target_dir , '<STR_LIT>' % tmpl ) <EOL> cache = '<STR_LIT:\n>' . join ( cache ) <EOL> template = self . template ( '<STR_LIT>' ) <EOL> template = template . safe_substitute ( <EOL> dict ( cache = cache , module_name = module_name ) <EOL> ) <EOL> with open ( target , '<STR_LIT:w>' ) as fp : <EOL> fp . write ( template ) <EOL> self . write ( '<STR_LIT>' % target ) <EOL> def app_base ( self ) : <EOL> '''<STR_LIT>''' <EOL> return os . getcwd ( ) <EOL> def scss_target ( self , * args ) : <EOL> base = os . path . join ( self . app_base ( ) , self . scss_src ) <EOL> return self . _join_paths ( base , * args ) <EOL> def js_target ( self , * args ) : <EOL> base = os . path . join ( self . app_base ( ) , self . js_src ) <EOL> return self . _join_paths ( base , * args ) <EOL> def template ( self , name ) : <EOL> filename = os . path . join ( lux . PACKAGE_DIR , '<STR_LIT>' , '<STR_LIT>' , name ) <EOL> with open ( filename , '<STR_LIT:r>' ) as fp : <EOL> return Template ( fp . read ( ) ) <EOL> def lines ( self , text ) : <EOL> for line in text . split ( '<STR_LIT:\n>' ) : <EOL> yield '<STR_LIT>' % escape_quote ( line ) <EOL> def _join_paths ( self , dir , * args ) : <EOL> dir = os . path . join ( dir , * args ) if args else dir <EOL> return dir <EOL> def _copy ( self , src , target ) : <EOL> self . write ( '<STR_LIT>' % ( src , target ) ) <EOL> distutils . dir_util . copy_tree ( src , target , verbose = <NUM_LIT:0> ) <EOL> def escape_quote ( text ) : <EOL> return text . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> def skipdir ( path ) : <EOL> name = os . path . basename ( path ) <EOL> return skipfile ( name ) or not os . path . isdir ( path ) or name in SKIPDIRS </s>
