Skip to content

Commit b1db810

Browse files
cclausstargos
authored andcommitted
gyp: pull Python 3 changes from node/node-gyp
PR-URL: #28573 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 44de431 commit b1db810

16 files changed

Lines changed: 218 additions & 180 deletions

‎tools/gyp/PRESUBMIT.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@
7676
def_LicenseHeader(input_api):
7777
# Accept any year number from 2009 to the current year.
7878
current_year=int(input_api.time.strftime('%Y'))
79-
allowed_years= (str(s) forsinreversed(xrange(2009, current_year+1)))
80-
79+
allowed_years= (str(s) forsinreversed(range(2009, current_year+1)))
8180
years_re='('+'|'.join(allowed_years) +')'
8281

8382
# The (c) is deprecated, but tolerate it until it's removed from all files.
@@ -124,3 +123,16 @@ def CheckChangeOnCommit(input_api, output_api):
124123
finally:
125124
sys.path=old_sys_path
126125
returnreport
126+
127+
128+
TRYBOTS= [
129+
'linux_try',
130+
'mac_try',
131+
'win_try',
132+
]
133+
134+
135+
defGetPreferredTryMasters(_, change):
136+
return {
137+
'client.gyp': { t: set(['defaulttests']) fortinTRYBOTS },
138+
}

‎tools/gyp/pylib/gyp/MSVSNew.py‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@
44

55
"""New implementation of Visual Studio project generation."""
66

7+
importhashlib
78
importos
89
importrandom
910

1011
importgyp.common
1112

12-
# hashlib is supplied as of Python 2.5 as the replacement interface for md5
13-
# and other secure hashes. In 2.6, md5 is deprecated. Import hashlib if
14-
# available, avoiding a deprecation warning under 2.6. Import md5 otherwise,
15-
# preserving 2.4 compatibility.
1613
try:
17-
importhashlib
18-
_new_md5=hashlib.md5
19-
exceptImportError:
20-
importmd5
21-
_new_md5=md5.new
22-
14+
cmp
15+
exceptNameError:
16+
defcmp(x, y):
17+
return (x>y) - (x<y)
2318

