gclient_hook: Add Python 3 support (see issue #2856)

This commit is contained in:
Marshall Greenblatt 2020-01-12 17:48:13 +02:00
parent 894ac21532
commit b98f142e0e
3 changed files with 34 additions and 27 deletions

View File

@ -3,11 +3,14 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import absolute_import
from __future__ import print_function
from file_util import make_dir, write_file
from gclient_util import * from gclient_util import *
from gn_args import GetAllPlatformConfigs, GetConfigFileContents from gn_args import GetAllPlatformConfigs, GetConfigFileContents
from file_util import make_dir, write_file
import os, sys
import issue_1999 import issue_1999
import os
import sys
# The CEF directory is the parent directory of _this_ script. # The CEF directory is the parent directory of _this_ script.
cef_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) cef_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
@ -22,21 +25,21 @@ elif sys.platform == 'darwin':
elif sys.platform.startswith('linux'): elif sys.platform.startswith('linux'):
platform = 'linux' platform = 'linux'
else: else:
print 'Unknown operating system platform' print('Unknown operating system platform')
sys.exit() sys.exit()
print "\nGenerating CEF version header file..." print("\nGenerating CEF version header file...")
cmd = [ cmd = [
'python', 'tools/make_version_header.py', '--header', sys.executable, 'tools/make_version_header.py', '--header',
'include/cef_version.h' 'include/cef_version.h'
] ]
RunAction(cef_dir, cmd) RunAction(cef_dir, cmd)
print "\nPatching build configuration and source files for CEF..." print("\nPatching build configuration and source files for CEF...")
cmd = ['python', 'tools/patcher.py'] cmd = [sys.executable, 'tools/patcher.py']
RunAction(cef_dir, cmd) RunAction(cef_dir, cmd)
print "\nGenerating CEF project files..." print("\nGenerating CEF project files...")
gn_args = {} gn_args = {}
@ -133,13 +136,13 @@ for dir, config in configs.items():
if platform == 'windows': if platform == 'windows':
issue_1999.apply(out_path) issue_1999.apply(out_path)
gn_dir = configs.keys()[0] gn_dir = list(configs.keys())[0]
out_gn_path = os.path.join(src_dir, 'out', gn_dir) out_gn_path = os.path.join(src_dir, 'out', gn_dir)
gn_path = os.path.join(out_gn_path, 'args.gn') gn_path = os.path.join(out_gn_path, 'args.gn')
print "\nGenerating CEF buildinfo header file..." print("\nGenerating CEF buildinfo header file...")
cmd = [ cmd = [
'python', 'tools/make_config_header.py', '--header', 'include/cef_config.h', sys.executable, 'tools/make_config_header.py', '--header',
'--cef_gn_config', gn_path 'include/cef_config.h', '--cef_gn_config', gn_path
] ]
RunAction(cef_dir, cmd) RunAction(cef_dir, cmd)

View File

@ -3,12 +3,14 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import absolute_import
from __future__ import print_function
import os, sys import os, sys
try: try:
# depot_tools may already be in the import path. # depot_tools may already be in the import path.
import gclient_utils import gclient_utils
except ImportError, e: except ImportError as e:
# Search the PATH environment variable to find the depot_tools folder. # Search the PATH environment variable to find the depot_tools folder.
depot_tools = None depot_tools = None
paths = os.environ.get('PATH').split(os.pathsep) paths = os.environ.get('PATH').split(os.pathsep)
@ -18,7 +20,7 @@ except ImportError, e:
break break
if depot_tools is None: if depot_tools is None:
print >> sys.stderr, 'Error: could not find depot_tools in PATH.' print('Error: could not find depot_tools in PATH.', file=sys.stderr)
sys.exit(2) sys.exit(2)
# Add depot_tools to import path. # Add depot_tools to import path.
@ -29,18 +31,12 @@ except ImportError, e:
# Copied from gclient.py python code. # Copied from gclient.py python code.
def RunAction(dir, command): def RunAction(dir, command):
"""Runs the action.""" """Runs the action."""
if command[0] == 'python':
# If the hook specified "python" as the first item, the action is a
# Python script. Run it by starting a new copy of the same
# interpreter.
command[0] = sys.executable
try: try:
gclient_utils.CheckCallAndFilter( gclient_utils.CheckCallAndFilter(
command, cwd=dir, always_show_header=True, print_stdout=True) command, cwd=dir, always_show_header=True, print_stdout=True)
except gclient_utils.Error, e: except gclient_utils.Error as e:
# Use a discrete exit status code of 2 to indicate that a hook action # Use a discrete exit status code of 2 to indicate that a hook action
# failed. Users of this script may wish to treat hook action failures # failed. Users of this script may wish to treat hook action failures
# differently from VC failures. # differently from VC failures.
print >> sys.stderr, 'Error: %s' % str(e) print('Error: %s' % str(e), file=sys.stderr)
sys.exit(2) sys.exit(2)

View File

@ -10,6 +10,9 @@
# import issue_1999 # import issue_1999
# issue_1999.apply(output_path) # issue_1999.apply(output_path)
# #
from __future__ import absolute_import
from __future__ import print_function
from io import open
import sys import sys
import os import os
@ -82,9 +85,9 @@ def process_line(line):
def process_file(path): def process_file(path):
print "Applying issue #1999 fix to " + path print("Applying issue #1999 fix to " + path)
with open(path) as f: with open(path, 'r', encoding='utf-8') as f:
content = f.read().splitlines() content = f.read().splitlines()
result = [] result = []
@ -92,9 +95,14 @@ def process_file(path):
for line in content: for line in content:
result.append(process_line(line)) result.append(process_line(line))
with open(path, "w") as f: with open(path, 'w', encoding='utf-8') as fp:
f.write("\n".join(result)) str = "\n".join(result) + "\n"
f.write("\n") try:
# Python 2
fp.write(str.decode('utf-8'))
except Exception as e:
# Python 3
fp.write(str)
def apply(confpath): def apply(confpath):