Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some Python 3 fixes to node-gyp #1150

Merged
merged 6 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions gyp/PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@
def _LicenseHeader(input_api):
# Accept any year number from 2009 to the current year.
current_year = int(input_api.time.strftime('%Y'))
allowed_years = (str(s) for s in reversed(xrange(2009, current_year + 1)))

allowed_years = (str(s) for s in reversed(range(2009, current_year + 1)))
years_re = '(' + '|'.join(allowed_years) + ')'

# The (c) is deprecated, but tolerate it until it's removed from all files.
Expand Down
33 changes: 18 additions & 15 deletions gyp/pylib/gyp/MSVSSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
The MSBuild schemas were also considered. They are typically found in the
MSBuild install directory, e.g. c:\Program Files (x86)\MSBuild
"""

from __future__ import print_function

from gyp import string_types

import sys
import re

Expand Down Expand Up @@ -107,11 +110,11 @@ class _String(_Type):
"""A setting that's just a string."""

def ValidateMSVS(self, value):
if not isinstance(value, basestring):
if not isinstance(value, string_types):
raise ValueError('expected string; got %r' % value)

def ValidateMSBuild(self, value):
if not isinstance(value, basestring):
if not isinstance(value, string_types):
raise ValueError('expected string; got %r' % value)

def ConvertToMSBuild(self, value):
Expand All @@ -123,11 +126,11 @@ class _StringList(_Type):
"""A settings that's a list of strings."""

def ValidateMSVS(self, value):
if not isinstance(value, basestring) and not isinstance(value, list):
if not isinstance(value, string_types) and not isinstance(value, list):
raise ValueError('expected string list; got %r' % value)

def ValidateMSBuild(self, value):
if not isinstance(value, basestring) and not isinstance(value, list):
if not isinstance(value, string_types) and not isinstance(value, list):
raise ValueError('expected string list; got %r' % value)