2419
# Initialize random number generator
2520
random.seed()
@@ -50,7 +45,7 @@ def MakeGuid(name, seed='msvs_new'):
5045
not change when the project for a target is rebuilt.
5146
"""
5247
# Calculate a MD5 signature for the seed and name.
53-
d=_new_md5(str(seed) +str(name)).hexdigest().upper()
48+
d=hashlib.md5(str(seed) +str(name)).hexdigest().upper()
5449
# Convert most of the signature to GUID form (discard the rest)
5550
guid= ('{'+d[:8] +'-'+d[8:12] +'-'+d[12:16] +'-'+d[16:20]
5651
+'-'+d[20:32] +'}')

‎tools/gyp/pylib/gyp/MSVSUserFile.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def AddDebugSettings(self, config_name, command, environment = {},
9191

9292
ifenvironmentandisinstance(environment, dict):
9393
env_list= ['%s="%s"'% (key, val)
94-
for (key,val) inenvironment.iteritems()]
94+
for (key,val) inenvironment.items()]
9595
environment=' '.join(env_list)
9696
else:
9797
environment=''
@@ -135,7 +135,7 @@ def AddDebugSettings(self, config_name, command, environment = {},
135135
defWriteIfChanged(self):
136136
"""Writes the user file."""
137137
configs= ['Configurations']
138-
forconfig, specinsorted(self.configurations.iteritems()):
138+
forconfig, specinsorted(self.configurations.items()):
139139
configs.append(spec)
140140

141141
content= ['VisualStudioUserFile',

‎tools/gyp/pylib/gyp/__init__.py‎

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
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__ importprint_function
8+
79
importcopy
810
importgyp.input
9-
importoptparse
11+
importargparse
1012
importos.path
1113
importre
1214
importshlex
1315
importsys
1416
importtraceback
1517
fromgyp.commonimportGypError
1618

19+
try:
20+
# Python 2
21+
string_types=basestring
22+
exceptNameError:
23+
# Python 3
24+
string_types=str
25+
1726
# Default debug modes for GYP
1827
debug= {}
1928

@@ -34,8 +43,8 @@ def DebugOutput(mode, message, *args):
3443
pass
3544
ifargs:
3645
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))
3948

4049
defFindBuildFiles():
4150
extension='.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.
209218
flags= ['--ignore-environment']
210-
forname, metadatainoptions._regeneration_metadata.iteritems():
219+
forname, metadatainoptions._regeneration_metadata.items():
211220
opt=metadata['opt']
212221
value=getattr(options, name)
213222
value_predicate=metadata['type'] =='path'andFixPathorNoop
@@ -226,24 +235,24 @@ def Noop(value):
226235
(action=='store_false'andnotvalue)):
227236
flags.append(opt)
228237
elifoptions.use_environmentandenv_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)
232241
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)
235244

236245
returnflags
237246

238-
classRegeneratableOptionParser(optparse.OptionParser):
239-
def__init__(self):
247+
classRegeneratableOptionParser(argparse.ArgumentParser):
248+
def__init__(self, usage):
240249
self.__regeneratable_options= {}
241-
optparse.OptionParser.__init__(self)
250+
argparse.ArgumentParser.__init__(self, usage=usage)
242251

243-
defadd_option(self, *args, **kw):
252+
defadd_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.
261270
type=kw.get('type')
262271
iftype=='path':
263-
kw['type'] ='string'
272+
kw['type'] =str
264273

265274
self.__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

274283
defparse_args(self, *args):
275-
values, args=optparse.OptionParser.parse_args(self, *args)
284+
values, args=argparse.ArgumentParser.parse_known_args(self, *args)
276285
values._regeneration_metadata=self.__regeneratable_options
277286
returnvalues, args
278287

279288
defgyp_main(args):
280289
my_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',
286295
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',
288297
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',
290299
env_name='GYP_CONFIG_DIR', default=None,
291300
help='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',
294303
action='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',
298307
env_name='GYP_DEFINES',
299308
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',
301310
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',
303312
env_name='GYP_GENERATORS', regenerate=False,
304313
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=[],
306315
metavar='FLAG=VAL', env_name='GYP_GENERATOR_FLAGS',
307316
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',
309318
action='store', default=None, metavar='DIR', type='path',
310319
env_name='GYP_GENERATOR_OUTPUT',
311320
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',
313322
action='store_false', default=True, regenerate=False,
314323
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',
316325
metavar='INCLUDE', type='path',
317326
help='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',
326335
action='store_false', default=True, regenerate=False,
327336
help="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',
335344
dest='duplicate_basename_check', action='store_false',
336345
default=True, regenerate=False,
337346
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,
339348
help='Disable multiprocessing')
340-
parser.add_option('-S', '--suffix', dest='suffix', default='',
349+
parser.add_argument('-S', '--suffix', dest='suffix', default='',
341350
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',
343352
default=None, metavar='DIR', type='path',
344353
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',
346355
action='append', metavar='TARGET',
347356
help='include only TARGET and its deep dependencies')
348357

@@ -410,7 +419,7 @@ def gyp_main(args):
410419
foroption, valueinsorted(options.__dict__.items()):
411420
ifoption[0] =='_':
412421
continue
413-
ifisinstance(value, basestring):
422+
ifisinstance(value, string_types):
414423
DebugOutput(DEBUG_GENERAL, " %s: '%s'", option, value)
415424
else:
416425
DebugOutput(DEBUG_GENERAL, " %s: %s", option, value)
@@ -432,7 +441,7 @@ def gyp_main(args):
432441
build_file_dir=os.path.abspath(os.path.dirname(build_file))
433442
build_file_dir_components=build_file_dir.split(os.path.sep)
434443
components_len=len(build_file_dir_components)
435-
forindexinxrange(components_len-1, -1, -1):
444+
forindexinrange(components_len-1, -1, -1):
436445
ifbuild_file_dir_components[index] =='src':
437446
options.depth=os.path.sep.join(build_file_dir_components)
438447
break
@@ -475,7 +484,7 @@ def gyp_main(args):
475484
ifhome_dot_gyp!=None:
476485
default_include=os.path.join(home_dot_gyp, 'include.gypi')
477486
ifos.path.exists(default_include):
478-
print'Using overrides found in '+default_include
487+
print('Using overrides found in '+default_include)
479488
includes.append(default_include)
480489

481490
# Command-line --include files come after the default include.
@@ -536,7 +545,7 @@ def gyp_main(args):
536545
defmain(args):
537546
try:
538547
returngyp_main(args)
539-
exceptGypError,e:
548+
exceptGypErrorase:
540549
sys.stderr.write("gyp: %s\n"%e)
541550
return1
542551

‎tools/gyp/pylib/gyp/flock_tool.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def ExecFlock(self, lockfile, *cmd_list):
3939
# where fcntl.flock(fd, LOCK_EX) always fails
4040
# with EBADF, that's why we use this F_SETLK
4141
# hack instead.
42-
fd=os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0666)
42+
fd=os.open(lockfile, os.O_WRONLY|os.O_NOCTTY|os.O_CREAT, 0o666)
4343
ifsys.platform.startswith('aix'):
4444
# Python on AIX is compiled with LARGEFILE support, which changes the
4545
# struct size.

0 commit comments

Comments
 (0)