Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python2.7
      2 
      3 # Copyright 2013, ARM Limited
      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 
     33 
     34 __need_newline__ = False
     35 __last_name_length__ = 0
     36 
     37 
     38 # If the last printed character was not a newline, print one.
     39 def EnsureNewLine():
     40   global __need_newline__
     41 
     42   if __need_newline__:
     43     __need_newline__ = False
     44     sys.stdout.write('\n')
     45 
     46 
     47 # Like print, but insert a newline if necessary to avoid corrupting the status
     48 # display (provided by UpdateProgress).
     49 def Print(string):
     50   global __last_name_length__
     51 
     52   EnsureNewLine()
     53   print string
     54   __last_name_length__ = 0
     55 
     56 
     57 # Display the run progress:
     58 # [time| progress|+ passed|- failed]  Name
     59 def UpdateProgress(start_time, passed, failed, count, verbose, name):
     60   global __need_newline__
     61   global __last_name_length__
     62 
     63   minutes, seconds = divmod(time.time() - start_time, 60)
     64   progress = float(passed + failed) / count * 100
     65   passed_colour = '\x1b[32m' if passed != 0 else ''
     66   failed_colour = '\x1b[31m' if failed != 0 else ''
     67   indicator = '[%02d:%02d| %3d%%|'
     68   indicator += passed_colour + '+ %d\x1b[0m|'
     69   indicator += failed_colour + '- %d\x1b[0m]'
     70 
     71   if verbose:
     72     # In verbose mode, put every status display on its own line.
     73     EnsureNewLine()
     74   else:
     75     # Otherwise, overwrite the old progress display.
     76     sys.stdout.write('\r')
     77   sys.stdout.write(indicator % (minutes, seconds, progress, passed, failed))
     78 
     79   # Write the test name after the progress indicator.
     80   sys.stdout.write('  ' + name)
     81   name_length = len(name)
     82 
     83   # Append spaces to hide the previous test name.
     84   spaces = __last_name_length__ - name_length
     85   if spaces > 0:
     86     sys.stdout.write(' ' * spaces)
     87   __last_name_length__ = name_length
     88 
     89   # We haven't printed a newline, so any subsequent print output (with verbose
     90   # logs or error reports) will need to print one.
     91   __need_newline__ = True
     92 
     93 
     94