<s> import sys <EOL> import compiler <EOL> from compiler import ast , syntax <EOL> from compiler import misc <EOL> import os <EOL> import copy <EOL> import imp <EOL> from compiler . pycodegen import ModuleCodeGenerator <EOL> class PlatformParser : <EOL> def __init__ ( self , platform_dir = "<STR_LIT>" , verbose = False , chain_plat = None ) : <EOL> self . platform_dir = platform_dir <EOL> self . parse_cache = { } <EOL> self . platform = "<STR_LIT>" <EOL> self . verbose = verbose <EOL> self . chain_plat = chain_plat <EOL> def setPlatform ( self , platform ) : <EOL> self . platform = platform <EOL> def checkOverridePlatformFile ( self , file_name ) : <EOL> if self . chain_plat : <EOL> return self . chain_plat . checkOverridePlatformFile ( file_name ) <EOL> if not self . platform : <EOL> return None <EOL> platform_file_name = self . generatePlatformFilename ( file_name ) <EOL> if os . path . isfile ( platform_file_name ) : <EOL> return platform_file_name <EOL> return None <EOL> def parseModule ( self , module_name , file_name ) : <EOL> importing = False <EOL> if not self . parse_cache . has_key ( file_name ) : <EOL> importing = True <EOL> if self . chain_plat : <EOL> mod , _ov = self . chain_plat . parseModule ( module_name , file_name ) <EOL> else : <EOL> mod = compiler . parseFile ( file_name ) <EOL> self . parse_cache [ file_name ] = mod <EOL> else : <EOL> mod = self . parse_cache [ file_name ] <EOL> override = False <EOL> platform_file_name = self . generatePlatformFilename ( file_name ) <EOL> if self . verbose : <EOL> print "<STR_LIT>" , platform_file_name <EOL> if self . platform and os . path . isfile ( platform_file_name ) : <EOL> mod = copy . deepcopy ( mod ) <EOL> mod_override = compiler . parseFile ( platform_file_name ) <EOL> if self . verbose : <EOL> print "<STR_LIT>" , module_name , self . platform <EOL> merge ( module_name , mod , mod_override ) <EOL> override = True <EOL> if self . verbose : <EOL> if override : <EOL> print "<STR_LIT>" % ( module_name , self . platform ) <EOL> elif importing : <EOL> print "<STR_LIT>" % ( module_name ) <EOL> if override : <EOL> return mod , platform_file_name <EOL> return mod , file_name <EOL> def generatePlatformFilename ( self , file_name ) : <EOL> ( module_name , extension ) = os . path . splitext ( os . path . basename ( file_name ) ) <EOL> platform_file_name = module_name + self . platform + extension <EOL> return os . path . join ( os . path . dirname ( file_name ) , self . platform_dir , platform_file_name ) <EOL> class TranslationError ( Exception ) : <EOL> def __init__ ( self , msg , node , module_name = '<STR_LIT>' ) : <EOL> if node : <EOL> lineno = node . lineno <EOL> else : <EOL> lineno = "<STR_LIT>" <EOL> self . msg = msg <EOL> self . node = node <EOL> self . module_name = module_name <EOL> self . lineno = lineno <EOL> self . message = "<STR_LIT>" % ( module_name , lineno , msg , node ) <EOL> def __str__ ( self ) : <EOL> return self . message <EOL> def merge ( module_name , tree1 , tree2 ) : <EOL> for child in tree2 . node : <EOL> if isinstance ( child , ast . Function ) : <EOL> replaceFunction ( tree1 , child . name , child ) <EOL> elif isinstance ( child , ast . Class ) : <EOL> replaceClassMethods ( tree1 , child . name , child ) <EOL> else : <EOL> raise TranslationError ( <EOL> "<STR_LIT>" % child , child , module_name ) <EOL> return tree1 <EOL> def replaceFunction ( tree , function_name , function_node ) : <EOL> for child in tree . node : <EOL> if isinstance ( child , ast . Function ) and child . name == function_name : <EOL> copyFunction ( child , function_node ) <EOL> return <EOL> raise TranslationError ( <EOL> "<STR_LIT>" + function_name , function_node , None ) <EOL> def copyFunction ( target , source ) : <EOL> target . code = source . code <EOL> target . argnames = source . argnames <EOL> target . defaults = source . defaults <EOL> target . doc = source . doc <EOL> def addCode ( target , source ) : <EOL> target . nodes . append ( source ) <EOL> def copyAssign ( target , source ) : <EOL> target . nodes = source . nodes <EOL> target . expr = source . expr <EOL> target . lineno = source . lineno <EOL> def eqNodes ( nodes1 , nodes2 ) : <EOL> return str ( nodes1 ) == str ( nodes2 ) <EOL> def replaceClassMethods ( tree , class_name , class_node ) : <EOL> old_class_node = None <EOL> for child in tree . node : <EOL> if isinstance ( child , ast . Class ) and child . name == class_name : <EOL> old_class_node = child <EOL> break <EOL> if not old_class_node : <EOL> raise TranslationError ( <EOL> "<STR_LIT>" + class_name , class_node , self . module_name ) <EOL> for node in class_node . code : <EOL> if isinstance ( node , ast . Function ) : <EOL> found = False <EOL> for child in old_class_node . code : <EOL> if isinstance ( child , ast . Function ) and child . name == node . name : <EOL> found = True <EOL> copyFunction ( child , node ) <EOL> break <EOL> if not found : <EOL> raise TranslationError ( <EOL> "<STR_LIT>" + class_name + "<STR_LIT:.>" + node . name , <EOL> node , self . module_name ) <EOL> elif isinstance ( node , ast . Assign ) and isinstance ( node . nodes [ <NUM_LIT:0> ] , ast . AssName ) : <EOL> found = False <EOL> for child in old_class_node . code : <EOL> if isinstance ( child , ast . Assign ) and eqNodes ( child . nodes , node . nodes ) : <EOL> found = True <EOL> copyAssign ( child , node ) <EOL> if not found : <EOL> addCode ( old_class_node . code , node ) <EOL> elif isinstance ( node , ast . Pass ) : <EOL> pass <EOL> else : <EOL> raise TranslationError ( <EOL> "<STR_LIT>" % node , node , self . module_name ) <EOL> class Module : <EOL> mode = "<STR_LIT>" <EOL> def __init__ ( self , tree , filename ) : <EOL> self . tree = tree <EOL> self . filename = filename <EOL> self . code = None <EOL> def _get_tree ( self ) : <EOL> misc . set_filename ( self . filename , self . tree ) <EOL> syntax . check ( self . tree ) <EOL> return self . tree <EOL> def compile ( self ) : <EOL> pass <EOL> def getCode ( self ) : <EOL> return self . code <EOL> def compile ( self , display = <NUM_LIT:0> ) : <EOL> tree = self . _get_tree ( ) <EOL> gen = ModuleCodeGenerator ( tree ) <EOL> if display : <EOL> import pprint <EOL> print pprint . pprint ( tree ) <EOL> self . code = gen . getCode ( ) <EOL> def dump ( self , f ) : <EOL> f . write ( self . getPycHeader ( ) ) <EOL> marshal . dump ( self . code , f ) <EOL> MAGIC = imp . get_magic ( ) <EOL> def getPycHeader ( self ) : <EOL> mtime = os . path . getmtime ( self . filename ) <EOL> mtime = struct . pack ( '<STR_LIT>' , mtime ) <EOL> return self . MAGIC + mtime </s>
