diff --git a/tools/colorize.py b/tools/colorize.py deleted file mode 100644 index 62070127b0..0000000000 --- a/tools/colorize.py +++ /dev/null @@ -1,80 +0,0 @@ -# mbed SDK -# Copyright (c) 2016 ARM Limited -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -""" This python file is responsible for generating colorized notifiers. -""" - -import sys -import re -from colorama import init, Fore, Back, Style -init() - -COLORS = { - 'none' : "", - 'default' : Style.RESET_ALL, - - 'black' : Fore.BLACK, - 'red' : Fore.RED, - 'green' : Fore.GREEN, - 'yellow' : Fore.YELLOW, - 'blue' : Fore.BLUE, - 'magenta' : Fore.MAGENTA, - 'cyan' : Fore.CYAN, - 'white' : Fore.WHITE, - - 'on_black' : Back.BLACK, - 'on_red' : Back.RED, - 'on_green' : Back.GREEN, - 'on_yellow' : Back.YELLOW, - 'on_blue' : Back.BLUE, - 'on_magenta' : Back.MAGENTA, - 'on_cyan' : Back.CYAN, - 'on_white' : Back.WHITE, -} - -COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?") -def colorstring_to_escapecode(color_string): - """ Convert a color string from a string into an ascii escape code that - will print that color on the terminal. - - Positional arguments: - color_string - the string to parse - """ - match = re.match(COLOR_MATCHER, color_string) - if match: - return COLORS[match.group(1)] + \ - (COLORS[match.group(2).strip().replace(" ", "_")] - if match.group(2) else "") - else: - return COLORS['default'] - - -def print_in_color_notifier(color_map, print_fn): - """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap - notifications in a color if the severity matches a color in the *color_map*. - """ - def wrap(event, silent=False): - """The notification function inself""" - file_desc = sys.stdout - self = event['toolchain'] - if file_desc.isatty() and 'severity' in event and \ - event['severity'] in color_map: - file_desc.write(colorstring_to_escapecode( - color_map[event['severity']])) - print_fn(self, event, silent) - file_desc.write(colorstring_to_escapecode('default')) - else: - print_fn(self, event, silent) - return wrap diff --git a/tools/make.py b/tools/make.py index 54bf2c5017..d9c3484449 100644 --- a/tools/make.py +++ b/tools/make.py @@ -55,7 +55,6 @@ from utils import argparse_filestring_type from utils import argparse_many from utils import argparse_dir_not_parent from tools.toolchains import mbedToolchain, TOOLCHAIN_CLASSES, TOOLCHAIN_PATHS -from tools.settings import CLI_COLOR_MAP if __name__ == '__main__': # Parse Options @@ -233,7 +232,7 @@ if __name__ == '__main__': args_error(parser, "argument --build is required when argument --source is provided") - notify = TerminalNotifier(options.verbose, options.silent) + notify = TerminalNotifier(options.verbose, options.silent, options.color) if not TOOLCHAIN_CLASSES[toolchain].check_executable(): search_path = TOOLCHAIN_PATHS[toolchain] or "No path set" diff --git a/tools/notifier/term.py b/tools/notifier/term.py index b03c43c774..2fa96bdfc4 100644 --- a/tools/notifier/term.py +++ b/tools/notifier/term.py @@ -15,20 +15,49 @@ from __future__ import print_function, division, absolute_import +import re +import sys from os.path import basename from . import Notifier -from ..settings import PRINT_COMPILER_OUTPUT_AS_LINK +from ..settings import (PRINT_COMPILER_OUTPUT_AS_LINK, + CLI_COLOR_MAP, COLOR) class TerminalNotifier(Notifier): """ - Writes notifications to a terminal based on a silent and verbose flag. + Writes notifications to a terminal based on silent, verbose and color flags. """ - def __init__(self, verbose=False, silent=False): + def __init__(self, verbose=False, silent=False, color=False): self.verbose = verbose self.silent = silent self.output = "" + self.color = color or COLOR + if self.color: + from colorama import init, Fore, Back, Style + init() + self.COLORS = { + 'none' : "", + 'default' : Style.RESET_ALL, + + 'black' : Fore.BLACK, + 'red' : Fore.RED, + 'green' : Fore.GREEN, + 'yellow' : Fore.YELLOW, + 'blue' : Fore.BLUE, + 'magenta' : Fore.MAGENTA, + 'cyan' : Fore.CYAN, + 'white' : Fore.WHITE, + + 'on_black' : Back.BLACK, + 'on_red' : Back.RED, + 'on_green' : Back.GREEN, + 'on_yellow' : Back.YELLOW, + 'on_blue' : Back.BLUE, + 'on_magenta' : Back.MAGENTA, + 'on_cyan' : Back.CYAN, + 'on_white' : Back.WHITE, + } def get_output(self): return self.output @@ -40,7 +69,10 @@ class TerminalNotifier(Notifier): msg = self.print_notify(event) if msg: if not self.silent: - print(msg) + if self.color: + self.print_in_color(event, msg) + else: + print(msg) self.output += msg + "\n" def print_notify(self, event): @@ -87,3 +119,33 @@ class TerminalNotifier(Notifier): elif event['type'] == 'progress': return self.print_notify(event) # standard handle + + COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?") + def colorstring_to_escapecode(self, color_string): + """ Convert a color string from a string into an ascii escape code that + will print that color on the terminal. + + Positional arguments: + color_string - the string to parse + """ + match = re.match(self.COLOR_MATCHER, color_string) + if match: + return self.COLORS[match.group(1)] + \ + (self.COLORS[match.group(2).strip().replace(" ", "_")] + if match.group(2) else "") + else: + return self.COLORS['default'] + + def print_in_color(self, event, msg): + """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap + notifications in a color if the severity matches a color in the + CLI_COLOR_MAP. + """ + """The notification function inself""" + if sys.stdout.isatty() and event.get('severity', None) in CLI_COLOR_MAP: + sys.stdout.write(self.colorstring_to_escapecode( + CLI_COLOR_MAP[event['severity']])) + print(msg) + sys.stdout.write(self.colorstring_to_escapecode('default')) + else: + print(msg) diff --git a/tools/settings.py b/tools/settings.py index 89109aaab2..3b7fa32b1b 100644 --- a/tools/settings.py +++ b/tools/settings.py @@ -45,6 +45,7 @@ IAR_PATH = "" # Goanna static analyser. Please overload it in mbed_settings.py GOANNA_PATH = "" + # cppcheck path (command) and output message format CPPCHECK_CMD = ["cppcheck", "--enable=all"] CPPCHECK_MSG_FORMAT = ["--template=[{severity}] {file}@{line}: {id}:{message}"] @@ -57,9 +58,12 @@ MBED_ORG_USER = "" # Print compiler warnings and errors as link format PRINT_COMPILER_OUTPUT_AS_LINK = False +# Print warnings/errors in color +COLOR = False + CLI_COLOR_MAP = { - "warning": "yellow", - "error" : "red" + "Warning": "yellow", + "Error" : "red" } ############################################################################## @@ -77,7 +81,7 @@ except ImportError: # User Settings (env vars) ############################################################################## _ENV_PATHS = ['ARM_PATH', 'GCC_ARM_PATH', 'GCC_CR_PATH', 'IAR_PATH', - 'ARMC6_PATH', 'PRINT_COMPILER_OUTPUT_AS_LINK'] + 'ARMC6_PATH'] for _n in _ENV_PATHS: if getenv('MBED_'+_n): @@ -87,6 +91,12 @@ for _n in _ENV_PATHS: print("WARNING: MBED_%s set as environment variable but doesn't" " exist" % _n) +_ENV_VARS = ['PRINT_COMPILER_OUTPUT_AS_LINK', 'COLOR'] +for _n in _ENV_VARS: + value = getenv('MBED_%s' % _n) + if value: + globals()[_n] = value + ############################################################################## # Test System Settings