blob: 57e22e3b614497aad6df4a0cdc3361970c005b7d [file] [log] [blame]
snickolls-arm2decd2c2024-01-17 11:24:49 +00001#!/usr/bin/env python3
armvixl5799d6c2014-05-01 11:05:00 +01002
Alexandre Ramesb78f1392016-07-01 14:22:22 +01003# Copyright 2014, VIXL authors
armvixl5799d6c2014-05-01 11:05:00 +01004# 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
30import sys
31import time
armvixl4a102ba2014-07-14 09:02:40 +010032import multiprocessing
armvixl5799d6c2014-05-01 11:05:00 +010033
armvixldb644342015-07-21 11:37:10 +010034output_redirected_to_file = not sys.stdout.isatty()
armvixl5799d6c2014-05-01 11:05:00 +010035
armvixldb644342015-07-21 11:37:10 +010036def ColourCode(colour):
37 return '' if output_redirected_to_file else colour
38
39COLOUR_GREEN = ColourCode("\x1b[0;32m")
40COLOUR_ORANGE = ColourCode("\x1b[0;33m")
41COLOUR_RED = ColourCode("\x1b[0;31m")
42NO_COLOUR = ColourCode("\x1b[0m")
43
44# Indicates the 'type' of the last printed line.
45LINE_TYPE_NONE = 0
46# Any type below this one is overwritable.
47LINE_TYPE_OVERWRITABLE = 1
48LINE_TYPE_PROGRESS = 2
49LINE_TYPE_LINTER = 3
50
armvixl4a102ba2014-07-14 09:02:40 +010051__print_lock__ = multiprocessing.Lock()
armvixldb644342015-07-21 11:37:10 +010052__last_overwritable_line_length__ = multiprocessing.Value('i', 0)
53__last_line_type__ = multiprocessing.Value('i', LINE_TYPE_NONE)
armvixl5799d6c2014-05-01 11:05:00 +010054
55
armvixl5799d6c2014-05-01 11:05:00 +010056def EnsureNewLine():
armvixldb644342015-07-21 11:37:10 +010057 if __last_line_type__.value >= LINE_TYPE_OVERWRITABLE:
58 sys.stdout.write('\n')
59 __last_line_type__.value = LINE_TYPE_NONE
armvixl5799d6c2014-05-01 11:05:00 +010060
armvixldb644342015-07-21 11:37:10 +010061
62def 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
69def Print(string, has_lock = False):
70 if not has_lock: __print_lock__.acquire()
71
72 if __last_line_type__.value != LINE_TYPE_NONE:
armvixl5799d6c2014-05-01 11:05:00 +010073 sys.stdout.write('\n')
74
armvixldb644342015-07-21 11:37:10 +010075 PrintInternal(string)
76 sys.stdout.write('\n')
armvixl4a102ba2014-07-14 09:02:40 +010077 __last_overwritable_line_length__.value = 0
armvixldb644342015-07-21 11:37:10 +010078 __last_line_type__.value = LINE_TYPE_NONE
79
80 if not has_lock: __print_lock__.release()
armvixl4a102ba2014-07-14 09:02:40 +010081
82
armvixldb644342015-07-21 11:37:10 +010083# Lines of a specific type only overwrite and can only be overwritten by lines
84# of the same type.
85def PrintOverwritableLine(string, has_lock = False, type = LINE_TYPE_NONE):
86 if not has_lock: __print_lock__.acquire()
armvixl4a102ba2014-07-14 09:02:40 +010087
armvixldb644342015-07-21 11:37:10 +010088 if (__last_line_type__.value != type) and \
89 (__last_line_type__.value >= LINE_TYPE_OVERWRITABLE):
90 sys.stdout.write('\n')
armvixl4a102ba2014-07-14 09:02:40 +010091
armvixldb644342015-07-21 11:37:10 +010092 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()
armvixl4a102ba2014-07-14 09:02:40 +010098
armvixldb644342015-07-21 11:37:10 +010099 __last_overwritable_line_length__.value = len(string)
100 __last_line_type__.value = type
armvixl4a102ba2014-07-14 09:02:40 +0100101
armvixldb644342015-07-21 11:37:10 +0100102 if not has_lock: __print_lock__.release()
armvixl5799d6c2014-05-01 11:05:00 +0100103
104
105# Display the run progress:
armvixldb644342015-07-21 11:37:10 +0100106# prefix [time| progress|+ passed|- failed] name
Anthony Barbier88e1d032019-06-13 15:20:20 +0100107def UpdateProgress(start_time, passed, failed, count, skipped, known_failures,
108 name, prefix = '', prevent_next_overwrite = False,
109 has_lock = False):
armvixl5799d6c2014-05-01 11:05:00 +0100110 minutes, seconds = divmod(time.time() - start_time, 60)
Anthony Barbier89eefef2019-07-05 11:15:13 +0100111 progress = float(passed + failed + skipped) / max(1, count) * 100
armvixldb644342015-07-21 11:37:10 +0100112 passed_colour = COLOUR_GREEN if passed != 0 else ''
113 failed_colour = COLOUR_RED if failed != 0 else ''
Anthony Barbier88e1d032019-06-13 15:20:20 +0100114 skipped_colour = COLOUR_ORANGE if (skipped + known_failures) != 0 else ''
armvixl5799d6c2014-05-01 11:05:00 +0100115 indicator = '[%02d:%02d| %3d%%|'
armvixldb644342015-07-21 11:37:10 +0100116 indicator += passed_colour + '+ %d' + NO_COLOUR + '|'
Anthony Barbier88e1d032019-06-13 15:20:20 +0100117 indicator += failed_colour + '- %d' + NO_COLOUR + '|'
118 indicator += skipped_colour + '? %d' + NO_COLOUR + ']'
armvixl5799d6c2014-05-01 11:05:00 +0100119
armvixl4a102ba2014-07-14 09:02:40 +0100120 progress_string = prefix
Anthony Barbier88e1d032019-06-13 15:20:20 +0100121 progress_string += indicator % (minutes, seconds, progress, passed, failed,
122 skipped + known_failures)
armvixl4a102ba2014-07-14 09:02:40 +0100123 progress_string += ' ' + name
armvixl5799d6c2014-05-01 11:05:00 +0100124
armvixldb644342015-07-21 11:37:10 +0100125 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