From 9ba7b0ebd064517832cc37f45d8f549666a06825 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Wed, 18 Nov 2020 14:09:30 +0100 Subject: [PATCH] mlog: add __str__ method to AnsiDecorator Automatically colorize the text when printing the AnsiDecorator, based on the result of mlog.colorize_console(). This is how AnsiDecorator is used most of the time anyway. --- mesonbuild/mlog.py | 3 +++ run_project_tests.py | 17 +---------------- run_tests.py | 6 +++--- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py index 0cffba7c3..6207d98a4 100644 --- a/mesonbuild/mlog.py +++ b/mesonbuild/mlog.py @@ -130,6 +130,9 @@ class AnsiDecorator: text = '"{}"'.format(text) return text + def __str__(self) -> str: + return self.get_text(colorize_console()) + def bold(text: str, quoted: bool = False) -> AnsiDecorator: return AnsiDecorator(text, "\033[1m", quoted=quoted) diff --git a/run_project_tests.py b/run_project_tests.py index 772aa2ebe..33641d708 100755 --- a/run_project_tests.py +++ b/run_project_tests.py @@ -43,6 +43,7 @@ from mesonbuild import mlog from mesonbuild import mtest from mesonbuild.build import ConfigurationData from mesonbuild.mesonlib import MachineChoice, Popen_safe, TemporaryDirectoryWinProof +from mesonbuild.mlog import bold, green, red, yellow from mesonbuild.coredata import backendlist, version as meson_version from run_tests import get_fake_options, run_configure, get_meson_script @@ -345,22 +346,6 @@ def log_text_file(logfile, testdir, stdo, stde): raise StopException() -def bold(text): - return mlog.bold(text).get_text(mlog.colorize_console()) - - -def green(text): - return mlog.green(text).get_text(mlog.colorize_console()) - - -def red(text): - return mlog.red(text).get_text(mlog.colorize_console()) - - -def yellow(text): - return mlog.yellow(text).get_text(mlog.colorize_console()) - - def _run_ci_include(args: T.List[str]) -> str: if not args: return 'At least one parameter required' diff --git a/run_tests.py b/run_tests.py index da894c055..24d6a516f 100755 --- a/run_tests.py +++ b/run_tests.py @@ -290,7 +290,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.')) print('Architecture:', platform.architecture()) print('Machine:', platform.machine()) print('Platform:', platform.system()) @@ -364,7 +364,7 @@ def main(): print(flush=True) returncode = 0 else: - print(mlog.bold('Running unittests.').get_text(mlog.colorize_console())) + print(mlog.bold('Running unittests.')) print(flush=True) cmd = mesonlib.python_command + ['run_unittests.py', '-v'] if options.failfast: @@ -377,7 +377,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))) print(flush=True) cmd = cross_test_args + ['cross/' + cf] if options.failfast: