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