Skip to content

Commit

Permalink
Mostly mechanical changes for Python3 support.
Browse files Browse the repository at this point in the history
Everything should still work fine in Python 2. With this change (and
the others I've just landed), Python 3 should work on at least
Linux, but I need to do more sanity-checking on Mac and Win.

The changes in this CL are all mechanical -- things like
print() instead of print, `key in dict` rather than dict.has_key(key),
and switching to use .decode('utf8') to handle the binary streams
returned from a subprocess (and a few other things).

Most of this work is derived from [email protected]'s
original work in https://codereview.chromium.org/1454433002/.

Bug: gyp:36

Change-Id: Ie04ebcf2d82e7b8ff34c6a112215eac46af688ba
Reviewed-on: https://chromium-review.googlesource.com/c/1357805
Reviewed-by: Mark Mentovai <[email protected]>
  • Loading branch information
dpranke committed Dec 4, 2018
1 parent f2dca32 commit ab4aca8
Show file tree
Hide file tree
Showing 169 changed files with 1,086 additions and 1,130 deletions.
3 changes: 1 addition & 2 deletions 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GYP can Generate Your Projects.
===================================

Documents are available at [gyp.gsrc.io](https://gyp.gsrc.io), or you can check out ```md-pages``` branch to read those documents offline.
Documents are available at [gyp.gsrc.io](https://gyp.gsrc.io), or you can
check out ```md-pages``` branch to read those documents offline.
24 changes: 13 additions & 11 deletions buildbot/buildbot_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"""Argument-less script to select what to run on the buildbots."""

from __future__ import print_function

import os
import shutil
import subprocess
Expand All @@ -24,25 +26,25 @@ def CallSubProcess(*args, **kwargs):
with open(os.devnull) as devnull_fd:
retcode = subprocess.call(stdin=devnull_fd, *args, **kwargs)
if retcode != 0:
print '@@@STEP_EXCEPTION@@@'
print('@@@STEP_EXCEPTION@@@')
sys.exit(1)


def PrepareCmake():
"""Build CMake 2.8.8 since the version in Precise is 2.8.7."""
if os.environ['BUILDBOT_CLOBBER'] == '1':
print '@@@BUILD_STEP Clobber CMake checkout@@@'
print('@@@BUILD_STEP Clobber CMake checkout@@@')
shutil.rmtree(CMAKE_DIR)

# We always build CMake 2.8.8, so no need to do anything
# if the directory already exists.
if os.path.isdir(CMAKE_DIR):
return

print '@@@BUILD_STEP Initialize CMake checkout@@@'
print('@@@BUILD_STEP Initialize CMake checkout@@@')
os.mkdir(CMAKE_DIR)

print '@@@BUILD_STEP Sync CMake@@@'
print('@@@BUILD_STEP Sync CMake@@@')
CallSubProcess(
['git', 'clone',
'--depth', '1',
Expand All @@ -53,7 +55,7 @@ def PrepareCmake():
CMAKE_DIR],
cwd=CMAKE_DIR)

print '@@@BUILD_STEP Build CMake@@@'
print('@@@BUILD_STEP Build CMake@@@')
CallSubProcess(
['/bin/bash', 'bootstrap', '--prefix=%s' % CMAKE_DIR],
cwd=CMAKE_DIR)
Expand All @@ -74,7 +76,7 @@ def GypTestFormat(title, format=None, msvs_version=None, tests=[]):
if not format:
format = title

print '@@@BUILD_STEP ' + title + '@@@'
print('@@@BUILD_STEP ' + title + '@@@')
sys.stdout.flush()
env = os.environ.copy()
if msvs_version:
Expand All @@ -89,17 +91,17 @@ def GypTestFormat(title, format=None, msvs_version=None, tests=[]):
retcode = subprocess.call(command, cwd=ROOT_DIR, env=env, shell=True)
if retcode:
# Emit failure tag, and keep going.
print '@@@STEP_FAILURE@@@'
print('@@@STEP_FAILURE@@@')
return 1
return 0


def GypBuild():
# Dump out/ directory.
print '@@@BUILD_STEP cleanup@@@'
print 'Removing %s...' % OUT_DIR
print('@@@BUILD_STEP cleanup@@@')
print('Removing %s...' % OUT_DIR)
shutil.rmtree(OUT_DIR, ignore_errors=True)
print 'Done.'
print('Done.')

retcode = 0
if sys.platform.startswith('linux'):
Expand Down Expand Up @@ -128,7 +130,7 @@ def GypBuild():
# after the build proper that could be used for cumulative failures),
# use that instead of this. This isolates the final return value so
# that it isn't misattributed to the last stage.
print '@@@BUILD_STEP failures@@@'
print('@@@BUILD_STEP failures@@@')
sys.exit(retcode)


Expand Down
37 changes: 23 additions & 14 deletions pylib/gyp/MSVSSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
MSBuild install directory, e.g. c:\Program Files (x86)\MSBuild
"""

from __future__ import print_function

import sys
import re

try:
# basestring was removed in python3.
basestring
except NameError:
basestring = str

# Dictionaries of settings validators. The key is the tool name, the value is
# a dictionary mapping setting names to validation functions.
_msvs_validators = {}
Expand Down Expand Up @@ -400,7 +408,7 @@ def _ValidateExclusionSetting(setting, settings, error_msg, stderr=sys.stderr):

if unrecognized:
# We don't know this setting. Give a warning.
print >> stderr, error_msg
print(error_msg, file=stderr)


def FixVCMacroSlashes(s):
Expand Down Expand Up @@ -433,7 +441,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 @@ -453,17 +461,18 @@ 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, e:
print >> stderr, ('Warning: while converting %s/%s to MSBuild, '
'%s' % (msvs_tool_name, msvs_setting, e))
except ValueError as e:
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 @@ -472,8 +481,8 @@ def ConvertToMSBuildSettings(msvs_settings, stderr=sys.stderr):
(msvs_tool_name, msvs_setting)),
stderr)
else:
print >> stderr, ('Warning: unrecognized tool %s while converting to '
'MSBuild.' % msvs_tool_name)
print(('Warning: unrecognized tool %s while converting to '
'MSBuild.' % msvs_tool_name), file=stderr)
return msbuild_settings


Expand Down Expand Up @@ -513,13 +522,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, e:
print >> stderr, ('Warning: for %s/%s, %s' %
(tool_name, setting, e))
except ValueError as e:
print(('Warning: for %s/%s, %s' %
(tool_name, setting, e)), file=stderr)
else:
_ValidateExclusionSetting(setting,
tool_validators,
Expand All @@ -528,7 +537,7 @@ def _ValidateSettings(validators, settings, stderr):
stderr)

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


# MSVS and MBuild names of the tools.
Expand Down
7 changes: 5 additions & 2 deletions pylib/gyp/MSVSSettings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

"""Unit tests for the MSVSSettings.py file."""

import StringIO
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import unittest
import gyp.MSVSSettings as MSVSSettings


class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.stderr = StringIO.StringIO()
self.stderr = StringIO()

def _ExpectedWarnings(self, expected):
"""Compares recorded lines to expected warnings."""
Expand Down
4 changes: 2 additions & 2 deletions 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 pylib/gyp/MSVSUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,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
11 changes: 7 additions & 4 deletions pylib/gyp/MSVSVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _RegistryQuery(key, value=None):
text = None
try:
text = _RegistryQueryBase('Sysnative', key, value)
except OSError, e:
except OSError as e:
if e.errno == errno.ENOENT:
text = _RegistryQueryBase('System32', key, value)
else:
Expand All @@ -207,12 +207,15 @@ def _RegistryGetValueUsingWinReg(key, value):
contents of the registry key's value, or None on failure. Throws
ImportError if _winreg is unavailable.
"""
import _winreg
try:
import _winreg as winreg
except ImportError:
import winreg
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 winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
return winreg.QueryValueEx(hkey, value)[0]
except WindowsError:
return None

Expand Down
39 changes: 23 additions & 16 deletions pylib/gyp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import print_function

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

try:
# basestring was removed in python3.
basestring
except NameError:
basestring = str

# Default debug modes for GYP
debug = {}

Expand All @@ -22,7 +30,6 @@
DEBUG_VARIABLES = 'variables'
DEBUG_INCLUDES = 'includes'


def DebugOutput(mode, message, *args):
if 'all' in gyp.debug or mode in gyp.debug:
ctx = ('unknown', 0, 'unknown')
Expand All @@ -34,8 +41,8 @@ def DebugOutput(mode, message, *args):
pass
if args:
message %= args
print '%s:%s:%d:%s %s' % (mode.upper(), os.path.basename(ctx[0]),
ctx[1], ctx[2], message)
print('%s:%s:%d:%s %s' % (mode.upper(), os.path.basename(ctx[0]),
ctx[1], ctx[2], message))

def FindBuildFiles():
extension = '.gyp'
Expand Down Expand Up @@ -207,7 +214,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 @@ -226,12 +233,13 @@ def Noop(value):
(action == 'store_false' and not value)):
flags.append(opt)
elif options.use_environment and env_name:
print >>sys.stderr, ('Warning: environment regeneration unimplemented '
print(('Warning: environment regeneration unimplemented '
'for %s flag %r env_name %r' % (action, opt,
env_name))
env_name)),
file=sys.stderr)
else:
print >>sys.stderr, ('Warning: regeneration unimplemented for action %r '
'flag %r' % (action, opt))
print(('Warning: regeneration unimplemented for action %r '
'flag %r' % (action, opt)), file=sys.stderr)

return flags

Expand Down Expand Up @@ -431,12 +439,11 @@ def gyp_main(args):
for build_file in build_files:
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):
if build_file_dir_components[index] == 'src':
for component in reversed(build_file_dir_components):
if component == 'src':
options.depth = os.path.sep.join(build_file_dir_components)
break
del build_file_dir_components[index]
del build_file_dir_components[-1]

