Python 3 updates to build script

These updates should allow the build_osd.py script to work with
Python 3.x and is based on changes made in the similar script in
the USD distribution.
This commit is contained in:
George ElKoura 2020-10-26 14:28:34 -07:00
parent 82ab1b9f54
commit dc368d0d88

View File

@ -21,14 +21,18 @@
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
from __future__ import print_function
from distutils.spawn import find_executable
import argparse
import codecs
import contextlib
import datetime
import distutils
import fnmatch
import glob
import locale
import multiprocessing
import os
import platform
@ -38,34 +42,41 @@ import shutil
import subprocess
import sys
import tarfile
import urllib2
import zipfile
if sys.version_info.major >= 3:
from urllib.request import urlopen
else:
from urllib2 import urlopen
# Helpers for printing output
verbosity = 1
def Print(msg):
if verbosity > 0:
print msg
print(msg)
def PrintWarning(warning):
if verbosity > 0:
print "WARNING:", warning
print("WARNING:", warning)
def PrintStatus(status):
if verbosity >= 1:
print "STATUS:", status
print("STATUS:", status)
def PrintInfo(info):
if verbosity >= 2:
print "INFO:", info
print("INFO:", info)
def PrintCommandOutput(output):
if verbosity >= 3:
sys.stdout.write(output)
def PrintError(error):
print "ERROR:", error
if verbosity >= 3 and sys.exc_info()[1] is not None:
import traceback
traceback.print_exc()
print("ERROR:", error)
# Helpers for determining platform
def Windows():
@ -75,11 +86,15 @@ def Linux():
def MacOS():
return platform.system() == "Darwin"
def GetLocale():
return sys.stdout.encoding or locale.getdefaultlocale()[1] or "UTF-8"
def GetCommandOutput(command):
"""Executes the specified command and returns output or None."""
try:
return subprocess.check_output(
shlex.split(command), stderr=subprocess.STDOUT).strip()
shlex.split(command),
stderr=subprocess.STDOUT).decode(GetLocale(), 'replace').strip()
except subprocess.CalledProcessError:
pass
return None
@ -136,7 +151,7 @@ def Run(cmd, logCommandOutput = True):
"""Run the specified command in a subprocess."""
PrintInfo('Running "{cmd}"'.format(cmd=cmd))
with open("log.txt", "a") as logfile:
with codecs.open("log.txt", "a", "utf-8") as logfile:
logfile.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
logfile.write("\n")
logfile.write(cmd)
@ -148,8 +163,8 @@ def Run(cmd, logCommandOutput = True):
p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
while True:
l = p.stdout.readline()
if l != "":
l = p.stdout.readline().decode(GetLocale(), 'replace')
if l:
logfile.write(l)
PrintCommandOutput(l)
elif p.poll() is not None:
@ -305,7 +320,7 @@ def DownloadFileWithPowershell(url, outputFilename):
Run(cmd,logCommandOutput=False)
def DownloadFileWithUrllib(url, outputFilename):
r = urllib2.urlopen(url)
r = urlopen(url)
with open(outputFilename, "wb") as outfile:
outfile.write(r.read())
@ -344,7 +359,7 @@ def DownloadURL(url, context, force, dontExtract = None):
if os.path.exists(tmpFilename):
os.remove(tmpFilename)
for i in xrange(maxRetries):
for i in range(maxRetries):
try:
context.downloader(url, tmpFilename)
break
@ -975,7 +990,7 @@ if context.buildArgs:
def FormatBuildArguments(buildArgs):
s = ""
for depName in sorted(buildArgs.iterkeys()):
for depName in sorted(buildArgs.keys()):
args = buildArgs[depName]
s += """
{name}: {args}""".format(