<s> """<STR_LIT>""" <EOL> import gettext <EOL> import heapq <EOL> import os <EOL> import unittest <EOL> import sys <EOL> import time <EOL> gettext . install ( '<STR_LIT>' , unicode = <NUM_LIT:1> ) <EOL> from nose import config <EOL> from nose import core <EOL> from nose import result <EOL> from nova import log as logging <EOL> class _AnsiColorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> _colors = dict ( black = <NUM_LIT:30> , red = <NUM_LIT> , green = <NUM_LIT:32> , yellow = <NUM_LIT> , <EOL> blue = <NUM_LIT> , magenta = <NUM_LIT> , cyan = <NUM_LIT> , white = <NUM_LIT> ) <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def supported ( cls , stream = sys . stdout ) : <EOL> """<STR_LIT>""" <EOL> if not stream . isatty ( ) : <EOL> return False <EOL> try : <EOL> import curses <EOL> except ImportError : <EOL> return False <EOL> else : <EOL> try : <EOL> try : <EOL> return curses . tigetnum ( "<STR_LIT>" ) > <NUM_LIT:2> <EOL> except curses . error : <EOL> curses . setupterm ( ) <EOL> return curses . tigetnum ( "<STR_LIT>" ) > <NUM_LIT:2> <EOL> except : <EOL> raise <EOL> return False <EOL> supported = classmethod ( supported ) <EOL> def write ( self , text , color ) : <EOL> """<STR_LIT>""" <EOL> color = self . _colors [ color ] <EOL> self . stream . write ( '<STR_LIT>' % ( color , text ) ) <EOL> class _Win32Colorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> from win32console import GetStdHandle , STD_OUT_HANDLE , FOREGROUND_RED , FOREGROUND_BLUE , FOREGROUND_GREEN , FOREGROUND_INTENSITY <EOL> red , green , blue , bold = ( FOREGROUND_RED , FOREGROUND_GREEN , <EOL> FOREGROUND_BLUE , FOREGROUND_INTENSITY ) <EOL> self . stream = stream <EOL> self . screenBuffer = GetStdHandle ( STD_OUT_HANDLE ) <EOL> self . _colors = { <EOL> '<STR_LIT>' : red | green | blue , <EOL> '<STR_LIT>' : red | bold , <EOL> '<STR_LIT>' : green | bold , <EOL> '<STR_LIT>' : blue | bold , <EOL> '<STR_LIT>' : red | green | bold , <EOL> '<STR_LIT>' : red | blue | bold , <EOL> '<STR_LIT>' : green | blue | bold , <EOL> '<STR_LIT>' : red | green | blue | bold <EOL> } <EOL> def supported ( cls , stream = sys . stdout ) : <EOL> try : <EOL> import win32console <EOL> screenBuffer = win32console . GetStdHandle ( <EOL> win32console . STD_OUT_HANDLE ) <EOL> except ImportError : <EOL> return False <EOL> import pywintypes <EOL> try : <EOL> screenBuffer . SetConsoleTextAttribute ( <EOL> win32console . FOREGROUND_RED | <EOL> win32console . FOREGROUND_GREEN | <EOL> win32console . FOREGROUND_BLUE ) <EOL> except pywintypes . error : <EOL> return False <EOL> else : <EOL> return True <EOL> supported = classmethod ( supported ) <EOL> def write ( self , text , color ) : <EOL> color = self . _colors [ color ] <EOL> self . screenBuffer . SetConsoleTextAttribute ( color ) <EOL> self . stream . write ( text ) <EOL> self . screenBuffer . SetConsoleTextAttribute ( self . _colors [ '<STR_LIT>' ] ) <EOL> class _NullColorizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def supported ( cls , stream = sys . stdout ) : <EOL> return True <EOL> supported = classmethod ( supported ) <EOL> def write ( self , text , color ) : <EOL> self . stream . write ( text ) <EOL> def get_elapsed_time_color ( elapsed_time ) : <EOL> if elapsed_time > <NUM_LIT:1.0> : <EOL> return '<STR_LIT>' <EOL> elif elapsed_time > <NUM_LIT> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> class NovaTestResult ( result . TextTestResult ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . show_elapsed = kw . pop ( '<STR_LIT>' ) <EOL> result . TextTestResult . __init__ ( self , * args , ** kw ) <EOL> self . num_slow_tests = <NUM_LIT:5> <EOL> self . slow_tests = [ ] <EOL> self . _last_case = None <EOL> self . colorizer = None <EOL> stdout = sys . stdout <EOL> sys . stdout = sys . __stdout__ <EOL> for colorizer in [ _Win32Colorizer , _AnsiColorizer , _NullColorizer ] : <EOL> if colorizer . supported ( ) : <EOL> self . colorizer = colorizer ( self . stream ) <EOL> break <EOL> sys . stdout = stdout <EOL> self . start_time = time . time ( ) <EOL> def getDescription ( self , test ) : <EOL> return str ( test ) <EOL> def _handleElapsedTime ( self , test ) : <EOL> self . elapsed_time = time . time ( ) - self . start_time <EOL> item = ( self . elapsed_time , test ) <EOL> if len ( self . slow_tests ) >= self . num_slow_tests : <EOL> heapq . heappushpop ( self . slow_tests , item ) <EOL> else : <EOL> heapq . heappush ( self . slow_tests , item ) <EOL> def _writeElapsedTime ( self , test ) : <EOL> color = get_elapsed_time_color ( self . elapsed_time ) <EOL> self . colorizer . write ( "<STR_LIT>" % self . elapsed_time , color ) <EOL> def _writeResult ( self , test , long_result , color , short_result , success ) : <EOL> if self . showAll : <EOL> self . colorizer . write ( long_result , color ) <EOL> if self . show_elapsed and success : <EOL> self . _writeElapsedTime ( test ) <EOL> self . stream . writeln ( ) <EOL> elif self . dots : <EOL> self . stream . write ( short_result ) <EOL> self . stream . flush ( ) <EOL> def addSuccess ( self , test ) : <EOL> unittest . TestResult . addSuccess ( self , test ) <EOL> self . _handleElapsedTime ( test ) <EOL> self . _writeResult ( test , '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT:.>' , True ) <EOL> def addFailure ( self , test , err ) : <EOL> unittest . TestResult . addFailure ( self , test , err ) <EOL> self . _handleElapsedTime ( test ) <EOL> self . _writeResult ( test , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:F>' , False ) <EOL> def addError ( self , test , err ) : <EOL> """<STR_LIT>""" <EOL> self . _handleElapsedTime ( test ) <EOL> stream = getattr ( self , '<STR_LIT>' , None ) <EOL> ec , ev , tb = err <EOL> try : <EOL> exc_info = self . _exc_info_to_string ( err , test ) <EOL> except TypeError : <EOL> exc_info = self . _exc_info_to_string ( err ) <EOL> for cls , ( storage , label , isfail ) in self . errorClasses . items ( ) : <EOL> if result . isclass ( ec ) and issubclass ( ec , cls ) : <EOL> if isfail : <EOL> test . passed = False <EOL> storage . append ( ( test , exc_info ) ) <EOL> if stream is not None : <EOL> if self . showAll : <EOL> message = [ label ] <EOL> detail = result . _exception_detail ( err [ <NUM_LIT:1> ] ) <EOL> if detail : <EOL> message . append ( detail ) <EOL> stream . writeln ( "<STR_LIT>" . join ( message ) ) <EOL> elif self . dots : <EOL> stream . write ( label [ : <NUM_LIT:1> ] ) <EOL> return <EOL> self . errors . append ( ( test , exc_info ) ) <EOL> test . passed = False <EOL> if stream is not None : <EOL> self . _writeResult ( test , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:E>' , False ) <EOL> def startTest ( self , test ) : <EOL> unittest . TestResult . startTest ( self , test ) <EOL> self . start_time = time . time ( ) <EOL> current_case = test . test . __class__ . __name__ <EOL> if self . showAll : <EOL> if current_case != self . _last_case : <EOL> self . stream . writeln ( current_case ) <EOL> self . _last_case = current_case <EOL> self . stream . write ( <EOL> '<STR_LIT>' % str ( test . test . _testMethodName ) . ljust ( <NUM_LIT> ) ) <EOL> self . stream . flush ( ) <EOL> class NovaTestRunner ( core . TextTestRunner ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . show_elapsed = kwargs . pop ( '<STR_LIT>' ) <EOL> core . TextTestRunner . __init__ ( self , * args , ** kwargs ) <EOL> def _makeResult ( self ) : <EOL> return NovaTestResult ( self . stream , <EOL> self . descriptions , <EOL> self . verbosity , <EOL> self . config , <EOL> show_elapsed = self . show_elapsed ) <EOL> def _writeSlowTests ( self , result_ ) : <EOL> slow_tests = [ item for item in result_ . slow_tests <EOL> if get_elapsed_time_color ( item [ <NUM_LIT:0> ] ) != '<STR_LIT>' ] <EOL> if slow_tests : <EOL> slow_total_time = sum ( item [ <NUM_LIT:0> ] for item in slow_tests ) <EOL> self . stream . writeln ( "<STR_LIT>" <EOL> % ( len ( slow_tests ) , slow_total_time ) ) <EOL> for elapsed_time , test in sorted ( slow_tests , reverse = True ) : <EOL> time_str = "<STR_LIT>" % elapsed_time <EOL> self . stream . writeln ( "<STR_LIT>" % ( time_str . ljust ( <NUM_LIT:10> ) , test ) ) <EOL> def run ( self , test ) : <EOL> result_ = core . TextTestRunner . run ( self , test ) <EOL> if self . show_elapsed : <EOL> self . _writeSlowTests ( result_ ) <EOL> return result_ <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> logging . setup ( ) <EOL> show_elapsed = True <EOL> argv = [ ] <EOL> for x in sys . argv : <EOL> if x . startswith ( '<STR_LIT>' ) : <EOL> argv . append ( '<STR_LIT>' % x ) <EOL> elif x . startswith ( '<STR_LIT>' ) : <EOL> show_elapsed = False <EOL> else : <EOL> argv . append ( x ) <EOL> testdir = os . path . abspath ( os . path . join ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> c = config . Config ( stream = sys . stdout , <EOL> env = os . environ , <EOL> verbosity = <NUM_LIT:3> , <EOL> workingDir = testdir , <EOL> plugins = core . DefaultPluginManager ( ) ) <EOL> runner = NovaTestRunner ( stream = c . stream , <EOL> verbosity = c . verbosity , <EOL> config = c , <EOL> show_elapsed = show_elapsed ) <EOL> sys . exit ( not core . run ( config = c , testRunner = runner , argv = argv ) ) </s>
