Skip to content

Commit

Permalink
Make colourize_console() a function
Browse files Browse the repository at this point in the history
Currently, colourize_console is a constant, set at process
initialization.

To allow the actual stdout to be easily compared with the expected when
running tests, we want to allow colourization to be on for the test
driver, but not for the in-process configure done by run_configure,
which has stdout redirected from a tty to a pipe.

v2:
Cache _colorize_console per file object

v3:
Reset cache on setup_console()
  • Loading branch information
jon-turney committed Apr 30, 2020
1 parent cd566d2 commit f107f9b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
29 changes: 23 additions & 6 deletions mesonbuild/mlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,32 @@ def _windows_ansi() -> bool:
# original behavior
return bool(kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON'))

def setup_console() -> bool:
def colorize_console() -> bool:
_colorize_console = getattr(sys.stdout, 'colorize_console', None) # type: bool
if _colorize_console is not None:
return _colorize_console

try:
if platform.system().lower() == 'windows':
return os.isatty(sys.stdout.fileno()) and _windows_ansi()
return os.isatty(sys.stdout.fileno()) and os.environ.get('TERM') != 'dumb'
_colorize_console = os.isatty(sys.stdout.fileno()) and _windows_ansi()
else:
_colorize_console = os.isatty(sys.stdout.fileno()) and os.environ.get('TERM', 'dumb') != 'dumb'
except Exception:
return False
_colorize_console = False

sys.stdout.colorize_console = _colorize_console # type: ignore[attr-defined]
return _colorize_console

def setup_console():
# on Windows, a subprocess might call SetConsoleMode() on the console
# connected to stdout and turn off ANSI escape processing. Call this after
# running a subprocess to ensure we turn it on again.
if platform.system().lower() == 'windows':
try:
delattr(sys.stdout, 'colorize_console')
except AttributeError:
pass

colorize_console = setup_console()
log_dir = None # type: T.Optional[str]
log_file = None # type: T.Optional[T.TextIO]
log_fname = 'meson-log.txt' # type: str
Expand Down Expand Up @@ -204,7 +221,7 @@ def log(*args: T.Union[str, AnsiDecorator], is_error: bool = False,
if log_file is not None:
print(*arr, file=log_file, **kwargs)
log_file.flush()
if colorize_console:
if colorize_console():
arr = process_markup(args, True)
if not log_errors_only or is_error:
force_print(*arr, **kwargs)
Expand Down
8 changes: 4 additions & 4 deletions run_project_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,19 @@ def log_text_file(logfile, testdir, stdo, stde):


def bold(text):
return mlog.bold(text).get_text(mlog.colorize_console)
return mlog.bold(text).get_text(mlog.colorize_console())


def green(text):
return mlog.green(text).get_text(mlog.colorize_console)
return mlog.green(text).get_text(mlog.colorize_console())


def red(text):
return mlog.red(text).get_text(mlog.colorize_console)
return mlog.red(text).get_text(mlog.colorize_console())


def yellow(text):
return mlog.yellow(text).get_text(mlog.colorize_console)
return mlog.yellow(text).get_text(mlog.colorize_console())


def _run_ci_include(args: T.List[str]) -> str:
Expand Down
6 changes: 3 additions & 3 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def run_configure(commandlist, env=None):
return run_configure_inprocess(commandlist, env=env)

def print_system_info():
print(mlog.bold('System information.').get_text(mlog.colorize_console))
print(mlog.bold('System information.').get_text(mlog.colorize_console()))
print('Architecture:', platform.architecture())
print('Machine:', platform.machine())
print('Platform:', platform.system())
Expand Down Expand Up @@ -377,7 +377,7 @@ def main():
print(flush=True)
returncode = 0
else:
print(mlog.bold('Running unittests.').get_text(mlog.colorize_console))
print(mlog.bold('Running unittests.').get_text(mlog.colorize_console()))
print(flush=True)
cmd = mesonlib.python_command + ['run_unittests.py', '-v']
if options.failfast:
Expand All @@ -390,7 +390,7 @@ def main():
else:
cross_test_args = mesonlib.python_command + ['run_cross_test.py']
for cf in options.cross:
print(mlog.bold('Running {} cross tests.'.format(cf)).get_text(mlog.colorize_console))
print(mlog.bold('Running {} cross tests.'.format(cf)).get_text(mlog.colorize_console()))
print(flush=True)
cmd = cross_test_args + ['cross/' + cf]
if options.failfast:
Expand Down

0 comments on commit f107f9b

Please sign in to comment.