Clone of official tools

Committer:
theotherjimmy
Date:
Tue Sep 25 13:43:09 2018 -0500
Revision:
43:2a7da56ebd24
Release 5.10.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theotherjimmy 43:2a7da56ebd24 1 # mbed SDK
theotherjimmy 43:2a7da56ebd24 2 # Copyright (c) 2011-2013 ARM Limited
theotherjimmy 43:2a7da56ebd24 3 #
theotherjimmy 43:2a7da56ebd24 4 # Licensed under the Apache License, Version 2.0 (the "License");
theotherjimmy 43:2a7da56ebd24 5 # you may not use this file except in compliance with the License.
theotherjimmy 43:2a7da56ebd24 6 # You may obtain a copy of the License at
theotherjimmy 43:2a7da56ebd24 7 #
theotherjimmy 43:2a7da56ebd24 8 # http://www.apache.org/licenses/LICENSE-2.0
theotherjimmy 43:2a7da56ebd24 9 #
theotherjimmy 43:2a7da56ebd24 10 # Unless required by applicable law or agreed to in writing, software
theotherjimmy 43:2a7da56ebd24 11 # distributed under the License is distributed on an "AS IS" BASIS,
theotherjimmy 43:2a7da56ebd24 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
theotherjimmy 43:2a7da56ebd24 13 # See the License for the specific language governing permissions and
theotherjimmy 43:2a7da56ebd24 14 # limitations under the License.
theotherjimmy 43:2a7da56ebd24 15
theotherjimmy 43:2a7da56ebd24 16 from __future__ import print_function, division, absolute_import
theotherjimmy 43:2a7da56ebd24 17 from past.builtins import basestring
theotherjimmy 43:2a7da56ebd24 18
theotherjimmy 43:2a7da56ebd24 19 import re
theotherjimmy 43:2a7da56ebd24 20 import sys
theotherjimmy 43:2a7da56ebd24 21 from os import getcwd
theotherjimmy 43:2a7da56ebd24 22 from os.path import (basename, abspath)
theotherjimmy 43:2a7da56ebd24 23
theotherjimmy 43:2a7da56ebd24 24 from . import Notifier
theotherjimmy 43:2a7da56ebd24 25 from ..settings import (PRINT_COMPILER_OUTPUT_AS_LINK,
theotherjimmy 43:2a7da56ebd24 26 CLI_COLOR_MAP, COLOR)
theotherjimmy 43:2a7da56ebd24 27
theotherjimmy 43:2a7da56ebd24 28 class TerminalNotifier(Notifier):
theotherjimmy 43:2a7da56ebd24 29 """
theotherjimmy 43:2a7da56ebd24 30 Writes notifications to a terminal based on silent, verbose and color flags.
theotherjimmy 43:2a7da56ebd24 31 """
theotherjimmy 43:2a7da56ebd24 32
theotherjimmy 43:2a7da56ebd24 33 def __init__(self, verbose=False, silent=False, color=False):
theotherjimmy 43:2a7da56ebd24 34 self.verbose = verbose
theotherjimmy 43:2a7da56ebd24 35 self.silent = silent
theotherjimmy 43:2a7da56ebd24 36 self.output = ""
theotherjimmy 43:2a7da56ebd24 37 self.color = color or COLOR
theotherjimmy 43:2a7da56ebd24 38 if self.color:
theotherjimmy 43:2a7da56ebd24 39 from colorama import init, Fore, Back, Style
theotherjimmy 43:2a7da56ebd24 40 init()
theotherjimmy 43:2a7da56ebd24 41 self.COLORS = {
theotherjimmy 43:2a7da56ebd24 42 'none' : "",
theotherjimmy 43:2a7da56ebd24 43 'default' : Style.RESET_ALL,
theotherjimmy 43:2a7da56ebd24 44
theotherjimmy 43:2a7da56ebd24 45 'black' : Fore.BLACK,
theotherjimmy 43:2a7da56ebd24 46 'red' : Fore.RED,
theotherjimmy 43:2a7da56ebd24 47 'green' : Fore.GREEN,
theotherjimmy 43:2a7da56ebd24 48 'yellow' : Fore.YELLOW,
theotherjimmy 43:2a7da56ebd24 49 'blue' : Fore.BLUE,
theotherjimmy 43:2a7da56ebd24 50 'magenta' : Fore.MAGENTA,
theotherjimmy 43:2a7da56ebd24 51 'cyan' : Fore.CYAN,
theotherjimmy 43:2a7da56ebd24 52 'white' : Fore.WHITE,
theotherjimmy 43:2a7da56ebd24 53
theotherjimmy 43:2a7da56ebd24 54 'on_black' : Back.BLACK,
theotherjimmy 43:2a7da56ebd24 55 'on_red' : Back.RED,
theotherjimmy 43:2a7da56ebd24 56 'on_green' : Back.GREEN,
theotherjimmy 43:2a7da56ebd24 57 'on_yellow' : Back.YELLOW,
theotherjimmy 43:2a7da56ebd24 58 'on_blue' : Back.BLUE,
theotherjimmy 43:2a7da56ebd24 59 'on_magenta' : Back.MAGENTA,
theotherjimmy 43:2a7da56ebd24 60 'on_cyan' : Back.CYAN,
theotherjimmy 43:2a7da56ebd24 61 'on_white' : Back.WHITE,
theotherjimmy 43:2a7da56ebd24 62 }
theotherjimmy 43:2a7da56ebd24 63
theotherjimmy 43:2a7da56ebd24 64 def get_output(self):
theotherjimmy 43:2a7da56ebd24 65 return self.output
theotherjimmy 43:2a7da56ebd24 66
theotherjimmy 43:2a7da56ebd24 67 def notify(self, event):
theotherjimmy 43:2a7da56ebd24 68 if self.verbose:
theotherjimmy 43:2a7da56ebd24 69 msg = self.print_notify_verbose(event)
theotherjimmy 43:2a7da56ebd24 70 else:
theotherjimmy 43:2a7da56ebd24 71 msg = self.print_notify(event)
theotherjimmy 43:2a7da56ebd24 72 if msg:
theotherjimmy 43:2a7da56ebd24 73 if not self.silent:
theotherjimmy 43:2a7da56ebd24 74 if self.color:
theotherjimmy 43:2a7da56ebd24 75 self.print_in_color(event, msg)
theotherjimmy 43:2a7da56ebd24 76 else:
theotherjimmy 43:2a7da56ebd24 77 print(msg)
theotherjimmy 43:2a7da56ebd24 78 self.output += msg + "\n"
theotherjimmy 43:2a7da56ebd24 79
theotherjimmy 43:2a7da56ebd24 80 def print_notify(self, event):
theotherjimmy 43:2a7da56ebd24 81 """ Command line notification
theotherjimmy 43:2a7da56ebd24 82 """
theotherjimmy 43:2a7da56ebd24 83 if event['type'] in ('tool_error', 'info'):
theotherjimmy 43:2a7da56ebd24 84 return event['message']
theotherjimmy 43:2a7da56ebd24 85
theotherjimmy 43:2a7da56ebd24 86 elif event['type'] == 'cc' and event['severity'] != 'verbose':
theotherjimmy 43:2a7da56ebd24 87 event['severity'] = event['severity'].title()
theotherjimmy 43:2a7da56ebd24 88
theotherjimmy 43:2a7da56ebd24 89 if PRINT_COMPILER_OUTPUT_AS_LINK:
theotherjimmy 43:2a7da56ebd24 90 event['file'] = abspath(event['file'])
theotherjimmy 43:2a7da56ebd24 91 return '[{severity}] {file}:{line}:{col}: {message}'.format(
theotherjimmy 43:2a7da56ebd24 92 **event)
theotherjimmy 43:2a7da56ebd24 93 else:
theotherjimmy 43:2a7da56ebd24 94 event['file'] = basename(event['file'])
theotherjimmy 43:2a7da56ebd24 95 return '[{severity}] {file}@{line},{col}: {message}'.format(
theotherjimmy 43:2a7da56ebd24 96 **event)
theotherjimmy 43:2a7da56ebd24 97
theotherjimmy 43:2a7da56ebd24 98 elif event['type'] == 'progress':
theotherjimmy 43:2a7da56ebd24 99 event['action'] = event['action'].title()
theotherjimmy 43:2a7da56ebd24 100 event['file'] = basename(event['file'])
theotherjimmy 43:2a7da56ebd24 101 if 'percent' in event:
theotherjimmy 43:2a7da56ebd24 102 format_string = '{action} [{percent:>5.1f}%]: {file}'
theotherjimmy 43:2a7da56ebd24 103 else:
theotherjimmy 43:2a7da56ebd24 104 format_string = '{action}: {file}'
theotherjimmy 43:2a7da56ebd24 105 return format_string.format(**event)
theotherjimmy 43:2a7da56ebd24 106
theotherjimmy 43:2a7da56ebd24 107 def print_notify_verbose(self, event):
theotherjimmy 43:2a7da56ebd24 108 """ Command line notification with more verbose mode
theotherjimmy 43:2a7da56ebd24 109 """
theotherjimmy 43:2a7da56ebd24 110 if event['type'] == 'info' or (event['type'] == 'cc' and
theotherjimmy 43:2a7da56ebd24 111 event['severity'] == 'verbose'):
theotherjimmy 43:2a7da56ebd24 112 return event['message']
theotherjimmy 43:2a7da56ebd24 113 elif event['type'] == 'debug':
theotherjimmy 43:2a7da56ebd24 114 return "[DEBUG] {message}".format(**event)
theotherjimmy 43:2a7da56ebd24 115 elif event['type'] in ('progress', 'cc'):
theotherjimmy 43:2a7da56ebd24 116 return self.print_notify(event)
theotherjimmy 43:2a7da56ebd24 117
theotherjimmy 43:2a7da56ebd24 118 COLOR_MATCHER = re.compile(r"(\w+)(\W+on\W+\w+)?")
theotherjimmy 43:2a7da56ebd24 119 def colorstring_to_escapecode(self, color_string):
theotherjimmy 43:2a7da56ebd24 120 """ Convert a color string from a string into an ascii escape code that
theotherjimmy 43:2a7da56ebd24 121 will print that color on the terminal.
theotherjimmy 43:2a7da56ebd24 122
theotherjimmy 43:2a7da56ebd24 123 Positional arguments:
theotherjimmy 43:2a7da56ebd24 124 color_string - the string to parse
theotherjimmy 43:2a7da56ebd24 125 """
theotherjimmy 43:2a7da56ebd24 126 match = re.match(self.COLOR_MATCHER, color_string)
theotherjimmy 43:2a7da56ebd24 127 if match:
theotherjimmy 43:2a7da56ebd24 128 return self.COLORS[match.group(1)] + \
theotherjimmy 43:2a7da56ebd24 129 (self.COLORS[match.group(2).strip().replace(" ", "_")]
theotherjimmy 43:2a7da56ebd24 130 if match.group(2) else "")
theotherjimmy 43:2a7da56ebd24 131 else:
theotherjimmy 43:2a7da56ebd24 132 return self.COLORS['default']
theotherjimmy 43:2a7da56ebd24 133
theotherjimmy 43:2a7da56ebd24 134 def print_in_color(self, event, msg):
theotherjimmy 43:2a7da56ebd24 135 """ Wrap a toolchain notifier in a colorizer. This colorizer will wrap
theotherjimmy 43:2a7da56ebd24 136 notifications in a color if the severity matches a color in the
theotherjimmy 43:2a7da56ebd24 137 CLI_COLOR_MAP.
theotherjimmy 43:2a7da56ebd24 138 """
theotherjimmy 43:2a7da56ebd24 139 """The notification function inself"""
theotherjimmy 43:2a7da56ebd24 140 if sys.stdout.isatty() and event.get('severity', None) in CLI_COLOR_MAP:
theotherjimmy 43:2a7da56ebd24 141 sys.stdout.write(self.colorstring_to_escapecode(
theotherjimmy 43:2a7da56ebd24 142 CLI_COLOR_MAP[event['severity']]))
theotherjimmy 43:2a7da56ebd24 143 print(msg)
theotherjimmy 43:2a7da56ebd24 144 sys.stdout.write(self.colorstring_to_escapecode('default'))
theotherjimmy 43:2a7da56ebd24 145 else:
theotherjimmy 43:2a7da56ebd24 146 print(msg)