def ConvertToMSBuild(self, value):
Expand Down Expand Up @@ -434,7 +437,7 @@ def ConvertVCMacrosToMSBuild(s):
'$(PlatformName)': '$(Platform)',
'$(SafeInputName)': '%(Filename)',
}
for old, new in replace_map.iteritems():
for old, new in replace_map.items():
s = s.replace(old, new)
s = FixVCMacroSlashes(s)
return s
Expand All @@ -454,17 +457,17 @@ def ConvertToMSBuildSettings(msvs_settings, stderr=sys.stderr):
dictionaries of settings and their values.
"""
msbuild_settings = {}
for msvs_tool_name, msvs_tool_settings in msvs_settings.iteritems():
for msvs_tool_name, msvs_tool_settings in msvs_settings.items():
if msvs_tool_name in _msvs_to_msbuild_converters:
msvs_tool = _msvs_to_msbuild_converters[msvs_tool_name]
for msvs_setting, msvs_value in msvs_tool_settings.iteritems():
for msvs_setting, msvs_value in msvs_tool_settings.items():
if msvs_setting in msvs_tool:
# Invoke the translation function.
try:
msvs_tool[msvs_setting](msvs_value, msbuild_settings)
except ValueError as e:
print(('Warning: while converting %s/%s to MSBuild, '
'%s' % (msvs_tool_name, msvs_setting, e)), file=stderr)
print('Warning: while converting %s/%s to MSBuild, '
'%s' % (msvs_tool_name, msvs_setting, e), file=stderr)
else:
_ValidateExclusionSetting(msvs_setting,
msvs_tool,
Expand All @@ -473,8 +476,8 @@ def ConvertToMSBuildSettings(msvs_settings, stderr=sys.stderr):
(msvs_tool_name, msvs_setting)),
stderr)
else:
print(('Warning: unrecognized tool %s while converting to '
'MSBuild.' % msvs_tool_name), file=stderr)
print('Warning: unrecognized tool %s while converting to '
'MSBuild.' % msvs_tool_name, file=stderr)
return msbuild_settings


Expand Down Expand Up @@ -514,13 +517,13 @@ def _ValidateSettings(validators, settings, stderr):
for tool_name in settings:
if tool_name in validators:
tool_validators = validators[tool_name]
for setting, value in settings[tool_name].iteritems():
for setting, value in settings[tool_name].items():
if setting in tool_validators:
try:
tool_validators[setting](value)
except ValueError as e:
print(('Warning: for %s/%s, %s' %
(tool_name, setting, e)), file=stderr)
print('Warning: for %s/%s, %s' %
(tool_name, setting, e), file=stderr)
else:
_ValidateExclusionSetting(setting,
tool_validators,
Expand All @@ -529,7 +532,7 @@ def _ValidateSettings(validators, settings, stderr):
stderr)

else:
print(('Warning: unrecognized tool %s' % tool_name), file=stderr)
print('Warning: unrecognized tool %s' % (tool_name), file=stderr)


# MSVS and MBuild names of the tools.
Expand Down
4 changes: 2 additions & 2 deletions gyp/pylib/gyp/MSVSUserFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def AddDebugSettings(self, config_name, command, environment = {},

if environment and isinstance(environment, dict):
env_list = ['%s="%s"' % (key, val)
for (key,val) in environment.iteritems()]
for (key,val) in environment.items()]
environment = ' '.join(env_list)
else:
environment = ''
Expand Down Expand Up @@ -135,7 +135,7 @@ def AddDebugSettings(self, config_name, command, environment = {},
def WriteIfChanged(self):
"""Writes the user file."""
configs = ['Configurations']
for config, spec in sorted(self.configurations.iteritems()):
for config, spec in sorted(self.configurations.items()):
configs.append(spec)

content = ['VisualStudioUserFile',
Expand Down
2 changes: 1 addition & 1 deletion gyp/pylib/gyp/MSVSUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def InsertLargePdbShims(target_list, target_dicts, vars):

# Set up the shim to output its PDB to the same location as the final linker
# target.
for config_name, config in shim_dict.get('configurations').iteritems():
for config_name, config in shim_dict.get('configurations').items():
pdb_path = _GetPdbPath(target_dict, config_name, vars)

# A few keys that we don't want to propagate.
Expand Down
12 changes: 9 additions & 3 deletions gyp/pylib/gyp/MSVSVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,18 @@ def _RegistryGetValueUsingWinReg(key, value):
contents of the registry key's value, or None on failure. Throws
ImportError if _winreg is unavailable.
"""
import _winreg
try:
# Python 2
from _winreg import OpenKey, QueryValueEx
except ImportError:
# Python 3
from winreg import OpenKey, QueryValueEx

try:
root, subkey = key.split('\\', 1)
assert root == 'HKLM' # Only need HKLM for now.
with _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
return _winreg.QueryValueEx(hkey, value)[0]
with OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
return QueryValueEx(hkey, value)[0]
except WindowsError:
return None

Expand Down
22 changes: 15 additions & 7 deletions gyp/pylib/gyp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# found in the LICENSE file.

from __future__ import print_function

import copy
import gyp.input
import optparse
Expand All @@ -15,6 +16,13 @@
import traceback
from gyp.common import GypError

try:
# Python 2
string_types = basestring
except NameError:
# Python 3
string_types = str

# Default debug modes for GYP
debug = {}

Expand Down Expand Up @@ -208,7 +216,7 @@ def Noop(value):
# We always want to ignore the environment when regenerating, to avoid
# duplicate or changed flags in the environment at the time of regeneration.
flags = ['--ignore-environment']
for name, metadata in options._regeneration_metadata.iteritems():
for name, metadata in options._regeneration_metadata.items():
opt = metadata['opt']
value = getattr(options, name)
value_predicate = metadata['type'] == 'path' and FixPath or Noop
Expand All @@ -227,12 +235,12 @@ def Noop(value):
(action == 'store_false' and not value)):
flags.append(opt)
elif options.use_environment and env_name:
print(('Warning: environment regeneration unimplemented '
print('Warning: environment regeneration unimplemented '
'for %s flag %r env_name %r' % (action, opt,
env_name)), file=sys.stderr)
env_name), file=sys.stderr)
else:
print(('Warning: regeneration unimplemented for action %r '
'flag %r' % (action, opt)), file=sys.stderr)
print('Warning: regeneration unimplemented for action %r '
'flag %r' % (action, opt), file=sys.stderr)

