4
4
# Use of this source code is governed by a BSD-style license that can be
5
5
# found in the LICENSE file.
6
6
7
+ from __future__ import print_function
8
+
7
9
import copy
8
10
import gyp .input
9
- import optparse
11
+ import argparse
10
12
import os .path
11
13
import re
12
14
import shlex
13
15
import sys
14
16
import traceback
15
17
from gyp .common import GypError
16
18
19
+ try :
20
+ # Python 2
21
+ string_types = basestring
22
+ except NameError :
23
+ # Python 3
24
+ string_types = str
25
+
17
26
# Default debug modes for GYP
18
27
debug = {}
19
28
@@ -34,8 +43,8 @@ def DebugOutput(mode, message, *args):
34
43
pass
35
44
if args :
36
45
message %= 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 ))
39
48
40
49
def FindBuildFiles ():
41
50
extension = '.gyp'
@@ -207,7 +216,7 @@ def Noop(value):
207
216
# We always want to ignore the environment when regenerating, to avoid
208
217
# duplicate or changed flags in the environment at the time of regeneration.
209
218
flags = ['--ignore-environment' ]
210
- for name , metadata in options ._regeneration_metadata .iteritems ():
219
+ for name , metadata in options ._regeneration_metadata .items ():
211
220
opt = metadata ['opt' ]
212
221
value = getattr (options , name )
213
222
value_predicate = metadata ['type' ] == 'path' and FixPath or Noop
@@ -226,24 +235,24 @@ def Noop(value):
226
235
(action == 'store_false' and not value )):
227
236
flags .append (opt )
228
237
elif options .use_environment and env_name :
229
- print >> sys . stderr , ('Warning: environment regeneration unimplemented '
238
+ print ('Warning: environment regeneration unimplemented '
230
239
'for %s flag %r env_name %r' % (action , opt ,
231
- env_name ))
240
+ env_name ), file = sys . stderr )
232
241
else :
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 )
235
244
236
245
return flags
237
246
238
- class RegeneratableOptionParser (optparse . OptionParser ):
239
- def __init__ (self ):
247
+ class RegeneratableOptionParser (argparse . ArgumentParser ):
248
+ def __init__ (self , usage ):
240
249
self .__regeneratable_options = {}
241
- optparse . OptionParser .__init__ (self )
250
+ argparse . ArgumentParser .__init__ (self , usage = usage )
242
251
243
- def add_option (self , * args , ** kw ):
252
+ def add_argument (self , * args , ** kw ):
244
253
"""Add an option to the parser.
245
254
246
- This accepts the same arguments as OptionParser.add_option , plus the
255
+ This accepts the same arguments as ArgumentParser.add_argument , plus the
247
256
following:
248
257
regenerate: can be set to False to prevent this option from being included
249
258
in regeneration.
@@ -260,7 +269,7 @@ def add_option(self, *args, **kw):
260
269
# it as a string.
261
270
type = kw .get ('type' )
262
271
if type == 'path' :
263
- kw ['type' ] = 'string'
272
+ kw ['type' ] = str
264
273
265
274
self .__regeneratable_options [dest ] = {
266
275
'action' : kw .get ('action' ),
@@ -269,50 +278,50 @@ def add_option(self, *args, **kw):
269
278
'opt' : args [0 ],
270
279
}
271
280
272
- optparse . OptionParser . add_option (self , * args , ** kw )
281
+ argparse . ArgumentParser . add_argument (self , * args , ** kw )
273
282
274
283
def parse_args (self , * args ):
275
- values , args = optparse . OptionParser . parse_args (self , * args )
284
+ values , args = argparse . ArgumentParser . parse_known_args (self , * args )
276
285
values ._regeneration_metadata = self .__regeneratable_options
277
286
return values , args
278
287
279
288
def gyp_main (args ):
280
289
my_name = os .path .basename (sys .argv [0 ])
290
+ usage = 'usage: %(prog)s [options ...] [build_file ...]'
291
+
281
292
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' ,
286
295
help = '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' ,
288
297
help = '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' ,
290
299
env_name = 'GYP_CONFIG_DIR' , default = None ,
291
300
help = 'The location for configuration files like '
292
301
'include.gypi.' )
293
- parser .add_option ('-d' , '--debug' , dest = 'debug' , metavar = 'DEBUGMODE' ,
302
+ parser .add_argument ('-d' , '--debug' , dest = 'debug' , metavar = 'DEBUGMODE' ,
294
303
action = 'append' , default = [], help = 'turn on a debugging '
295
304
'mode for debugging GYP. Supported modes are "variables", '
296
305
'"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' ,
298
307
env_name = 'GYP_DEFINES' ,
299
308
help = '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' ,
301
310
help = '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' ,
303
312
env_name = 'GYP_GENERATORS' , regenerate = False ,
304
313
help = '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 = [],
306
315
metavar = 'FLAG=VAL' , env_name = 'GYP_GENERATOR_FLAGS' ,
307
316
help = 'sets generator flag FLAG to VAL' )
308
- parser .add_option ('--generator-output' , dest = 'generator_output' ,
317
+ parser .add_argument ('--generator-output' , dest = 'generator_output' ,
309
318
action = 'store' , default = None , metavar = 'DIR' , type = 'path' ,
310
319
env_name = 'GYP_GENERATOR_OUTPUT' ,
311
320
help = 'puts generated build files under DIR' )
312
- parser .add_option ('--ignore-environment' , dest = 'use_environment' ,
321
+ parser .add_argument ('--ignore-environment' , dest = 'use_environment' ,
313
322
action = 'store_false' , default = True , regenerate = False ,
314
323
help = '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' ,
316
325
metavar = 'INCLUDE' , type = 'path' ,
317
326
help = 'files to include in all loaded .gyp files' )
318
327
# --no-circular-check disables the check for circular relationships between
@@ -322,7 +331,7 @@ def gyp_main(args):
322
331
# option allows the strict behavior to be used on Macs and the lenient
323
332
# behavior to be used elsewhere.
324
333
# 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' ,
326
335
action = 'store_false' , default = True , regenerate = False ,
327
336
help = "don't check for circular relationships between files" )
328
337
# --no-duplicate-basename-check disables the check for duplicate basenames
@@ -331,18 +340,18 @@ def gyp_main(args):
331
340
# when duplicate basenames are passed into Make generator on Mac.
332
341
# TODO(yukawa): Remove this option when these legacy generators are
333
342
# deprecated.
334
- parser .add_option ('--no-duplicate-basename-check' ,
343
+ parser .add_argument ('--no-duplicate-basename-check' ,
335
344
dest = 'duplicate_basename_check' , action = 'store_false' ,
336
345
default = True , regenerate = False ,
337
346
help = "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 ,
339
348
help = 'Disable multiprocessing' )
340
- parser .add_option ('-S' , '--suffix' , dest = 'suffix' , default = '' ,
349
+ parser .add_argument ('-S' , '--suffix' , dest = 'suffix' , default = '' ,
341
350
help = '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' ,
343
352
default = None , metavar = 'DIR' , type = 'path' ,
344
353
help = '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' ,
346
355
action = 'append' , metavar = 'TARGET' ,
347
356
help = 'include only TARGET and its deep dependencies' )
348
357
@@ -410,7 +419,7 @@ def gyp_main(args):
410
419
for option , value in sorted (options .__dict__ .items ()):
411
420
if option [0 ] == '_' :
412
421
continue
413
- if isinstance (value , basestring ):
422
+ if isinstance (value , string_types ):
414
423
DebugOutput (DEBUG_GENERAL , " %s: '%s'" , option , value )
415
424
else :
416
425
DebugOutput (DEBUG_GENERAL , " %s: %s" , option , value )
@@ -432,7 +441,7 @@ def gyp_main(args):
432
441
build_file_dir = os .path .abspath (os .path .dirname (build_file ))
433
442
build_file_dir_components = build_file_dir .split (os .path .sep )
434
443
components_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 ):
436
445
if build_file_dir_components [index ] == 'src' :
437
446
options .depth = os .path .sep .join (build_file_dir_components )
438
447
break
@@ -475,7 +484,7 @@ def gyp_main(args):
475
484
if home_dot_gyp != None :
476
485
default_include = os .path .join (home_dot_gyp , 'include.gypi' )
477
486
if os .path .exists (default_include ):
478
- print 'Using overrides found in ' + default_include
487
+ print ( 'Using overrides found in ' + default_include )
479
488
includes .append (default_include )
480
489
481
490
# Command-line --include files come after the default include.
@@ -536,7 +545,7 @@ def gyp_main(args):
536
545
def main (args ):
537
546
try :
538
547
return gyp_main (args )
539
- except GypError , e :
548
+ except GypError as e :
540
549
sys .stderr .write ("gyp: %s\n " % e )
541
550
return 1
542
551
0 commit comments