snickolls-arm | 2decd2c | 2024-01-17 11:24:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 2 | |
Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 3 | # Copyright 2014, VIXL authors |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 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 |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 32 | import multiprocessing |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 33 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 34 | output_redirected_to_file = not sys.stdout.isatty() |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 35 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 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 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 51 | __print_lock__ = multiprocessing.Lock() |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 52 | __last_overwritable_line_length__ = multiprocessing.Value('i', 0) |
| 53 | __last_line_type__ = multiprocessing.Value('i', LINE_TYPE_NONE) |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 54 | |
| 55 | |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 56 | def EnsureNewLine(): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 57 | if __last_line_type__.value >= LINE_TYPE_OVERWRITABLE: |
| 58 | sys.stdout.write('\n') |
| 59 | __last_line_type__.value = LINE_TYPE_NONE |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 60 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 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: |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 73 | sys.stdout.write('\n') |
| 74 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 75 | PrintInternal(string) |
| 76 | sys.stdout.write('\n') |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 77 | __last_overwritable_line_length__.value = 0 |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 78 | __last_line_type__.value = LINE_TYPE_NONE |
| 79 | |
| 80 | if not has_lock: __print_lock__.release() |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 81 | |
| 82 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 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() |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 87 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 88 | if (__last_line_type__.value != type) and \ |
| 89 | (__last_line_type__.value >= LINE_TYPE_OVERWRITABLE): |
| 90 | sys.stdout.write('\n') |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 91 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 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() |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 98 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 99 | __last_overwritable_line_length__.value = len(string) |
| 100 | __last_line_type__.value = type |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 101 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 102 | if not has_lock: __print_lock__.release() |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 103 | |
| 104 | |
| 105 | # Display the run progress: |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 106 | # prefix [time| progress|+ passed|- failed] name |
Anthony Barbier | 88e1d03 | 2019-06-13 15:20:20 +0100 | [diff] [blame] | 107 | def UpdateProgress(start_time, passed, failed, count, skipped, known_failures, |
| 108 | name, prefix = '', prevent_next_overwrite = False, |
| 109 | has_lock = False): |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 110 | minutes, seconds = divmod(time.time() - start_time, 60) |
Anthony Barbier | 89eefef | 2019-07-05 11:15:13 +0100 | [diff] [blame] | 111 | progress = float(passed + failed + skipped) / max(1, count) * 100 |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 112 | passed_colour = COLOUR_GREEN if passed != 0 else '' |
| 113 | failed_colour = COLOUR_RED if failed != 0 else '' |
Anthony Barbier | 88e1d03 | 2019-06-13 15:20:20 +0100 | [diff] [blame] | 114 | skipped_colour = COLOUR_ORANGE if (skipped + known_failures) != 0 else '' |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 115 | indicator = '[%02d:%02d| %3d%%|' |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 116 | indicator += passed_colour + '+ %d' + NO_COLOUR + '|' |
Anthony Barbier | 88e1d03 | 2019-06-13 15:20:20 +0100 | [diff] [blame] | 117 | indicator += failed_colour + '- %d' + NO_COLOUR + '|' |
| 118 | indicator += skipped_colour + '? %d' + NO_COLOUR + ']' |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 119 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 120 | progress_string = prefix |
Anthony Barbier | 88e1d03 | 2019-06-13 15:20:20 +0100 | [diff] [blame] | 121 | progress_string += indicator % (minutes, seconds, progress, passed, failed, |
| 122 | skipped + known_failures) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 123 | progress_string += ' ' + name |
armvixl | 5799d6c | 2014-05-01 11:05:00 +0100 | [diff] [blame] | 124 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 125 | PrintOverwritableLine(progress_string, type = LINE_TYPE_PROGRESS, |
| 126 | has_lock = has_lock) |
| 127 | if prevent_next_overwrite: |
| 128 | sys.stdout.write('\n') |
| 129 | __last_line_type__.value = LINE_TYPE_NONE |