44# Use of this source code is governed by a BSD-style license that can be
55# found in the LICENSE file.
66
7+ from __future__ import print_function
8+
79import copy
810import gyp .input
9- import optparse
11+ import argparse
1012import os .path
1113import re
1214import shlex
1315import sys
1416import traceback
1517from gyp .common import GypError
1618
19+ try :
20+ # Python 2
21+ string_types = basestring
22+ except NameError :
23+ # Python 3
24+ string_types = str
25+
1726# Default debug modes for GYP
1827debug = {}
1928
@@ -34,8 +43,8 @@ def DebugOutput(mode, message, *args):
3443pass
3544if args :
3645message %= args
37- print '%s:%s:%d:%s %s' % (mode .upper (), os .path .basename (ctx [0 ]),
38- ctx [1 ], ctx [2 ], message )
46+ print ( '%s:%s:%d:%s %s' % (mode .upper (), os .path .basename (ctx [0 ]),
47+ ctx [1 ], ctx [2 ], message ))
3948
4049def FindBuildFiles ():
4150extension = '.gyp'
@@ -207,7 +216,7 @@ def Noop(value):
207216# We always want to ignore the environment when regenerating, to avoid
208217# duplicate or changed flags in the environment at the time of regeneration.
209218flags = ['--ignore-environment' ]
210- for name , metadata in options ._regeneration_metadata .iteritems ():
219+ for name , metadata in options ._regeneration_metadata .items ():
211220opt = metadata ['opt' ]
212221value = getattr (options , name )
213222value_predicate = metadata ['type' ] == 'path' and FixPath or Noop
@@ -226,24 +235,24 @@ def Noop(value):
226235 (action == 'store_false' and not value )):
227236flags .append (opt )
228237elif options .use_environment and env_name :
229- print >> sys . stderr , ('Warning: environment regeneration unimplemented '
238+ print ('Warning: environment regeneration unimplemented '
230239'for %s flag %r env_name %r' % (action , opt ,
231- env_name ))
240+ env_name ), file = sys . stderr )
232241else :
233- print >> sys . stderr , ('Warning: regeneration unimplemented for action %r '
234- 'flag %r' % (action , opt ))
242+ print ('Warning: regeneration unimplemented for action %r '
243+ 'flag %r' % (action , opt ), file = sys . stderr )
235244
236245return flags
237246
238- class RegeneratableOptionParser (optparse . OptionParser ):
239- def __init__ (self ):
247+ class RegeneratableOptionParser (argparse . ArgumentParser ):
248+ def __init__ (self , usage ):
240249self .__regeneratable_options = {}
241- optparse . OptionParser .__init__ (self )
250+ argparse . ArgumentParser .__init__ (self , usage = usage )
242251
243- def add_option (self , * args , ** kw ):
252+ def add_argument (self , * args , ** kw ):
244253"""Add an option to the parser.
245254
246- This accepts the same arguments as OptionParser.add_option , plus the
255+ This accepts the same arguments as ArgumentParser.add_argument , plus the
247256 following:
248257 regenerate: can be set to False to prevent this option from being included
249258 in regeneration.
@@ -260,7 +269,7 @@ def add_option(self, *args, **kw):
260269# it as a string.
261270type = kw .get ('type' )
262271if type == 'path' :
263- kw ['type' ] = 'string'
272+ kw ['type' ] = str
264273
265274self .__regeneratable_options [dest ] = {
266275'action' : kw .get ('action' ),
@@ -269,50 +278,50 @@ def add_option(self, *args, **kw):
269278'opt' : args [0 ],
270279 }
271280
272- optparse . OptionParser . add_option (self , * args , ** kw )
281+ argparse . ArgumentParser . add_argument (self , * args , ** kw )
273282
274283def parse_args (self , * args ):
275- values , args = optparse . OptionParser . parse_args (self , * args )
284+ values , args = argparse . ArgumentParser . parse_known_args (self , * args )
276285values ._regeneration_metadata = self .__regeneratable_options
277286return values , args
278287
279288def gyp_main (args ):
280289my_name = os .path .basename (sys .argv [0 ])
290+ usage = 'usage: %(prog)s [options ...] [build_file ...]'
291+
281292
282- parser = RegeneratableOptionParser ()
283- usage = 'usage: %s [options ...] [build_file ...]'
284- parser .set_usage (usage .replace ('%s' , '%prog' ))
285- parser .add_option ('--build' , dest = 'configs' , action = 'append' ,
293+ parser = RegeneratableOptionParser (usage = usage .replace ('%s' , '%(prog)s' ))
294+ parser .add_argument ('--build' , dest = 'configs' , action = 'append' ,
286295help = 'configuration for build after project generation' )
287- parser .add_option ('--check' , dest = 'check' , action = 'store_true' ,
296+ parser .add_argument ('--check' , dest = 'check' , action = 'store_true' ,
288297help = 'check format of gyp files' )
289- parser .add_option ('--config-dir' , dest = 'config_dir' , action = 'store' ,
298+ parser .add_argument ('--config-dir' , dest = 'config_dir' , action = 'store' ,
290299env_name = 'GYP_CONFIG_DIR' , default = None ,
291300help = 'The location for configuration files like '
292301'include.gypi.' )
293- parser .add_option ('-d' , '--debug' , dest = 'debug' , metavar = 'DEBUGMODE' ,
302+ parser .add_argument ('-d' , '--debug' , dest = 'debug' , metavar = 'DEBUGMODE' ,
294303action = 'append' , default = [], help = 'turn on a debugging '
295304'mode for debugging GYP. Supported modes are "variables", '
296305'"includes" and "general" or "all" for all of them.' )
297- parser .add_option ('-D' , dest = 'defines' , action = 'append' , metavar = 'VAR=VAL' ,
306+ parser .add_argument ('-D' , dest = 'defines' , action = 'append' , metavar = 'VAR=VAL' ,
298307env_name = 'GYP_DEFINES' ,
299308help = 'sets variable VAR to value VAL' )
300- parser .add_option ('--depth' , dest = 'depth' , metavar = 'PATH' , type = 'path' ,
309+ parser .add_argument ('--depth' , dest = 'depth' , metavar = 'PATH' , type = 'path' ,
301310help = 'set DEPTH gyp variable to a relative path to PATH' )
302- parser .add_option ('-f' , '--format' , dest = 'formats' , action = 'append' ,
311+ parser .add_argument ('-f' , '--format' , dest = 'formats' , action = 'append' ,
303312env_name = 'GYP_GENERATORS' , regenerate = False ,
304313help = 'output formats to generate' )
305- parser .add_option ('-G' , dest = 'generator_flags' , action = 'append' , default = [],
314+ parser .add_argument ('-G' , dest = 'generator_flags' , action = 'append' , default = [],
306315metavar = 'FLAG=VAL' , env_name = 'GYP_GENERATOR_FLAGS' ,
307316help = 'sets generator flag FLAG to VAL' )
308- parser .add_option ('--generator-output' , dest = 'generator_output' ,
317+ parser .add_argument ('--generator-output' , dest = 'generator_output' ,
309318action = 'store' , default = None , metavar = 'DIR' , type = 'path' ,
310319env_name = 'GYP_GENERATOR_OUTPUT' ,
311320help = 'puts generated build files under DIR' )
312- parser .add_option ('--ignore-environment' , dest = 'use_environment' ,
321+ parser .add_argument ('--ignore-environment' , dest = 'use_environment' ,
313322action = 'store_false' , default = True , regenerate = False ,
314323help = 'do not read options from environment variables' )
315- parser .add_option ('-I' , '--include' , dest = 'includes' , action = 'append' ,
324+ parser .add_argument ('-I' , '--include' , dest = 'includes' , action = 'append' ,
316325metavar = 'INCLUDE' , type = 'path' ,
317326help = 'files to include in all loaded .gyp files' )
318327# --no-circular-check disables the check for circular relationships between
@@ -322,7 +331,7 @@ def gyp_main(args):
322331# option allows the strict behavior to be used on Macs and the lenient
323332# behavior to be used elsewhere.
324333# TODO(mark): Remove this option when http://crbug.com/35878 is fixed.
325- parser .add_option ('--no-circular-check' , dest = 'circular_check' ,
334+ parser .add_argument ('--no-circular-check' , dest = 'circular_check' ,
326335action = 'store_false' , default = True , regenerate = False ,
327336help = "don't check for circular relationships between files" )
328337# --no-duplicate-basename-check disables the check for duplicate basenames
@@ -331,18 +340,18 @@ def gyp_main(args):
331340# when duplicate basenames are passed into Make generator on Mac.
332341# TODO(yukawa): Remove this option when these legacy generators are
333342# deprecated.
334- parser .add_option ('--no-duplicate-basename-check' ,
343+ parser .add_argument ('--no-duplicate-basename-check' ,
335344dest = 'duplicate_basename_check' , action = 'store_false' ,
336345default = True , regenerate = False ,
337346help = "don't check for duplicate basenames" )
338- parser .add_option ('--no-parallel' , action = 'store_true' , default = False ,
347+ parser .add_argument ('--no-parallel' , action = 'store_true' , default = False ,
339348help = 'Disable multiprocessing' )
340- parser .add_option ('-S' , '--suffix' , dest = 'suffix' , default = '' ,
349+ parser .add_argument ('-S' , '--suffix' , dest = 'suffix' , default = '' ,
341350help = 'suffix to add to generated files' )
342- parser .add_option ('--toplevel-dir' , dest = 'toplevel_dir' , action = 'store' ,
351+ parser .add_argument ('--toplevel-dir' , dest = 'toplevel_dir' , action = 'store' ,
343352default = None , metavar = 'DIR' , type = 'path' ,
344353help = 'directory to use as the root of the source tree' )
345- parser .add_option ('-R' , '--root-target' , dest = 'root_targets' ,
354+ parser .add_argument ('-R' , '--root-target' , dest = 'root_targets' ,
346355action = 'append' , metavar = 'TARGET' ,
347356help = 'include only TARGET and its deep dependencies' )
348357
@@ -410,7 +419,7 @@ def gyp_main(args):
410419for option , value in sorted (options .__dict__ .items ()):
411420if option [0 ] == '_' :
412421continue
413- if isinstance (value , basestring ):
422+ if isinstance (value , string_types ):
414423DebugOutput (DEBUG_GENERAL , " %s: '%s'" , option , value )
415424else :
416425DebugOutput (DEBUG_GENERAL , " %s: %s" , option , value )
@@ -432,7 +441,7 @@ def gyp_main(args):
432441build_file_dir = os .path .abspath (os .path .dirname (build_file ))
433442build_file_dir_components = build_file_dir .split (os .path .sep )
434443components_len = len (build_file_dir_components )
435- for index in xrange (components_len - 1 , - 1 , - 1 ):
444+ for index in range (components_len - 1 , - 1 , - 1 ):
436445if build_file_dir_components [index ] == 'src' :
437446options .depth = os .path .sep .join (build_file_dir_components )
438447break
@@ -475,7 +484,7 @@ def gyp_main(args):
475484if home_dot_gyp != None :
476485default_include = os .path .join (home_dot_gyp , 'include.gypi' )
477486if os .path .exists (default_include ):
478- print 'Using overrides found in ' + default_include
487+ print ( 'Using overrides found in ' + default_include )
479488includes .append (default_include )
480489
481490# Command-line --include files come after the default include.
@@ -536,7 +545,7 @@ def gyp_main(args):
536545def main (args ):
537546try :
538547return gyp_main (args )
539- except GypError , e :
548+ except GypError as e :
540549sys .stderr .write ("gyp: %s\n " % e )
541550return 1
542551
0 commit comments