Handle unicode correctly when detecting compiler version

This commit is contained in:
Massimiliano Culpo 2018-12-25 10:12:55 +01:00
parent a061c4a7ad
commit 16f402a6c0
No known key found for this signature in database
GPG Key ID: D1ADB1014FF1118C
5 changed files with 28 additions and 16 deletions

View File

@ -3,7 +3,8 @@
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
from datetime import datetime from __future__ import unicode_literals
import fcntl import fcntl
import os import os
import struct import struct
@ -11,6 +12,8 @@
import termios import termios
import textwrap import textwrap
import traceback import traceback
import six
from datetime import datetime
from six import StringIO from six import StringIO
from six.moves import input from six.moves import input
@ -155,7 +158,7 @@ def msg(message, *args, **kwargs):
cwrite("@*b{%s==>} %s%s" % ( cwrite("@*b{%s==>} %s%s" % (
st_text, get_timestamp(), cescape(message))) st_text, get_timestamp(), cescape(message)))
for arg in args: for arg in args:
print(indent + str(arg)) print(indent + six.text_type(arg))
def info(message, *args, **kwargs): def info(message, *args, **kwargs):
@ -172,17 +175,17 @@ def info(message, *args, **kwargs):
if _stacktrace: if _stacktrace:
st_text = process_stacktrace(st_countback) st_text = process_stacktrace(st_countback)
cprint("@%s{%s==>} %s%s" % ( cprint("@%s{%s==>} %s%s" % (
format, st_text, get_timestamp(), cescape(str(message))), format, st_text, get_timestamp(), cescape(six.text_type(message))
stream=stream) ), stream=stream)
for arg in args: for arg in args:
if wrap: if wrap:
lines = textwrap.wrap( lines = textwrap.wrap(
str(arg), initial_indent=indent, subsequent_indent=indent, six.text_type(arg), initial_indent=indent,
break_long_words=break_long_words) subsequent_indent=indent, break_long_words=break_long_words)
for line in lines: for line in lines:
stream.write(line + '\n') stream.write(line + '\n')
else: else:
stream.write(indent + str(arg) + '\n') stream.write(indent + six.text_type(arg) + '\n')
def verbose(message, *args, **kwargs): def verbose(message, *args, **kwargs):
@ -204,7 +207,7 @@ def error(message, *args, **kwargs):
kwargs.setdefault('format', '*r') kwargs.setdefault('format', '*r')
kwargs.setdefault('stream', sys.stderr) kwargs.setdefault('stream', sys.stderr)
info("Error: " + str(message), *args, **kwargs) info("Error: " + six.text_type(message), *args, **kwargs)
def warn(message, *args, **kwargs): def warn(message, *args, **kwargs):
@ -213,7 +216,7 @@ def warn(message, *args, **kwargs):
kwargs.setdefault('format', '*Y') kwargs.setdefault('format', '*Y')
kwargs.setdefault('stream', sys.stderr) kwargs.setdefault('stream', sys.stderr)
info("Warning: " + str(message), *args, **kwargs) info("Warning: " + six.text_type(message), *args, **kwargs)
def die(message, *args, **kwargs): def die(message, *args, **kwargs):
@ -237,7 +240,7 @@ def get_number(prompt, **kwargs):
while number is None: while number is None:
msg(prompt, newline=False) msg(prompt, newline=False)
ans = input() ans = input()
if ans == str(abort): if ans == six.text_type(abort):
return None return None
if ans: if ans:
@ -303,7 +306,7 @@ def hline(label=None, **kwargs):
cols -= 2 cols -= 2
cols = min(max_width, cols) cols = min(max_width, cols)
label = str(label) label = six.text_type(label)
prefix = char * 2 + " " prefix = char * 2 + " "
suffix = " " + (cols - len(prefix) - clen(label)) * char suffix = " " + (cols - len(prefix) - clen(label)) * char

View File

@ -6,7 +6,7 @@
""" """
Routines for printing columnar output. See ``colify()`` for more information. Routines for printing columnar output. See ``colify()`` for more information.
""" """
from __future__ import division from __future__ import division, unicode_literals
import os import os
import sys import sys

View File

@ -59,10 +59,14 @@
To output an @, use '@@'. To output a } inside braces, use '}}'. To output an @, use '@@'. To output a } inside braces, use '}}'.
""" """
from __future__ import unicode_literals
import re import re
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
import six
class ColorParseError(Exception): class ColorParseError(Exception):
"""Raised when a color format fails to parse.""" """Raised when a color format fails to parse."""
@ -244,7 +248,7 @@ def cescape(string):
Returns: Returns:
(str): the string with color codes escaped (str): the string with color codes escaped
""" """
string = str(string) string = six.text_type(string)
string = string.replace('@', '@@') string = string.replace('@', '@@')
string = string.replace('}', '}}') string = string.replace('}', '}}')
return string return string

View File

@ -5,6 +5,8 @@
"""Utility classes for logging the output of blocks of code. """Utility classes for logging the output of blocks of code.
""" """
from __future__ import unicode_literals
import multiprocessing import multiprocessing
import os import os
import re import re

View File

@ -10,6 +10,7 @@
import functools_backport import functools_backport
import platform as py_platform import platform as py_platform
import six
import llnl.util.lang import llnl.util.lang
import llnl.util.multiproc import llnl.util.multiproc
@ -366,16 +367,18 @@ def detect_version_command(callback, path):
""" """
try: try:
version = callback(path) version = callback(path)
if version and str(version).strip(): if version and six.text_type(version).strip():
return (version, path), None return (version, path), None
error = "Couldn't get version for compiler {0}".format(path) error = "Couldn't get version for compiler {0}".format(path)
except spack.util.executable.ProcessError as e: except spack.util.executable.ProcessError as e:
error = "Couldn't get version for compiler {0}\n".format(path) + str(e) error = "Couldn't get version for compiler {0}\n".format(path) + \
six.text_type(e)
except Exception as e: except Exception as e:
# Catching "Exception" here is fine because it just # Catching "Exception" here is fine because it just
# means something went wrong running a candidate executable. # means something went wrong running a candidate executable.
error = "Error while executing candidate compiler {0}" \ error = "Error while executing candidate compiler {0}" \
"\n{1}: {2}".format(path, e.__class__.__name__, str(e)) "\n{1}: {2}".format(path, e.__class__.__name__,
six.text_type(e))
return None, error return None, error