return flags

Expand Down Expand Up @@ -411,7 +419,7 @@ def gyp_main(args):
for option, value in sorted(options.__dict__.items()):
if option[0] == '_':
continue
if isinstance(value, basestring):
if isinstance(value, string_types):
DebugOutput(DEBUG_GENERAL, " %s: '%s'", option, value)
else:
DebugOutput(DEBUG_GENERAL, " %s: %s", option, value)
Expand All @@ -433,7 +441,7 @@ def gyp_main(args):
build_file_dir = os.path.abspath(os.path.dirname(build_file))
build_file_dir_components = build_file_dir.split(os.path.sep)
components_len = len(build_file_dir_components)
for index in xrange(components_len - 1, -1, -1):
for index in range(components_len - 1, -1, -1):
if build_file_dir_components[index] == 'src':
options.depth = os.path.sep.join(build_file_dir_components)
break
Expand Down
2 changes: 1 addition & 1 deletion gyp/pylib/gyp/easy_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _ConstructContentList(xml_parts, specification, pretty, level=0):
# Optionally in second position is a dictionary of the attributes.
rest = specification[1:]
if rest and isinstance(rest[0], dict):
for at, val in sorted(rest[0].iteritems()):
for at, val in sorted(rest[0].items()):
xml_parts.append(' %s="%s"' % (at, _XmlEscape(val, attr=True)))
rest = rest[1:]
if rest:
Expand Down
15 changes: 8 additions & 7 deletions gyp/pylib/gyp/generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
directly supplied to gyp. OTOH if both "a.gyp" and "b.gyp" are supplied to gyp
then the "all" target includes "b1" and "b2".
"""

from __future__ import print_function

import gyp.common
Expand Down Expand Up @@ -292,8 +293,8 @@ def _WasBuildFileModified(build_file, data, files, toplevel_dir):
_ToGypPath(gyp.common.UnrelativePath(include_file, build_file))
if _ToLocalPath(toplevel_dir, rel_include_file) in files:
if debug:
print('included gyp file modified, gyp_file=', build_file, \
'included file=', rel_include_file)
print('included gyp file modified, gyp_file=', build_file,
'included file=', rel_include_file)
return True
return False

Expand Down Expand Up @@ -485,11 +486,11 @@ def _AddCompileTargets(target, roots, add_if_no_ancestor, result):
(add_if_no_ancestor or target.requires_build)) or
(target.is_static_library and add_if_no_ancestor and
not target.is_or_has_linked_ancestor)):
print('\t\tadding to compile targets', target.name, 'executable', \
target.is_executable, 'added_to_compile_targets', \
target.added_to_compile_targets, 'add_if_no_ancestor', \
add_if_no_ancestor, 'requires_build', target.requires_build, \
'is_static_library', target.is_static_library, \
print('\t\tadding to compile targets', target.name, 'executable',
target.is_executable, 'added_to_compile_targets',
target.added_to_compile_targets, 'add_if_no_ancestor',
add_if_no_ancestor, 'requires_build', target.requires_build,
'is_static_library', target.is_static_library,
'is_or_has_linked_ancestor', target.is_or_has_linked_ancestor)
result.add(target)
target.added_to_compile_targets = True
Expand Down
21 changes: 11 additions & 10 deletions gyp/pylib/gyp/generator/android.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import print_function
# Copyright (c) 2012 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
Expand All @@ -15,6 +14,8 @@
# variables set potentially clash with other Android build system variables.
# Try to avoid setting global variables where possible.

from __future__ import print_function

import gyp
import gyp.common
import gyp.generator.make as make # Reuse global functions from make backend.
Expand Down Expand Up @@ -251,7 +252,7 @@ def WriteActions(self, actions, extra_sources, extra_outputs):
dirs = set()
for out in outputs:
if not out.startswith('$'):
print ('WARNING: Action for target "%s" writes output to local path '
print('WARNING: Action for target "%s" writes output to local path '
'"%s".' % (self.target, out))
dir = os.path.split(out)[0]
if dir:
Expand Down Expand Up @@ -356,7 +357,7 @@ def WriteRules(self, rules, extra_sources, extra_outputs):
dirs = set()
for out in outputs:
if not out.startswith('$'):
print ('WARNING: Rule for target %s writes output to local path %s'
print('WARNING: Rule for target %s writes output to local path %s'
% (self.target, out))
dir = os.path.dirname(out)
if dir:
Expand Down Expand Up @@ -430,7 +431,7 @@ def WriteCopies(self, copies, extra_outputs):
# $(gyp_shared_intermediate_dir). Note that we can't use an assertion
# because some of the gyp tests depend on this.
if not copy['destination'].startswith('$'):
print ('WARNING: Copy rule for target %s writes output to '
print('WARNING: Copy rule for target %s writes output to '
'local path %s' % (self.target, copy['destination']))

# LocalPathify() calls normpath, stripping trailing slashes.
Expand Down Expand Up @@ -459,7 +460,7 @@ def WriteSourceFlags(self, spec, configs):
Args:
spec, configs: input from gyp.
"""
for configname, config in sorted(configs.iteritems()):
for configname, config in sorted(configs.items()):
extracted_includes = []

