mirror of https://github.com/ARMmbed/mbed-os.git
Implement color terminal notifications
You can turn them on with MBED_COLOR=Truepull/6781/head
parent
fcf7dbf4c6
commit
5c16cd3b39
|
@ -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
|
|
|
@ -55,7 +55,6 @@ from utils import argparse_filestring_type
|
||||||
from utils import argparse_many
|
from utils import argparse_many
|
||||||
from utils import argparse_dir_not_parent
|
from utils import argparse_dir_not_parent
|
||||||
from tools.toolchains import mbedToolchain, TOOLCHAIN_CLASSES, TOOLCHAIN_PATHS
|
from tools.toolchains import mbedToolchain, TOOLCHAIN_CLASSES, TOOLCHAIN_PATHS
|
||||||
from tools.settings import CLI_COLOR_MAP
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Parse Options
|
# Parse Options
|
||||||
|
@ -233,7 +232,7 @@ if __name__ == '__main__':
|
||||||
args_error(parser, "argument --build is required when argument --source is provided")
|
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():
|
if not TOOLCHAIN_CLASSES[toolchain].check_executable():
|
||||||
search_path = TOOLCHAIN_PATHS[toolchain] or "No path set"
|
search_path = TOOLCHAIN_PATHS[toolchain] or "No path set"
|
||||||
|
|
|
@ -15,20 +15,49 @@
|
||||||
|
|
||||||
from __future__ import print_function, division, absolute_import
|
from __future__ import print_function, division, absolute_import
|
||||||
|
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
|
|
||||||
from . import Notifier
|
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):
|
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.verbose = verbose
|
||||||
self.silent = silent
|
self.silent = silent
|
||||||
self.output = ""
|
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):
|
def get_output(self):
|
||||||
return self.output
|
return self.output
|
||||||
|
@ -40,6 +69,9 @@ class TerminalNotifier(Notifier):
|
||||||
msg = self.print_notify(event)
|
msg = self.print_notify(event)
|
||||||
if msg:
|
if msg:
|
||||||
if not self.silent:
|
if not self.silent:
|
||||||
|
if self.color:
|
||||||
|
self.print_in_color(event, msg)
|
||||||
|
else:
|
||||||
print(msg)
|
print(msg)
|
||||||
self.output += msg + "\n"
|
self.output += msg + "\n"
|
||||||
|
|
||||||
|
@ -87,3 +119,33 @@ class TerminalNotifier(Notifier):
|
||||||
|
|
||||||
elif event['type'] == 'progress':
|
elif event['type'] == 'progress':
|
||||||
return self.print_notify(event) # standard handle
|
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)
|
||||||
|
|
|
@ -45,6 +45,7 @@ IAR_PATH = ""
|
||||||
# Goanna static analyser. Please overload it in mbed_settings.py
|
# Goanna static analyser. Please overload it in mbed_settings.py
|
||||||
GOANNA_PATH = ""
|
GOANNA_PATH = ""
|
||||||
|
|
||||||
|
|
||||||
# cppcheck path (command) and output message format
|
# cppcheck path (command) and output message format
|
||||||
CPPCHECK_CMD = ["cppcheck", "--enable=all"]
|
CPPCHECK_CMD = ["cppcheck", "--enable=all"]
|
||||||
CPPCHECK_MSG_FORMAT = ["--template=[{severity}] {file}@{line}: {id}:{message}"]
|
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 warnings and errors as link format
|
||||||
PRINT_COMPILER_OUTPUT_AS_LINK = False
|
PRINT_COMPILER_OUTPUT_AS_LINK = False
|
||||||
|
|
||||||
|
# Print warnings/errors in color
|
||||||
|
COLOR = False
|
||||||
|
|
||||||
CLI_COLOR_MAP = {
|
CLI_COLOR_MAP = {
|
||||||
"warning": "yellow",
|
"Warning": "yellow",
|
||||||
"error" : "red"
|
"Error" : "red"
|
||||||
}
|
}
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
@ -77,7 +81,7 @@ except ImportError:
|
||||||
# User Settings (env vars)
|
# User Settings (env vars)
|
||||||
##############################################################################
|
##############################################################################
|
||||||
_ENV_PATHS = ['ARM_PATH', 'GCC_ARM_PATH', 'GCC_CR_PATH', 'IAR_PATH',
|
_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:
|
for _n in _ENV_PATHS:
|
||||||
if getenv('MBED_'+_n):
|
if getenv('MBED_'+_n):
|
||||||
|
@ -87,6 +91,12 @@ for _n in _ENV_PATHS:
|
||||||
print("WARNING: MBED_%s set as environment variable but doesn't"
|
print("WARNING: MBED_%s set as environment variable but doesn't"
|
||||||
" exist" % _n)
|
" 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
|
# Test System Settings
|
||||||
|
|
Loading…
Reference in New Issue