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