self.WriteLn('\n# Flags passed to both C and C++ files.')
Expand Down Expand Up @@ -637,8 +638,8 @@ def ComputeOutputParts(self, spec):
elif self.type == 'none':
target_ext = '.stamp'
elif self.type != 'executable':
print(("ERROR: What output file should be generated?",
"type", self.type, "target", target))
print("ERROR: What output file should be generated?",
"type", self.type, "target", target)

if self.type != 'static_library' and self.type != 'shared_library':
target_prefix = spec.get('product_prefix', target_prefix)
Expand Down Expand Up @@ -789,7 +790,7 @@ def WriteTargetFlags(self, spec, configs, link_deps):
static_libs, dynamic_libs, ldflags_libs = self.FilterLibraries(libraries)

if self.type != 'static_library':
for configname, config in sorted(configs.iteritems()):
for configname, config in sorted(configs.items()):
ldflags = list(config.get('ldflags', []))
self.WriteLn('')
self.WriteList(ldflags, 'LOCAL_LDFLAGS_%s' % configname)
Expand Down Expand Up @@ -838,7 +839,7 @@ def WriteTarget(self, spec, configs, deps, link_deps, part_of_all,
settings = spec.get('aosp_build_settings', {})
if settings:
self.WriteLn('### Set directly by aosp_build_settings.')
for k, v in settings.iteritems():
for k, v in settings.items():
if isinstance(v, list):
self.WriteList(v, k)
else:
Expand Down Expand Up @@ -1066,7 +1067,7 @@ def CalculateMakefilePath(build_file, base_name):
write_alias_target=write_alias_targets,
sdk_version=sdk_version)
if android_module in android_modules:
print ('ERROR: Android module names must be unique. The following '
print('ERROR: Android module names must be unique. The following '
'targets both generate Android module name %s.\n %s\n %s' %
(android_module, android_modules[android_module],
qualified_target))
Expand Down
9 changes: 5 additions & 4 deletions gyp/pylib/gyp/generator/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
not be able to find the header file directories described in the generated
CMakeLists.txt file.
"""

from __future__ import print_function

import multiprocessing
Expand Down Expand Up @@ -640,8 +641,8 @@ def WriteTarget(namer, qualified_target, target_dicts, build_dir, config_to_use,

cmake_target_type = cmake_target_type_from_gyp_target_type.get(target_type)
if cmake_target_type is None:
print ('Target %s has unknown target type %s, skipping.' %
( target_name, target_type ) )
print('Target %s has unknown target type %s, skipping.' %
( target_name, target_type ))
return

SetVariable(output, 'TARGET', target_name)
Expand Down Expand Up @@ -864,8 +865,8 @@ def WriteTarget(namer, qualified_target, target_dicts, build_dir, config_to_use,
default_product_ext = generator_default_variables['SHARED_LIB_SUFFIX']

elif target_type != 'executable':
print(('ERROR: What output file should be generated?',
'type', target_type, 'target', target_name))
print('ERROR: What output file should be generated?',
'type', target_type, 'target', target_name)

product_prefix = spec.get('product_prefix', default_product_prefix)
product_name = spec.get('product_name', default_product_name)
Expand Down
Loading