<s> import copy <EOL> import gyp . input <EOL> import optparse <EOL> import os . path <EOL> import re <EOL> import shlex <EOL> import sys <EOL> import traceback <EOL> from gyp . common import GypError <EOL> debug = { } <EOL> DEBUG_GENERAL = '<STR_LIT>' <EOL> DEBUG_VARIABLES = '<STR_LIT>' <EOL> DEBUG_INCLUDES = '<STR_LIT>' <EOL> def DebugOutput ( mode , message ) : <EOL> if '<STR_LIT:all>' in gyp . debug . keys ( ) or mode in gyp . debug . keys ( ) : <EOL> ctx = ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> try : <EOL> f = traceback . extract_stack ( limit = <NUM_LIT:2> ) <EOL> if f : <EOL> ctx = f [ <NUM_LIT:0> ] [ : <NUM_LIT:3> ] <EOL> except : <EOL> pass <EOL> print '<STR_LIT>' % ( mode . upper ( ) , os . path . basename ( ctx [ <NUM_LIT:0> ] ) , <EOL> ctx [ <NUM_LIT:1> ] , ctx [ <NUM_LIT:2> ] , message ) <EOL> def FindBuildFiles ( ) : <EOL> extension = '<STR_LIT>' <EOL> files = os . listdir ( os . getcwd ( ) ) <EOL> build_files = [ ] <EOL> for file in files : <EOL> if file . endswith ( extension ) : <EOL> build_files . append ( file ) <EOL> return build_files <EOL> def Load ( build_files , format , default_variables = { } , <EOL> includes = [ ] , depth = '<STR_LIT:.>' , params = None , check = False , <EOL> circular_check = True ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = { } <EOL> flavor = None <EOL> if '<STR_LIT:->' in format : <EOL> format , params [ '<STR_LIT>' ] = format . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> default_variables = copy . copy ( default_variables ) <EOL> default_variables [ '<STR_LIT>' ] = format <EOL> if format . endswith ( '<STR_LIT>' ) : <EOL> generator_name = os . path . splitext ( format ) [ <NUM_LIT:0> ] <EOL> path , generator_name = os . path . split ( generator_name ) <EOL> path = os . path . abspath ( path ) <EOL> if path not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , path ) <EOL> else : <EOL> generator_name = '<STR_LIT>' + format <EOL> generator = __import__ ( generator_name , globals ( ) , locals ( ) , generator_name ) <EOL> for ( key , val ) in generator . generator_default_variables . items ( ) : <EOL> default_variables . setdefault ( key , val ) <EOL> if getattr ( generator , '<STR_LIT>' , None ) : <EOL> generator . CalculateVariables ( default_variables , params ) <EOL> if getattr ( generator , '<STR_LIT>' , None ) : <EOL> generator . CalculateGeneratorInputInfo ( params ) <EOL> generator_input_info = { <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , <EOL> '<STR_LIT>' , True ) , <EOL> '<STR_LIT>' : <EOL> getattr ( generator , '<STR_LIT>' , False ) , <EOL> } <EOL> result = gyp . input . Load ( build_files , default_variables , includes [ : ] , <EOL> depth , generator_input_info , check , circular_check , <EOL> params [ '<STR_LIT>' ] ) <EOL> return [ generator ] + result <EOL> def NameValueListToDict ( name_value_list ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for item in name_value_list : <EOL> tokens = item . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( tokens ) == <NUM_LIT:2> : <EOL> try : <EOL> token_value = int ( tokens [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> token_value = tokens [ <NUM_LIT:1> ] <EOL> result [ tokens [ <NUM_LIT:0> ] ] = token_value <EOL> else : <EOL> result [ tokens [ <NUM_LIT:0> ] ] = True <EOL> return result <EOL> def ShlexEnv ( env_name ) : <EOL> flags = os . environ . get ( env_name , [ ] ) <EOL> if flags : <EOL> flags = shlex . split ( flags ) <EOL> return flags <EOL> def FormatOpt ( opt , value ) : <EOL> if opt . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' % ( opt , value ) <EOL> return opt + value <EOL> def RegenerateAppendFlag ( flag , values , predicate , env_name , options ) : <EOL> """<STR_LIT>""" <EOL> flags = [ ] <EOL> if options . use_environment and env_name : <EOL> for flag_value in ShlexEnv ( env_name ) : <EOL> value = FormatOpt ( flag , predicate ( flag_value ) ) <EOL> if value in flags : <EOL> flags . remove ( value ) <EOL> flags . append ( value ) <EOL> if values : <EOL> for flag_value in values : <EOL> flags . append ( FormatOpt ( flag , predicate ( flag_value ) ) ) <EOL> return flags <EOL> def RegenerateFlags ( options ) : <EOL> """<STR_LIT>""" <EOL> def FixPath ( path ) : <EOL> path = gyp . common . FixIfRelativePath ( path , options . depth ) <EOL> if not path : <EOL> return os . path . curdir <EOL> return path <EOL> def Noop ( value ) : <EOL> return value <EOL> flags = [ '<STR_LIT>' ] <EOL> for name , metadata in options . _regeneration_metadata . iteritems ( ) : <EOL> opt = metadata [ '<STR_LIT>' ] <EOL> value = getattr ( options , name ) <EOL> value_predicate = metadata [ '<STR_LIT:type>' ] == '<STR_LIT:path>' and FixPath or Noop <EOL> action = metadata [ '<STR_LIT:action>' ] <EOL> env_name = metadata [ '<STR_LIT>' ] <EOL> if action == '<STR_LIT>' : <EOL> flags . extend ( RegenerateAppendFlag ( opt , value , value_predicate , <EOL> env_name , options ) ) <EOL> elif action in ( '<STR_LIT:store>' , None ) : <EOL> if value : <EOL> flags . append ( FormatOpt ( opt , value_predicate ( value ) ) ) <EOL> elif options . use_environment and env_name and os . environ . get ( env_name ) : <EOL> flags . append ( FormatOpt ( opt , value_predicate ( os . environ . get ( env_name ) ) ) ) <EOL> elif action in ( '<STR_LIT:store_true>' , '<STR_LIT>' ) : <EOL> if ( ( action == '<STR_LIT:store_true>' and value ) or <EOL> ( action == '<STR_LIT>' and not value ) ) : <EOL> flags . append ( opt ) <EOL> elif options . use_environment and env_name : <EOL> print >> sys . stderr , ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( action , opt , <EOL> env_name ) ) <EOL> else : <EOL> print >> sys . stderr , ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( action , opt ) ) <EOL> return flags <EOL> class RegeneratableOptionParser ( optparse . OptionParser ) : <EOL> def __init__ ( self ) : <EOL> self . __regeneratable_options = { } <EOL> optparse . OptionParser . __init__ ( self ) <EOL> def add_option ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> env_name = kw . pop ( '<STR_LIT>' , None ) <EOL> if '<STR_LIT>' in kw and kw . pop ( '<STR_LIT>' , True ) : <EOL> dest = kw [ '<STR_LIT>' ] <EOL> type = kw . get ( '<STR_LIT:type>' ) <EOL> if type == '<STR_LIT:path>' : <EOL> kw [ '<STR_LIT:type>' ] = '<STR_LIT:string>' <EOL> self . __regeneratable_options [ dest ] = { <EOL> '<STR_LIT:action>' : kw . get ( '<STR_LIT:action>' ) , <EOL> '<STR_LIT:type>' : type , <EOL> '<STR_LIT>' : env_name , <EOL> '<STR_LIT>' : args [ <NUM_LIT:0> ] , <EOL> } <EOL> optparse . OptionParser . add_option ( self , * args , ** kw ) <EOL> def parse_args ( self , * args ) : <EOL> values , args = optparse . OptionParser . parse_args ( self , * args ) <EOL> values . _regeneration_metadata = self . __regeneratable_options <EOL> return values , args <EOL> def gyp_main ( args ) : <EOL> my_name = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> parser = RegeneratableOptionParser ( ) <EOL> usage = '<STR_LIT>' <EOL> parser . set_usage ( usage . replace ( '<STR_LIT:%s>' , '<STR_LIT>' ) ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> env_name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> env_name = '<STR_LIT>' , regenerate = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> regenerate = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , type = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , type = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = [ ] , help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , default = [ ] , <EOL> metavar = '<STR_LIT>' , env_name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , default = None , metavar = '<STR_LIT>' , type = '<STR_LIT:path>' , <EOL> env_name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = True , regenerate = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> env_name = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> default = None , metavar = '<STR_LIT>' , type = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = True , regenerate = False , <EOL> help = "<STR_LIT>" ) <EOL> home_vars = [ '<STR_LIT>' ] <EOL> if sys . platform in ( '<STR_LIT>' , '<STR_LIT:win32>' ) : <EOL> home_vars . append ( '<STR_LIT>' ) <EOL> home = None <EOL> home_dot_gyp = None <EOL> for home_var in home_vars : <EOL> home = os . getenv ( home_var ) <EOL> if home != None : <EOL> home_dot_gyp = os . path . join ( home , '<STR_LIT>' ) <EOL> if not os . path . exists ( home_dot_gyp ) : <EOL> home_dot_gyp = None <EOL> else : <EOL> break <EOL> options , build_files_arg = parser . parse_args ( args ) <EOL> build_files = build_files_arg <EOL> if not options . formats : <EOL> generate_formats = [ ] <EOL> if options . use_environment : <EOL> generate_formats = os . environ . get ( '<STR_LIT>' , [ ] ) <EOL> if generate_formats : <EOL> generate_formats = re . split ( '<STR_LIT>' , generate_formats ) <EOL> if generate_formats : <EOL> options . formats = generate_formats <EOL> else : <EOL> if sys . platform == '<STR_LIT>' : <EOL> options . formats = [ '<STR_LIT>' ] <EOL> elif sys . platform in ( '<STR_LIT:win32>' , '<STR_LIT>' ) : <EOL> options . formats = [ '<STR_LIT>' ] <EOL> else : <EOL> options . formats = [ '<STR_LIT>' ] <EOL> if not options . generator_output and options . use_environment : <EOL> g_o = os . environ . get ( '<STR_LIT>' ) <EOL> if g_o : <EOL> options . generator_output = g_o <EOL> if not options . parallel and options . use_environment : <EOL> options . parallel = bool ( os . environ . get ( '<STR_LIT>' ) ) <EOL> for mode in options . debug : <EOL> gyp . debug [ mode ] = <NUM_LIT:1> <EOL> if DEBUG_GENERAL in gyp . debug . keys ( ) : <EOL> DebugOutput ( DEBUG_GENERAL , '<STR_LIT>' ) <EOL> for option , value in sorted ( options . __dict__ . items ( ) ) : <EOL> if option [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> continue <EOL> if isinstance ( value , basestring ) : <EOL> DebugOutput ( DEBUG_GENERAL , "<STR_LIT>" % ( option , value ) ) <EOL> else : <EOL> DebugOutput ( DEBUG_GENERAL , "<STR_LIT>" % ( option , str ( value ) ) ) <EOL> if not build_files : <EOL> build_files = FindBuildFiles ( ) <EOL> if not build_files : <EOL> raise GypError ( ( usage + '<STR_LIT>' ) % <EOL> ( my_name , my_name ) ) <EOL> if not options . depth : <EOL> for build_file in build_files : <EOL> build_file_dir = os . path . abspath ( os . path . dirname ( build_file ) ) <EOL> build_file_dir_components = build_file_dir . split ( os . path . sep ) <EOL> components_len = len ( build_file_dir_components ) <EOL> for index in xrange ( components_len - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if build_file_dir_components [ index ] == '<STR_LIT:src>' : <EOL> options . depth = os . path . sep . join ( build_file_dir_components ) <EOL> break <EOL> del build_file_dir_components [ index ] <EOL> if options . depth : <EOL> break <EOL> if not options . depth : <EOL> raise GypError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not options . toplevel_dir : <EOL> options . toplevel_dir = options . depth <EOL> cmdline_default_variables = { } <EOL> defines = [ ] <EOL> if options . use_environment : <EOL> defines += ShlexEnv ( '<STR_LIT>' ) <EOL> if options . defines : <EOL> defines += options . defines <EOL> cmdline_default_variables = NameValueListToDict ( defines ) <EOL> if DEBUG_GENERAL in gyp . debug . keys ( ) : <EOL> DebugOutput ( DEBUG_GENERAL , <EOL> "<STR_LIT>" % cmdline_default_variables ) <EOL> includes = [ ] <EOL> if home_dot_gyp != None : <EOL> default_include = os . path . join ( home_dot_gyp , '<STR_LIT>' ) <EOL> if os . path . exists ( default_include ) : <EOL> print '<STR_LIT>' + default_include <EOL> includes . append ( default_include ) <EOL> if options . includes : <EOL> includes . extend ( options . includes ) <EOL> gen_flags = [ ] <EOL> if options . use_environment : <EOL> gen_flags += ShlexEnv ( '<STR_LIT>' ) <EOL> if options . generator_flags : <EOL> gen_flags += options . generator_flags <EOL> generator_flags = NameValueListToDict ( gen_flags ) <EOL> if DEBUG_GENERAL in gyp . debug . keys ( ) : <EOL> DebugOutput ( DEBUG_GENERAL , "<STR_LIT>" % generator_flags ) <EOL> if options . msvs_version : <EOL> print >> sys . stderr , '<STR_LIT>' + options . msvs_version + '<STR_LIT>' + options . msvs_version <EOL> generator_flags [ '<STR_LIT>' ] = options . msvs_version <EOL> for format in set ( options . formats ) : <EOL> params = { '<STR_LIT>' : options , <EOL> '<STR_LIT>' : build_files , <EOL> '<STR_LIT>' : generator_flags , <EOL> '<STR_LIT>' : os . getcwd ( ) , <EOL> '<STR_LIT>' : build_files_arg , <EOL> '<STR_LIT>' : sys . argv [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : home_dot_gyp , <EOL> '<STR_LIT>' : options . parallel } <EOL> [ generator , flat_list , targets , data ] = Load ( build_files , format , <EOL> cmdline_default_variables , <EOL> includes , options . depth , <EOL> params , options . check , <EOL> options . circular_check ) <EOL> generator . GenerateOutput ( flat_list , targets , data , params ) <EOL> if options . configs : <EOL> valid_configs = targets [ flat_list [ <NUM_LIT:0> ] ] [ '<STR_LIT>' ] . keys ( ) <EOL> for conf in options . configs : <EOL> if conf not in valid_configs : <EOL> raise GypError ( '<STR_LIT>' % conf ) <EOL> generator . PerformBuild ( data , options . configs , params ) <EOL> return <NUM_LIT:0> <EOL> def main ( args ) : <EOL> try : <EOL> return gyp_main ( args ) <EOL> except GypError , e : <EOL> sys . stderr . write ( "<STR_LIT>" % e ) <EOL> return <NUM_LIT:1> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( sys . argv [ <NUM_LIT:1> : ] ) ) </s>
