<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import io <EOL> import os <EOL> import re <EOL> import shutil <EOL> import string <EOL> from anvil . context import RuleContext <EOL> from anvil . rule import Rule , build_rule <EOL> from anvil . task import Task , ExecutableTask <EOL> @ build_rule ( '<STR_LIT>' ) <EOL> class TemplateFilesRule ( Rule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , new_extension = None , params = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( TemplateFilesRule , self ) . __init__ ( name , * args , ** kwargs ) <EOL> self . new_extension = new_extension <EOL> self . params = params <EOL> class _Context ( RuleContext ) : <EOL> def begin ( self ) : <EOL> super ( TemplateFilesRule . _Context , self ) . begin ( ) <EOL> file_pairs = [ ] <EOL> for src_path in self . src_paths : <EOL> out_path = self . _get_out_path_for_src ( src_path ) <EOL> if self . rule . new_extension : <EOL> out_path = os . path . splitext ( out_path ) [ <NUM_LIT:0> ] + self . rule . new_extension <EOL> self . _ensure_output_exists ( os . path . dirname ( out_path ) ) <EOL> self . _append_output_paths ( [ out_path ] ) <EOL> file_pairs . append ( ( src_path , out_path ) ) <EOL> if self . _check_if_cached ( ) : <EOL> self . _succeed ( ) <EOL> return <EOL> d = self . _run_task_async ( _TemplateFilesTask ( <EOL> self . build_env , file_pairs , self . rule . params ) ) <EOL> self . _chain ( d ) <EOL> class _TemplateFilesTask ( Task ) : <EOL> def __init__ ( self , build_env , file_pairs , params , * args , ** kwargs ) : <EOL> super ( _TemplateFilesTask , self ) . __init__ ( build_env , * args , ** kwargs ) <EOL> self . file_pairs = file_pairs <EOL> self . params = params <EOL> def execute ( self ) : <EOL> for file_pair in self . file_pairs : <EOL> with io . open ( file_pair [ <NUM_LIT:0> ] , '<STR_LIT>' ) as f : <EOL> template_str = f . read ( ) <EOL> template = string . Template ( template_str ) <EOL> result_str = template . substitute ( self . params ) <EOL> with io . open ( file_pair [ <NUM_LIT:1> ] , '<STR_LIT>' ) as f : <EOL> f . write ( result_str ) <EOL> return True <EOL> @ build_rule ( '<STR_LIT>' ) <EOL> class StripCommentsRule ( Rule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , new_extension = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( StripCommentsRule , self ) . __init__ ( name , * args , ** kwargs ) <EOL> self . new_extension = new_extension <EOL> class _Context ( RuleContext ) : <EOL> def begin ( self ) : <EOL> super ( StripCommentsRule . _Context , self ) . begin ( ) <EOL> file_pairs = [ ] <EOL> for src_path in self . src_paths : <EOL> out_path = self . _get_out_path_for_src ( src_path ) <EOL> if self . rule . new_extension : <EOL> out_path = os . path . splitext ( out_path ) [ <NUM_LIT:0> ] + self . rule . new_extension <EOL> self . _ensure_output_exists ( os . path . dirname ( out_path ) ) <EOL> self . _append_output_paths ( [ out_path ] ) <EOL> file_pairs . append ( ( src_path , out_path ) ) <EOL> if self . _check_if_cached ( ) : <EOL> self . _succeed ( ) <EOL> return <EOL> d = self . _run_task_async ( _StripCommentsRuleTask ( <EOL> self . build_env , file_pairs ) ) <EOL> self . _chain ( d ) <EOL> class _StripCommentsRuleTask ( Task ) : <EOL> def __init__ ( self , build_env , file_pairs , * args , ** kwargs ) : <EOL> super ( _StripCommentsRuleTask , self ) . __init__ ( build_env , * args , ** kwargs ) <EOL> self . file_pairs = file_pairs <EOL> def execute ( self ) : <EOL> for file_pair in self . file_pairs : <EOL> with io . open ( file_pair [ <NUM_LIT:0> ] , '<STR_LIT>' ) as f : <EOL> raw_str = f . read ( ) <EOL> def replacer ( match ) : <EOL> s = match . group ( <NUM_LIT:0> ) <EOL> if s . startswith ( '<STR_LIT:/>' ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return s <EOL> pattern = re . compile ( <EOL> r'<STR_LIT>' , <EOL> re . DOTALL | re . MULTILINE ) <EOL> result_str = re . sub ( pattern , replacer , raw_str ) <EOL> with io . open ( file_pair [ <NUM_LIT:1> ] , '<STR_LIT>' ) as f : <EOL> f . write ( result_str ) <EOL> return True <EOL> @ build_rule ( '<STR_LIT>' ) <EOL> class PreprocessRule ( Rule ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , new_extension = None , defines = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( PreprocessRule , self ) . __init__ ( name , * args , ** kwargs ) <EOL> self . new_extension = new_extension <EOL> self . defines = defines [ : ] if defines else [ ] <EOL> class _Context ( RuleContext ) : <EOL> def begin ( self ) : <EOL> super ( PreprocessRule . _Context , self ) . begin ( ) <EOL> file_pairs = [ ] <EOL> for src_path in self . src_paths : <EOL> out_path = self . _get_out_path_for_src ( src_path ) <EOL> if self . rule . new_extension : <EOL> out_path = os . path . splitext ( out_path ) [ <NUM_LIT:0> ] + self . rule . new_extension <EOL> self . _ensure_output_exists ( os . path . dirname ( out_path ) ) <EOL> self . _append_output_paths ( [ out_path ] ) <EOL> file_pairs . append ( ( src_path , out_path ) ) <EOL> if self . _check_if_cached ( ) : <EOL> self . _succeed ( ) <EOL> return <EOL> d = self . _run_task_async ( _PreprocessFilesTask ( <EOL> self . build_env , file_pairs , self . rule . defines ) ) <EOL> self . _chain ( d ) <EOL> class _PreprocessFilesTask ( Task ) : <EOL> def __init__ ( self , build_env , file_pairs , defines , * args , ** kwargs ) : <EOL> super ( _PreprocessFilesTask , self ) . __init__ ( build_env , * args , ** kwargs ) <EOL> self . file_pairs = file_pairs <EOL> self . defines = defines <EOL> def execute ( self ) : <EOL> for file_pair in self . file_pairs : <EOL> with io . open ( file_pair [ <NUM_LIT:0> ] , '<STR_LIT>' ) as f : <EOL> source_lines = f . readlines ( ) <EOL> result_str = self . _preprocess ( source_lines , self . defines ) <EOL> with io . open ( file_pair [ <NUM_LIT:1> ] , '<STR_LIT>' ) as f : <EOL> f . write ( result_str ) <EOL> return True <EOL> def _preprocess ( self , source_lines , global_defines ) : <EOL> file_defines = set ( global_defines ) <EOL> inclusion_scopes = [ True ] <EOL> target_lines = [ ] <EOL> for line in source_lines : <EOL> line_included = all ( inclusion_scopes ) <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> line_included = False <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> value = line [ <NUM_LIT:7> : ] . strip ( ) <EOL> inclusion_scopes . append ( value in file_defines ) <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> inclusion_scopes [ - <NUM_LIT:1> ] = not inclusion_scopes [ - <NUM_LIT:1> ] <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> inclusion_scopes . pop ( ) <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> value = line [ <NUM_LIT:8> : ] . strip ( ) <EOL> file_defines . add ( value ) <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> value = line [ <NUM_LIT:7> : ] . strip ( ) <EOL> file_defines . remove ( value ) <EOL> if line_included : <EOL> target_lines . append ( line ) <EOL> return '<STR_LIT:\n>' . join ( target_lines ) </s>
