1 #!/usr/bin/env python2.7 2 3 # Copyright 2014, VIXL authors 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions are met: 8 # 9 # * Redistributions of source code must retain the above copyright notice, 10 # this list of conditions and the following disclaimer. 11 # * Redistributions in binary form must reproduce the above copyright notice, 12 # this list of conditions and the following disclaimer in the documentation 13 # and/or other materials provided with the distribution. 14 # * Neither the name of ARM Limited nor the names of its contributors may be 15 # used to endorse or promote products derived from this software without 16 # specific prior written permission. 17 # 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 30 import sys 31 import time 32 import multiprocessing 33 34 output_redirected_to_file = not sys.stdout.isatty() 35 36 def ColourCode(colour): 37 return '' if output_redirected_to_file else colour 38 39 COLOUR_GREEN = ColourCode("\x1b[0;32m") 40 COLOUR_ORANGE = ColourCode("\x1b[0;33m") 41 COLOUR_RED = ColourCode("\x1b[0;31m") 42 NO_COLOUR = ColourCode("\x1b[0m") 43 44 # Indicates the 'type' of the last printed line. 45 LINE_TYPE_NONE = 0 46 # Any type below this one is overwritable. 47 LINE_TYPE_OVERWRITABLE = 1 48 LINE_TYPE_PROGRESS = 2 49 LINE_TYPE_LINTER = 3 50 51 __print_lock__ = multiprocessing.Lock() 52 __last_overwritable_line_length__ = multiprocessing.Value('i', 0) 53 __last_line_type__ = multiprocessing.Value('i', LINE_TYPE_NONE) 54 55 56 def EnsureNewLine(): 57 if __last_line_type__.value >= LINE_TYPE_OVERWRITABLE: 58 sys.stdout.write('\n') 59 __last_line_type__.value = LINE_TYPE_NONE 60 61 62 def PrintInternal(string): 63 sys.stdout.write(string) 64 spaces = __last_overwritable_line_length__.value - len(string) 65 if spaces > 0: 66 sys.stdout.write(' ' * spaces) 67 68 69 def Print(string, has_lock = False): 70 if not has_lock: __print_lock__.acquire() 71 72 if __last_line_type__.value != LINE_TYPE_NONE: 73 sys.stdout.write('\n') 74 75 PrintInternal(string) 76 sys.stdout.write('\n') 77 __last_overwritable_line_length__.value = 0 78 __last_line_type__.value = LINE_TYPE_NONE 79 80 if not has_lock: __print_lock__.release() 81 82 83 # Lines of a specific type only overwrite and can only be overwritten by lines 84 # of the same type. 85 def PrintOverwritableLine(string, has_lock = False, type = LINE_TYPE_NONE): 86 if not has_lock: __print_lock__.acquire() 87 88 if (__last_line_type__.value != type) and \ 89 (__last_line_type__.value >= LINE_TYPE_OVERWRITABLE): 90 sys.stdout.write('\n') 91 92 PrintInternal(string) 93 if not output_redirected_to_file: 94 sys.stdout.write('\r') 95 else: 96 sys.stdout.write('\n') 97 sys.stdout.flush() 98 99 __last_overwritable_line_length__.value = len(string) 100 __last_line_type__.value = type 101 102 if not has_lock: __print_lock__.release() 103 104 105 # Display the run progress: 106 # prefix [time| progress|+ passed|- failed] name 107 def UpdateProgress(start_time, passed, failed, count, name, prefix = '', 108 prevent_next_overwrite = False, has_lock = False): 109 minutes, seconds = divmod(time.time() - start_time, 60) 110 progress = float(passed + failed) / count * 100 111 passed_colour = COLOUR_GREEN if passed != 0 else '' 112 failed_colour = COLOUR_RED if failed != 0 else '' 113 indicator = '[%02d:%02d| %3d%%|' 114 indicator += passed_colour + '+ %d' + NO_COLOUR + '|' 115 indicator += failed_colour + '- %d' + NO_COLOUR + ']' 116 117 progress_string = prefix 118 progress_string += indicator % (minutes, seconds, progress, passed, failed) 119 progress_string += ' ' + name 120 121 PrintOverwritableLine(progress_string, type = LINE_TYPE_PROGRESS, 122 has_lock = has_lock) 123 if prevent_next_overwrite: 124 sys.stdout.write('\n') 125 __last_line_type__.value = LINE_TYPE_NONE 126