# If the inner loop found something, break without advancing to another
# build file.
Expand Down Expand Up @@ -475,7 +482,7 @@ def gyp_main(args):
if home_dot_gyp != None:
default_include = os.path.join(home_dot_gyp, 'include.gypi')
if os.path.exists(default_include):
print 'Using overrides found in ' + default_include
print('Using overrides found in ' + default_include)
includes.append(default_include)

# Command-line --include files come after the default include.
Expand All @@ -490,7 +497,7 @@ def gyp_main(args):
if options.generator_flags:
gen_flags += options.generator_flags
generator_flags = NameValueListToDict(gen_flags)
if DEBUG_GENERAL in gyp.debug.keys():
if DEBUG_GENERAL in gyp.debug:
DebugOutput(DEBUG_GENERAL, "generator_flags: %s", generator_flags)

# Generate all requested formats (use a set in case we got one format request
Expand Down Expand Up @@ -523,7 +530,7 @@ def gyp_main(args):
generator.GenerateOutput(flat_list, targets, data, params)

if options.configs:
valid_configs = targets[flat_list[0]]['configurations'].keys()
valid_configs = targets[flat_list[0]]['configurations']
for conf in options.configs:
if conf not in valid_configs:
raise GypError('Invalid config specified via --build: %s' % conf)
Expand All @@ -536,7 +543,7 @@ def gyp_main(args):
def main(args):
try:
return gyp_main(args)
except GypError, e:
except GypError as e:
sys.stderr.write("gyp: %s\n" % e)
return 1

Expand Down
Loading

0 comments on commit ab4aca8

Please sign in to comment.