Skip to content

Commit

Permalink
tools: port Python 3 compat patch a48f09b from node-gyp to gyp
Browse files Browse the repository at this point in the history
original commit: nodejs/node-gyp@a48f09b
Refs: nodejs/node-gyp#1937
Co-Authored-By: Christian Clauss <cclauss@me.com>
  • Loading branch information
chrmoritz committed Nov 5, 2019
1 parent 1f5d5cc commit b1bbc13
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions tools/gyp/pylib/gyp/MSVSVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import gyp
import glob

PY3 = bytes != str


def JoinPath(*args):
return os.path.normpath(os.path.join(*args))
Expand Down Expand Up @@ -163,6 +165,8 @@ def _RegistryQueryBase(sysdir, key, value):
# Obtain the stdout from reg.exe, reading to the end so p.returncode is valid
# Note that the error text may be in [1] in some cases
text = p.communicate()[0]
if PY3:
text = text.decode('utf-8')
# Check return code from reg.exe; officially 0==success and 1==error
if p.returncode:
return None
Expand Down Expand Up @@ -385,6 +389,8 @@ def _ConvertToCygpath(path):
if sys.platform == 'cygwin':
p = subprocess.Popen(['cygpath', path], stdout=subprocess.PIPE)
path = p.communicate()[0].strip()
if PY3:
path = path.decode('utf-8')
return path


Expand Down
6 changes: 6 additions & 0 deletions tools/gyp/pylib/gyp/generator/eclipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import shlex
import xml.etree.cElementTree as ET

PY3 = bytes != str

generator_wants_static_library_dependencies_adjusted = False

generator_default_variables = {
Expand Down Expand Up @@ -97,6 +99,8 @@ def GetAllIncludeDirectories(target_list, target_dicts,
proc = subprocess.Popen(args=command, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = proc.communicate()[1]
if PY3:
output = output.decode('utf-8')
# Extract the list of include dirs from the output, which has this format:
# ...
# #include "..." search starts here:
Expand Down Expand Up @@ -234,6 +238,8 @@ def GetAllDefines(target_list, target_dicts, data, config_name, params,
cpp_proc = subprocess.Popen(args=command, cwd='.',
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
cpp_output = cpp_proc.communicate()[0]
if PY3:
cpp_output = cpp_output.decode('utf-8')
cpp_lines = cpp_output.split('\n')
for cpp_line in cpp_lines:
if not cpp_line.strip():
Expand Down
4 changes: 4 additions & 0 deletions tools/gyp/pylib/gyp/generator/msvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from gyp.common import GypError
from gyp.common import OrderedSet

PY3 = bytes != str

# TODO: Remove once bots are on 2.7, http://crbug.com/241769
def _import_OrderedDict():
import collections
Expand Down Expand Up @@ -126,6 +128,8 @@ def _GetDomainAndUserName():
call = subprocess.Popen(['net', 'config', 'Workstation'],
stdout=subprocess.PIPE)
config = call.communicate()[0]
if PY3:
config = config.decode('utf-8')
username_re = re.compile(r'^User name\s+(\S+)', re.MULTILINE)
username_match = username_re.search(config)
if username_match:
Expand Down
4 changes: 4 additions & 0 deletions tools/gyp/pylib/gyp/mac_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import sys
import tempfile

PY3 = bytes != str


def main(args):
executor = MacTool()
Expand Down Expand Up @@ -263,6 +265,8 @@ def ExecFilterLibtool(self, *cmd_list):
env['ZERO_AR_DATE'] = '1'
libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env)
_, err = libtoolout.communicate()
if PY3:
err = err.decode('utf-8')
for line in err.splitlines():
if not libtool_re.match(line) and not libtool_re5.match(line):
print(line, file=sys.stderr)
Expand Down
10 changes: 9 additions & 1 deletion tools/gyp/pylib/gyp/msvs_emulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import gyp.MSVSUtil
import gyp.MSVSVersion

PY3 = bytes != str

windows_quoter_regex = re.compile(r'(\\*)"')

Expand Down Expand Up @@ -130,7 +131,10 @@ def _FindDirectXInstallation():
# Setup params to pass to and attempt to launch reg.exe.
cmd = ['reg.exe', 'query', r'HKLM\Software\Microsoft\DirectX', '/s']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in p.communicate()[0].splitlines():
stdout = p.communicate()[0]
if PY3:
stdout = stdout.decode('utf-8')
for line in stdout.splitlines():
if 'InstallPath' in line:
dxsdk_dir = line.split(' ')[3] + "\\"

Expand Down Expand Up @@ -1045,6 +1049,8 @@ def GenerateEnvironmentFiles(toplevel_build_dir, generator_flags,
popen = subprocess.Popen(
args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
variables, _ = popen.communicate()
if PY3:
variables = variables.decode('utf-8')
if popen.returncode != 0:
raise Exception('"%s" failed with error %d' % (args, popen.returncode))
env = _ExtractImportantEnvironment(variables)
Expand All @@ -1066,6 +1072,8 @@ def GenerateEnvironmentFiles(toplevel_build_dir, generator_flags,
'for', '%i', 'in', '(cl.exe)', 'do', '@echo', 'LOC:%~$PATH:i'))
popen = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)
output, _ = popen.communicate()
if PY3:
output = output.decode('utf-8')
cl_paths[arch] = _ExtractCLPath(output)
return cl_paths

Expand Down
11 changes: 11 additions & 0 deletions tools/gyp/pylib/gyp/win_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PY3 = bytes != str

# A regex matching an argument corresponding to the output filename passed to
# link.exe.
Expand Down Expand Up @@ -132,6 +133,8 @@ def ExecLinkWrapper(self, arch, use_separate_mspdbsrv, *args):
link = subprocess.Popen(args, shell=sys.platform == 'win32', env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = link.communicate()
if PY3:
out = out.decode('utf-8')
for line in out.splitlines():
if (not line.startswith(' Creating library ') and
not line.startswith('Generating code') and
Expand Down Expand Up @@ -223,6 +226,8 @@ def ExecManifestWrapper(self, arch, *args):
popen = subprocess.Popen(args, shell=True, env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = popen.communicate()
if PY3:
out = out.decode('utf-8')
for line in out.splitlines():
if line and 'manifest authoring warning 81010002' not in line:
print(line)
Expand Down Expand Up @@ -255,6 +260,8 @@ def ExecMidlWrapper(self, arch, outdir, tlb, h, dlldata, iid, proxy, idl,
popen = subprocess.Popen(args, shell=True, env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = popen.communicate()
if PY3:
out = out.decode('utf-8')
# Filter junk out of stdout, and write filtered versions. Output we want
# to filter is pairs of lines that look like this:
# Processing C:\Program Files (x86)\Microsoft SDKs\...\include\objidl.idl
Expand All @@ -274,6 +281,8 @@ def ExecAsmWrapper(self, arch, *args):
popen = subprocess.Popen(args, shell=True, env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = popen.communicate()
if PY3:
out = out.decode('utf-8')
for line in out.splitlines():
if (not line.startswith('Copyright (C) Microsoft Corporation') and
not line.startswith('Microsoft (R) Macro Assembler') and
Expand All @@ -289,6 +298,8 @@ def ExecRcWrapper(self, arch, *args):
popen = subprocess.Popen(args, shell=True, env=env,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = popen.communicate()
if PY3:
out = out.decode('utf-8')
for line in out.splitlines():
if (not line.startswith('Microsoft (R) Windows (R) Resource Compiler') and
not line.startswith('Copyright (C) Microsoft Corporation') and
Expand Down

0 comments on commit b1bbc13

Please sign in to comment.