<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __revision__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> import errno <EOL> import os <EOL> import os . path <EOL> import re <EOL> import shutil <EOL> import stat <EOL> import string <EOL> import sys <EOL> import tempfile <EOL> import time <EOL> import traceback <EOL> import types <EOL> import UserList <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> try : <EOL> import difflib <EOL> except ImportError : <EOL> __all__ . append ( '<STR_LIT>' ) <EOL> def is_List ( e ) : <EOL> return type ( e ) is types . ListType or isinstance ( e , UserList . UserList ) <EOL> try : <EOL> from UserString import UserString <EOL> except ImportError : <EOL> class UserString : <EOL> pass <EOL> if hasattr ( types , '<STR_LIT>' ) : <EOL> def is_String ( e ) : <EOL> return type ( e ) is types . StringType or type ( e ) is types . UnicodeType or isinstance ( e , UserString ) <EOL> else : <EOL> def is_String ( e ) : <EOL> return type ( e ) is types . StringType or isinstance ( e , UserString ) <EOL> tempfile . template = '<STR_LIT>' <EOL> if os . name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> tempfile . template = '<STR_LIT>' + str ( os . getpid ( ) ) + '<STR_LIT:.>' <EOL> else : <EOL> tempfile . template = '<STR_LIT>' <EOL> re_space = re . compile ( '<STR_LIT>' ) <EOL> _Cleanup = [ ] <EOL> _chain_to_exitfunc = None <EOL> def _clean ( ) : <EOL> global _Cleanup <EOL> cleanlist = filter ( None , _Cleanup ) <EOL> del _Cleanup [ : ] <EOL> cleanlist . reverse ( ) <EOL> for test in cleanlist : <EOL> test . cleanup ( ) <EOL> if _chain_to_exitfunc : <EOL> _chain_to_exitfunc ( ) <EOL> try : <EOL> import atexit <EOL> except ImportError : <EOL> try : <EOL> _chain_to_exitfunc = sys . exitfunc <EOL> except AttributeError : <EOL> pass <EOL> sys . exitfunc = _clean <EOL> else : <EOL> atexit . register ( _clean ) <EOL> try : <EOL> zip <EOL> except NameError : <EOL> def zip ( * lists ) : <EOL> result = [ ] <EOL> for i in xrange ( min ( map ( len , lists ) ) ) : <EOL> result . append ( tuple ( map ( lambda l , i = i : l [ i ] , lists ) ) ) <EOL> return result <EOL> class Collector : <EOL> def __init__ ( self , top ) : <EOL> self . entries = [ top ] <EOL> def __call__ ( self , arg , dirname , names ) : <EOL> pathjoin = lambda n , d = dirname : os . path . join ( d , n ) <EOL> self . entries . extend ( map ( pathjoin , names ) ) <EOL> def _caller ( tblist , skip ) : <EOL> string = "<STR_LIT>" <EOL> arr = [ ] <EOL> for file , line , name , text in tblist : <EOL> if file [ - <NUM_LIT:10> : ] == "<STR_LIT>" : <EOL> break <EOL> arr = [ ( file , line , name , text ) ] + arr <EOL> atfrom = "<STR_LIT>" <EOL> for file , line , name , text in arr [ skip : ] : <EOL> if name in ( "<STR_LIT:?>" , "<STR_LIT>" ) : <EOL> name = "<STR_LIT>" <EOL> else : <EOL> name = "<STR_LIT>" + name + "<STR_LIT:)>" <EOL> string = string + ( "<STR_LIT>" % ( atfrom , line , file , name ) ) <EOL> atfrom = "<STR_LIT>" <EOL> return string <EOL> def fail_test ( self = None , condition = <NUM_LIT:1> , function = None , skip = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return <EOL> if not function is None : <EOL> function ( ) <EOL> of = "<STR_LIT>" <EOL> desc = "<STR_LIT>" <EOL> sep = "<STR_LIT:U+0020>" <EOL> if not self is None : <EOL> if self . program : <EOL> of = "<STR_LIT>" + self . program <EOL> sep = "<STR_LIT>" <EOL> if self . description : <EOL> desc = "<STR_LIT>" + self . description + "<STR_LIT:]>" <EOL> sep = "<STR_LIT>" <EOL> at = _caller ( traceback . extract_stack ( ) , skip ) <EOL> sys . stderr . write ( "<STR_LIT>" + of + desc + sep + at ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def no_result ( self = None , condition = <NUM_LIT:1> , function = None , skip = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return <EOL> if not function is None : <EOL> function ( ) <EOL> of = "<STR_LIT>" <EOL> desc = "<STR_LIT>" <EOL> sep = "<STR_LIT:U+0020>" <EOL> if not self is None : <EOL> if self . program : <EOL> of = "<STR_LIT>" + self . program <EOL> sep = "<STR_LIT>" <EOL> if self . description : <EOL> desc = "<STR_LIT>" + self . description + "<STR_LIT:]>" <EOL> sep = "<STR_LIT>" <EOL> if os . environ . get ( '<STR_LIT>' ) : <EOL> at = _caller ( traceback . extract_stack ( ) , skip ) <EOL> sys . stderr . write ( "<STR_LIT>" + of + desc + sep + at ) <EOL> else : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def pass_test ( self = None , condition = <NUM_LIT:1> , function = None ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return <EOL> if not function is None : <EOL> function ( ) <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def match_exact ( lines = None , matches = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not is_List ( lines ) : <EOL> lines = string . split ( lines , "<STR_LIT:\n>" ) <EOL> if not is_List ( matches ) : <EOL> matches = string . split ( matches , "<STR_LIT:\n>" ) <EOL> if len ( lines ) != len ( matches ) : <EOL> return <EOL> for i in range ( len ( lines ) ) : <EOL> if lines [ i ] != matches [ i ] : <EOL> return <EOL> return <NUM_LIT:1> <EOL> def match_re ( lines = None , res = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not is_List ( lines ) : <EOL> lines = string . split ( lines , "<STR_LIT:\n>" ) <EOL> if not is_List ( res ) : <EOL> res = string . split ( res , "<STR_LIT:\n>" ) <EOL> if len ( lines ) != len ( res ) : <EOL> return <EOL> for i in range ( len ( lines ) ) : <EOL> s = "<STR_LIT>" + res [ i ] + "<STR_LIT:$>" <EOL> try : <EOL> expr = re . compile ( s ) <EOL> except re . error , e : <EOL> msg = "<STR_LIT>" <EOL> raise re . error , msg % ( repr ( s ) , e [ <NUM_LIT:0> ] ) <EOL> if not expr . search ( lines [ i ] ) : <EOL> return <EOL> return <NUM_LIT:1> <EOL> def match_re_dotall ( lines = None , res = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not type ( lines ) is type ( "<STR_LIT>" ) : <EOL> lines = string . join ( lines , "<STR_LIT:\n>" ) <EOL> if not type ( res ) is type ( "<STR_LIT>" ) : <EOL> res = string . join ( res , "<STR_LIT:\n>" ) <EOL> s = "<STR_LIT>" + res + "<STR_LIT:$>" <EOL> try : <EOL> expr = re . compile ( s , re . DOTALL ) <EOL> except re . error , e : <EOL> msg = "<STR_LIT>" <EOL> raise re . error , msg % ( repr ( s ) , e [ <NUM_LIT:0> ] ) <EOL> if expr . match ( lines ) : <EOL> return <NUM_LIT:1> <EOL> try : <EOL> import difflib <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> def simple_diff ( a , b , fromfile = '<STR_LIT>' , tofile = '<STR_LIT>' , <EOL> fromfiledate = '<STR_LIT>' , tofiledate = '<STR_LIT>' , n = <NUM_LIT:3> , lineterm = '<STR_LIT:\n>' ) : <EOL> """<STR_LIT>""" <EOL> sm = difflib . SequenceMatcher ( None , a , b ) <EOL> def comma ( x1 , x2 ) : <EOL> return x1 + <NUM_LIT:1> == x2 and str ( x2 ) or '<STR_LIT>' % ( x1 + <NUM_LIT:1> , x2 ) <EOL> result = [ ] <EOL> for op , a1 , a2 , b1 , b2 in sm . get_opcodes ( ) : <EOL> if op == '<STR_LIT>' : <EOL> result . append ( "<STR_LIT>" % ( comma ( a1 , a2 ) , b1 ) ) <EOL> result . extend ( map ( lambda l : '<STR_LIT>' + l , a [ a1 : a2 ] ) ) <EOL> elif op == '<STR_LIT>' : <EOL> result . append ( "<STR_LIT>" % ( a1 , comma ( b1 , b2 ) ) ) <EOL> result . extend ( map ( lambda l : '<STR_LIT>' + l , b [ b1 : b2 ] ) ) <EOL> elif op == '<STR_LIT:replace>' : <EOL> result . append ( "<STR_LIT>" % ( comma ( a1 , a2 ) , comma ( b1 , b2 ) ) ) <EOL> result . extend ( map ( lambda l : '<STR_LIT>' + l , a [ a1 : a2 ] ) ) <EOL> result . append ( '<STR_LIT>' ) <EOL> result . extend ( map ( lambda l : '<STR_LIT>' + l , b [ b1 : b2 ] ) ) <EOL> return result <EOL> def diff_re ( a , b , fromfile = '<STR_LIT>' , tofile = '<STR_LIT>' , <EOL> fromfiledate = '<STR_LIT>' , tofiledate = '<STR_LIT>' , n = <NUM_LIT:3> , lineterm = '<STR_LIT:\n>' ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> diff = len ( a ) - len ( b ) <EOL> if diff < <NUM_LIT:0> : <EOL> a = a + [ '<STR_LIT>' ] * ( - diff ) <EOL> elif diff > <NUM_LIT:0> : <EOL> b = b + [ '<STR_LIT>' ] * diff <EOL> i = <NUM_LIT:0> <EOL> for aline , bline in zip ( a , b ) : <EOL> s = "<STR_LIT>" + aline + "<STR_LIT:$>" <EOL> try : <EOL> expr = re . compile ( s ) <EOL> except re . error , e : <EOL> msg = "<STR_LIT>" <EOL> raise re . error , msg % ( repr ( s ) , e [ <NUM_LIT:0> ] ) <EOL> if not expr . search ( bline ) : <EOL> result . append ( "<STR_LIT>" % ( i + <NUM_LIT:1> , i + <NUM_LIT:1> ) ) <EOL> result . append ( '<STR_LIT>' + repr ( a [ i ] ) ) <EOL> result . append ( '<STR_LIT>' ) <EOL> result . append ( '<STR_LIT>' + repr ( b [ i ] ) ) <EOL> i = i + <NUM_LIT:1> <EOL> return result <EOL> if os . name == '<STR_LIT>' : <EOL> python_executable = os . path . join ( sys . prefix , '<STR_LIT>' ) <EOL> else : <EOL> python_executable = sys . executable <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> default_sleep_seconds = <NUM_LIT:2> <EOL> def where_is ( file , path = None , pathext = None ) : <EOL> if path is None : <EOL> path = os . environ [ '<STR_LIT>' ] <EOL> if is_String ( path ) : <EOL> path = string . split ( path , os . pathsep ) <EOL> if pathext is None : <EOL> pathext = os . environ [ '<STR_LIT>' ] <EOL> if is_String ( pathext ) : <EOL> pathext = string . split ( pathext , os . pathsep ) <EOL> for ext in pathext : <EOL> if string . lower ( ext ) == string . lower ( file [ - len ( ext ) : ] ) : <EOL> pathext = [ '<STR_LIT>' ] <EOL> break <EOL> for dir in path : <EOL> f = os . path . join ( dir , file ) <EOL> for ext in pathext : <EOL> fext = f + ext <EOL> if os . path . isfile ( fext ) : <EOL> return fext <EOL> return None <EOL> else : <EOL> def where_is ( file , path = None , pathext = None ) : <EOL> if path is None : <EOL> path = os . environ [ '<STR_LIT>' ] <EOL> if is_String ( path ) : <EOL> path = string . split ( path , os . pathsep ) <EOL> for dir in path : <EOL> f = os . path . join ( dir , file ) <EOL> if os . path . isfile ( f ) : <EOL> try : <EOL> st = os . stat ( f ) <EOL> except OSError : <EOL> continue <EOL> if stat . S_IMODE ( st [ stat . ST_MODE ] ) & <NUM_LIT:0> <NUM_LIT> : <EOL> return f <EOL> return None <EOL> default_sleep_seconds = <NUM_LIT:1> <EOL> try : <EOL> import subprocess <EOL> except ImportError : <EOL> import new <EOL> subprocess = new . module ( '<STR_LIT>' ) <EOL> subprocess . PIPE = '<STR_LIT>' <EOL> subprocess . STDOUT = '<STR_LIT>' <EOL> subprocess . mswindows = ( sys . platform == '<STR_LIT:win32>' ) <EOL> try : <EOL> import popen2 <EOL> popen2 . Popen3 <EOL> except AttributeError : <EOL> class Popen3 : <EOL> universal_newlines = <NUM_LIT:1> <EOL> def __init__ ( self , command , ** kw ) : <EOL> if sys . platform == '<STR_LIT:win32>' and command [ <NUM_LIT:0> ] == '<STR_LIT:">' : <EOL> command = '<STR_LIT:">' + command + '<STR_LIT:">' <EOL> ( stdin , stdout , stderr ) = os . popen3 ( '<STR_LIT:U+0020>' + command ) <EOL> self . stdin = stdin <EOL> self . stdout = stdout <EOL> self . stderr = stderr <EOL> def close_output ( self ) : <EOL> self . stdout . close ( ) <EOL> self . resultcode = self . stderr . close ( ) <EOL> def wait ( self ) : <EOL> resultcode = self . resultcode <EOL> if os . WIFEXITED ( resultcode ) : <EOL> return os . WEXITSTATUS ( resultcode ) <EOL> elif os . WIFSIGNALED ( resultcode ) : <EOL> return os . WTERMSIG ( resultcode ) <EOL> else : <EOL> return None <EOL> else : <EOL> try : <EOL> popen2 . Popen4 <EOL> except AttributeError : <EOL> class Popen4 ( popen2 . Popen3 ) : <EOL> childerr = None <EOL> def __init__ ( self , cmd , bufsize = - <NUM_LIT:1> ) : <EOL> p2cread , p2cwrite = os . pipe ( ) <EOL> c2pread , c2pwrite = os . pipe ( ) <EOL> self . pid = os . fork ( ) <EOL> if self . pid == <NUM_LIT:0> : <EOL> os . dup2 ( p2cread , <NUM_LIT:0> ) <EOL> os . dup2 ( c2pwrite , <NUM_LIT:1> ) <EOL> os . dup2 ( c2pwrite , <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:3> , popen2 . MAXFD ) : <EOL> try : <EOL> os . close ( i ) <EOL> except : pass <EOL> try : <EOL> os . execvp ( cmd [ <NUM_LIT:0> ] , cmd ) <EOL> finally : <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> os . close ( p2cread ) <EOL> self . tochild = os . fdopen ( p2cwrite , '<STR_LIT:w>' , bufsize ) <EOL> os . close ( c2pwrite ) <EOL> self . fromchild = os . fdopen ( c2pread , '<STR_LIT:r>' , bufsize ) <EOL> popen2 . _active . append ( self ) <EOL> popen2 . Popen4 = Popen4 <EOL> class Popen3 ( popen2 . Popen3 , popen2 . Popen4 ) : <EOL> universal_newlines = <NUM_LIT:1> <EOL> def __init__ ( self , command , ** kw ) : <EOL> if kw . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> apply ( popen2 . Popen4 . __init__ , ( self , command , <NUM_LIT:1> ) ) <EOL> else : <EOL> apply ( popen2 . Popen3 . __init__ , ( self , command , <NUM_LIT:1> ) ) <EOL> self . stdin = self . tochild <EOL> self . stdout = self . fromchild <EOL> self . stderr = self . childerr <EOL> def wait ( self , * args , ** kw ) : <EOL> resultcode = apply ( popen2 . Popen3 . wait , ( self , ) + args , kw ) <EOL> if os . WIFEXITED ( resultcode ) : <EOL> return os . WEXITSTATUS ( resultcode ) <EOL> elif os . WIFSIGNALED ( resultcode ) : <EOL> return os . WTERMSIG ( resultcode ) <EOL> else : <EOL> return None <EOL> subprocess . Popen = Popen3 <EOL> PIPE = subprocess . PIPE <EOL> if subprocess . mswindows : <EOL> from win32file import ReadFile , WriteFile <EOL> from win32pipe import PeekNamedPipe <EOL> import msvcrt <EOL> else : <EOL> import select <EOL> import fcntl <EOL> try : fcntl . F_GETFL <EOL> except AttributeError : fcntl . F_GETFL = <NUM_LIT:3> <EOL> try : fcntl . F_SETFL <EOL> except AttributeError : fcntl . F_SETFL = <NUM_LIT:4> <EOL> class Popen ( subprocess . Popen ) : <EOL> def recv ( self , maxsize = None ) : <EOL> return self . _recv ( '<STR_LIT>' , maxsize ) <EOL> def recv_err ( self , maxsize = None ) : <EOL> return self . _recv ( '<STR_LIT>' , maxsize ) <EOL> def send_recv ( self , input = '<STR_LIT>' , maxsize = None ) : <EOL> return self . send ( input ) , self . recv ( maxsize ) , self . recv_err ( maxsize ) <EOL> def get_conn_maxsize ( self , which , maxsize ) : <EOL> if maxsize is None : <EOL> maxsize = <NUM_LIT> <EOL> elif maxsize < <NUM_LIT:1> : <EOL> maxsize = <NUM_LIT:1> <EOL> return getattr ( self , which ) , maxsize <EOL> def _close ( self , which ) : <EOL> getattr ( self , which ) . close ( ) <EOL> setattr ( self , which , None ) <EOL> if subprocess . mswindows : <EOL> def send ( self , input ) : <EOL> if not self . stdin : <EOL> return None <EOL> try : <EOL> x = msvcrt . get_osfhandle ( self . stdin . fileno ( ) ) <EOL> ( errCode , written ) = WriteFile ( x , input ) <EOL> except ValueError : <EOL> return self . _close ( '<STR_LIT>' ) <EOL> except ( subprocess . pywintypes . error , Exception ) , why : <EOL> if why [ <NUM_LIT:0> ] in ( <NUM_LIT> , errno . ESHUTDOWN ) : <EOL> return self . _close ( '<STR_LIT>' ) <EOL> raise <EOL> return written <EOL> def _recv ( self , which , maxsize ) : <EOL> conn , maxsize = self . get_conn_maxsize ( which , maxsize ) <EOL> if conn is None : <EOL> return None <EOL> try : <EOL> x = msvcrt . get_osfhandle ( conn . fileno ( ) ) <EOL> ( read , nAvail , nMessage ) = PeekNamedPipe ( x , <NUM_LIT:0> ) <EOL> if maxsize < nAvail : <EOL> nAvail = maxsize <EOL> if nAvail > <NUM_LIT:0> : <EOL> ( errCode , read ) = ReadFile ( x , nAvail , None ) <EOL> except ValueError : <EOL> return self . _close ( which ) <EOL> except ( subprocess . pywintypes . error , Exception ) , why : <EOL> if why [ <NUM_LIT:0> ] in ( <NUM_LIT> , errno . ESHUTDOWN ) : <EOL> return self . _close ( which ) <EOL> raise <EOL> return read <EOL> else : <EOL> def send ( self , input ) : <EOL> if not self . stdin : <EOL> return None <EOL> if not select . select ( [ ] , [ self . stdin ] , [ ] , <NUM_LIT:0> ) [ <NUM_LIT:1> ] : <EOL> return <NUM_LIT:0> <EOL> try : <EOL> written = os . write ( self . stdin . fileno ( ) , input ) <EOL> except OSError , why : <EOL> if why [ <NUM_LIT:0> ] == errno . EPIPE : <EOL> return self . _close ( '<STR_LIT>' ) <EOL> raise <EOL> return written <EOL> def _recv ( self , which , maxsize ) : <EOL> conn , maxsize = self . get_conn_maxsize ( which , maxsize ) <EOL> if conn is None : <EOL> return None <EOL> try : <EOL> flags = fcntl . fcntl ( conn , fcntl . F_GETFL ) <EOL> except TypeError : <EOL> flags = None <EOL> else : <EOL> if not conn . closed : <EOL> fcntl . fcntl ( conn , fcntl . F_SETFL , flags | os . O_NONBLOCK ) <EOL> try : <EOL> if not select . select ( [ conn ] , [ ] , [ ] , <NUM_LIT:0> ) [ <NUM_LIT:0> ] : <EOL> return '<STR_LIT>' <EOL> r = conn . read ( maxsize ) <EOL> if not r : <EOL> return self . _close ( which ) <EOL> return r <EOL> finally : <EOL> if not conn . closed and not flags is None : <EOL> fcntl . fcntl ( conn , fcntl . F_SETFL , flags ) <EOL> disconnect_message = "<STR_LIT>" <EOL> def recv_some ( p , t = <NUM_LIT> , e = <NUM_LIT:1> , tr = <NUM_LIT:5> , stderr = <NUM_LIT:0> ) : <EOL> if tr < <NUM_LIT:1> : <EOL> tr = <NUM_LIT:1> <EOL> x = time . time ( ) + t <EOL> y = [ ] <EOL> r = '<STR_LIT>' <EOL> pr = p . recv <EOL> if stderr : <EOL> pr = p . recv_err <EOL> while time . time ( ) < x or r : <EOL> r = pr ( ) <EOL> if r is None : <EOL> if e : <EOL> raise Exception ( disconnect_message ) <EOL> else : <EOL> break <EOL> elif r : <EOL> y . append ( r ) <EOL> else : <EOL> time . sleep ( max ( ( x - time . time ( ) ) / tr , <NUM_LIT:0> ) ) <EOL> return '<STR_LIT>' . join ( y ) <EOL> def send_all ( p , data ) : <EOL> while len ( data ) : <EOL> sent = p . send ( data ) <EOL> if sent is None : <EOL> raise Exception ( disconnect_message ) <EOL> data = buffer ( data , sent ) <EOL> try : <EOL> object <EOL> except NameError : <EOL> class object : <EOL> pass <EOL> class TestCmd ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , description = None , <EOL> program = None , <EOL> interpreter = None , <EOL> workdir = None , <EOL> subdir = None , <EOL> verbose = None , <EOL> match = None , <EOL> diff = None , <EOL> combine = <NUM_LIT:0> , <EOL> universal_newlines = <NUM_LIT:1> ) : <EOL> self . _cwd = os . getcwd ( ) <EOL> self . description_set ( description ) <EOL> self . program_set ( program ) <EOL> self . interpreter_set ( interpreter ) <EOL> if verbose is None : <EOL> try : <EOL> verbose = max ( <NUM_LIT:0> , int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) <EOL> except ValueError : <EOL> verbose = <NUM_LIT:0> <EOL> self . verbose_set ( verbose ) <EOL> self . combine = combine <EOL> self . universal_newlines = universal_newlines <EOL> if match is not None : <EOL> self . match_function = match <EOL> else : <EOL> self . match_function = match_re <EOL> if diff is not None : <EOL> self . diff_function = diff <EOL> else : <EOL> try : <EOL> difflib <EOL> except NameError : <EOL> pass <EOL> else : <EOL> self . diff_function = simple_diff <EOL> self . _dirlist = [ ] <EOL> self . _preserve = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> if os . environ . has_key ( '<STR_LIT>' ) and not os . environ [ '<STR_LIT>' ] is '<STR_LIT>' : <EOL> self . _preserve [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> self . _preserve [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> self . _preserve [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> else : <EOL> try : <EOL> self . _preserve [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> self . _preserve [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> self . _preserve [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> self . _stdout = [ ] <EOL> self . _stderr = [ ] <EOL> self . status = None <EOL> self . condition = '<STR_LIT>' <EOL> self . workdir_set ( workdir ) <EOL> self . subdir ( subdir ) <EOL> def __del__ ( self ) : <EOL> self . cleanup ( ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % id ( self ) <EOL> banner_char = '<STR_LIT:=>' <EOL> banner_width = <NUM_LIT> <EOL> def banner ( self , s , width = None ) : <EOL> if width is None : <EOL> width = self . banner_width <EOL> return s + self . banner_char * ( width - len ( s ) ) <EOL> if os . name == '<STR_LIT>' : <EOL> def escape ( self , arg ) : <EOL> "<STR_LIT>" <EOL> slash = '<STR_LIT:\\>' <EOL> special = '<STR_LIT>' <EOL> arg = string . replace ( arg , slash , slash + slash ) <EOL> for c in special : <EOL> arg = string . replace ( arg , c , slash + c ) <EOL> if re_space . search ( arg ) : <EOL> arg = '<STR_LIT:">' + arg + '<STR_LIT:">' <EOL> return arg <EOL> else : <EOL> def escape ( self , arg ) : <EOL> if re_space . search ( arg ) : <EOL> arg = '<STR_LIT:">' + arg + '<STR_LIT:">' <EOL> return arg <EOL> def canonicalize ( self , path ) : <EOL> if is_List ( path ) : <EOL> path = apply ( os . path . join , tuple ( path ) ) <EOL> if not os . path . isabs ( path ) : <EOL> path = os . path . join ( self . workdir , path ) <EOL> return path <EOL> def chmod ( self , path , mode ) : <EOL> """<STR_LIT>""" <EOL> path = self . canonicalize ( path ) <EOL> os . chmod ( path , mode ) <EOL> def cleanup ( self , condition = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . _dirlist : <EOL> return <EOL> os . chdir ( self . _cwd ) <EOL> self . workdir = None <EOL> if condition is None : <EOL> condition = self . condition <EOL> if self . _preserve [ condition ] : <EOL> for dir in self . _dirlist : <EOL> print "<STR_LIT>" , dir <EOL> else : <EOL> list = self . _dirlist [ : ] <EOL> list . reverse ( ) <EOL> for dir in list : <EOL> self . writable ( dir , <NUM_LIT:1> ) <EOL> shutil . rmtree ( dir , ignore_errors = <NUM_LIT:1> ) <EOL> self . _dirlist = [ ] <EOL> try : <EOL> global _Cleanup <EOL> _Cleanup . remove ( self ) <EOL> except ( AttributeError , ValueError ) : <EOL> pass <EOL> def command_args ( self , program = None , <EOL> interpreter = None , <EOL> arguments = None ) : <EOL> if program : <EOL> if type ( program ) == type ( '<STR_LIT>' ) and not os . path . isabs ( program ) : <EOL> program = os . path . join ( self . _cwd , program ) <EOL> else : <EOL> program = self . program <EOL> if not interpreter : <EOL> interpreter = self . interpreter <EOL> if not type ( program ) in [ type ( [ ] ) , type ( ( ) ) ] : <EOL> program = [ program ] <EOL> cmd = list ( program ) <EOL> if interpreter : <EOL> if not type ( interpreter ) in [ type ( [ ] ) , type ( ( ) ) ] : <EOL> interpreter = [ interpreter ] <EOL> cmd = list ( interpreter ) + cmd <EOL> if arguments : <EOL> if type ( arguments ) == type ( '<STR_LIT>' ) : <EOL> arguments = string . split ( arguments ) <EOL> cmd . extend ( arguments ) <EOL> return cmd <EOL> def description_set ( self , description ) : <EOL> """<STR_LIT>""" <EOL> self . description = description <EOL> try : <EOL> difflib <EOL> except NameError : <EOL> def diff ( self , a , b , name , * args , ** kw ) : <EOL> print self . banner ( '<STR_LIT>' % name ) <EOL> print a <EOL> print self . banner ( '<STR_LIT>' % name ) <EOL> print b <EOL> else : <EOL> def diff ( self , a , b , name , * args , ** kw ) : <EOL> print self . banner ( name ) <EOL> args = ( a . splitlines ( ) , b . splitlines ( ) ) + args <EOL> lines = apply ( self . diff_function , args , kw ) <EOL> for l in lines : <EOL> print l <EOL> def fail_test ( self , condition = <NUM_LIT:1> , function = None , skip = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return <EOL> self . condition = '<STR_LIT>' <EOL> fail_test ( self = self , <EOL> condition = condition , <EOL> function = function , <EOL> skip = skip ) <EOL> def interpreter_set ( self , interpreter ) : <EOL> """<STR_LIT>""" <EOL> self . interpreter = interpreter <EOL> def match ( self , lines , matches ) : <EOL> """<STR_LIT>""" <EOL> return self . match_function ( lines , matches ) <EOL> def match_exact ( self , lines , matches ) : <EOL> """<STR_LIT>""" <EOL> return match_exact ( lines , matches ) <EOL> def match_re ( self , lines , res ) : <EOL> """<STR_LIT>""" <EOL> return match_re ( lines , res ) <EOL> def match_re_dotall ( self , lines , res ) : <EOL> """<STR_LIT>""" <EOL> return match_re_dotall ( lines , res ) <EOL> def no_result ( self , condition = <NUM_LIT:1> , function = None , skip = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return <EOL> self . condition = '<STR_LIT>' <EOL> no_result ( self = self , <EOL> condition = condition , <EOL> function = function , <EOL> skip = skip ) <EOL> def pass_test ( self , condition = <NUM_LIT:1> , function = None ) : <EOL> """<STR_LIT>""" <EOL> if not condition : <EOL> return <EOL> self . condition = '<STR_LIT>' <EOL> pass_test ( self = self , condition = condition , function = function ) <EOL> def preserve ( self , * conditions ) : <EOL> """<STR_LIT>""" <EOL> if conditions is ( ) : <EOL> conditions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for cond in conditions : <EOL> self . _preserve [ cond ] = <NUM_LIT:1> <EOL> def program_set ( self , program ) : <EOL> """<STR_LIT>""" <EOL> if program and not os . path . isabs ( program ) : <EOL> program = os . path . join ( self . _cwd , program ) <EOL> self . program = program <EOL> def read ( self , file , mode = '<STR_LIT:rb>' ) : <EOL> """<STR_LIT>""" <EOL> file = self . canonicalize ( file ) <EOL> if mode [ <NUM_LIT:0> ] != '<STR_LIT:r>' : <EOL> raise ValueError , "<STR_LIT>" <EOL> with open ( file , mode ) as f : <EOL> result = f . read ( ) <EOL> return result <EOL> def rmdir ( self , dir ) : <EOL> """<STR_LIT>""" <EOL> dir = self . canonicalize ( dir ) <EOL> os . rmdir ( dir ) <EOL> def start ( self , program = None , <EOL> interpreter = None , <EOL> arguments = None , <EOL> universal_newlines = None , <EOL> ** kw ) : <EOL> """<STR_LIT>""" <EOL> cmd = self . command_args ( program , interpreter , arguments ) <EOL> cmd_string = string . join ( map ( self . escape , cmd ) , '<STR_LIT:U+0020>' ) <EOL> if self . verbose : <EOL> sys . stderr . write ( cmd_string + "<STR_LIT:\n>" ) <EOL> if universal_newlines is None : <EOL> universal_newlines = self . universal_newlines <EOL> stdin = kw . get ( '<STR_LIT>' , None ) <EOL> if stdin is not None : <EOL> stdin = subprocess . PIPE <EOL> combine = kw . get ( '<STR_LIT>' , self . combine ) <EOL> if combine : <EOL> stderr_value = subprocess . STDOUT <EOL> else : <EOL> stderr_value = subprocess . PIPE <EOL> return Popen ( cmd , <EOL> stdin = stdin , <EOL> stdout = subprocess . PIPE , <EOL> stderr = stderr_value , <EOL> universal_newlines = universal_newlines ) <EOL> def finish ( self , popen , ** kw ) : <EOL> """<STR_LIT>""" <EOL> popen . stdin . close ( ) <EOL> self . status = popen . wait ( ) <EOL> if not self . status : <EOL> self . status = <NUM_LIT:0> <EOL> self . _stdout . append ( popen . stdout . read ( ) ) <EOL> if popen . stderr : <EOL> stderr = popen . stderr . read ( ) <EOL> else : <EOL> stderr = '<STR_LIT>' <EOL> self . _stderr . append ( stderr ) <EOL> def run ( self , program = None , <EOL> interpreter = None , <EOL> arguments = None , <EOL> chdir = None , <EOL> stdin = None , <EOL> universal_newlines = None ) : <EOL> """<STR_LIT>""" <EOL> if chdir : <EOL> oldcwd = os . getcwd ( ) <EOL> if not os . path . isabs ( chdir ) : <EOL> chdir = os . path . join ( self . workpath ( chdir ) ) <EOL> if self . verbose : <EOL> sys . stderr . write ( "<STR_LIT>" + chdir + "<STR_LIT>" ) <EOL> os . chdir ( chdir ) <EOL> p = self . start ( program , <EOL> interpreter , <EOL> arguments , <EOL> universal_newlines , <EOL> stdin = stdin ) <EOL> if stdin : <EOL> if is_List ( stdin ) : <EOL> for line in stdin : <EOL> p . stdin . write ( line ) <EOL> else : <EOL> p . stdin . write ( stdin ) <EOL> p . stdin . close ( ) <EOL> out = p . stdout . read ( ) <EOL> if p . stderr is None : <EOL> err = '<STR_LIT>' <EOL> else : <EOL> err = p . stderr . read ( ) <EOL> try : <EOL> close_output = p . close_output <EOL> except AttributeError : <EOL> p . stdout . close ( ) <EOL> if not p . stderr is None : <EOL> p . stderr . close ( ) <EOL> else : <EOL> close_output ( ) <EOL> self . _stdout . append ( out ) <EOL> self . _stderr . append ( err ) <EOL> self . status = p . wait ( ) <EOL> if not self . status : <EOL> self . status = <NUM_LIT:0> <EOL> if chdir : <EOL> os . chdir ( oldcwd ) <EOL> if self . verbose >= <NUM_LIT:2> : <EOL> write = sys . stdout . write <EOL> write ( '<STR_LIT>' % self . status ) <EOL> out = self . stdout ( ) <EOL> if out or self . verbose >= <NUM_LIT:3> : <EOL> write ( '<STR_LIT>' % len ( out ) ) <EOL> write ( out ) <EOL> write ( '<STR_LIT>' ) <EOL> err = self . stderr ( ) <EOL> if err or self . verbose >= <NUM_LIT:3> : <EOL> write ( '<STR_LIT>' % len ( err ) ) <EOL> write ( err ) <EOL> write ( '<STR_LIT>' ) <EOL> def sleep ( self , seconds = default_sleep_seconds ) : <EOL> """<STR_LIT>""" <EOL> time . sleep ( seconds ) <EOL> def stderr ( self , run = None ) : <EOL> """<STR_LIT>""" <EOL> if not run : <EOL> run = len ( self . _stderr ) <EOL> elif run < <NUM_LIT:0> : <EOL> run = len ( self . _stderr ) + run <EOL> run = run - <NUM_LIT:1> <EOL> return self . _stderr [ run ] <EOL> def stdout ( self , run = None ) : <EOL> """<STR_LIT>""" <EOL> if not run : <EOL> run = len ( self . _stdout ) <EOL> elif run < <NUM_LIT:0> : <EOL> run = len ( self . _stdout ) + run <EOL> run = run - <NUM_LIT:1> <EOL> return self . _stdout [ run ] <EOL> def subdir ( self , * subdirs ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for sub in subdirs : <EOL> if sub is None : <EOL> continue <EOL> if is_List ( sub ) : <EOL> sub = apply ( os . path . join , tuple ( sub ) ) <EOL> new = os . path . join ( self . workdir , sub ) <EOL> try : <EOL> os . mkdir ( new ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> count = count + <NUM_LIT:1> <EOL> return count <EOL> def symlink ( self , target , link ) : <EOL> """<STR_LIT>""" <EOL> link = self . canonicalize ( link ) <EOL> os . symlink ( target , link ) <EOL> def tempdir ( self , path = None ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> try : <EOL> path = tempfile . mktemp ( prefix = tempfile . template ) <EOL> except TypeError : <EOL> path = tempfile . mktemp ( ) <EOL> os . mkdir ( path ) <EOL> cwd = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( path ) <EOL> path = os . getcwd ( ) <EOL> finally : <EOL> os . chdir ( cwd ) <EOL> drive , rest = os . path . splitdrive ( path ) <EOL> if drive : <EOL> path = string . upper ( drive ) + rest <EOL> self . _dirlist . append ( path ) <EOL> global _Cleanup <EOL> try : <EOL> _Cleanup . index ( self ) <EOL> except ValueError : <EOL> _Cleanup . append ( self ) <EOL> return path <EOL> def touch ( self , path , mtime = None ) : <EOL> """<STR_LIT>""" <EOL> path = self . canonicalize ( path ) <EOL> atime = os . path . getatime ( path ) <EOL> if mtime is None : <EOL> mtime = time . time ( ) <EOL> os . utime ( path , ( atime , mtime ) ) <EOL> def unlink ( self , file ) : <EOL> """<STR_LIT>""" <EOL> file = self . canonicalize ( file ) <EOL> os . unlink ( file ) <EOL> def verbose_set ( self , verbose ) : <EOL> """<STR_LIT>""" <EOL> self . verbose = verbose <EOL> def where_is ( self , file , path = None , pathext = None ) : <EOL> """<STR_LIT>""" <EOL> if is_List ( file ) : <EOL> file = apply ( os . path . join , tuple ( file ) ) <EOL> if not os . path . isabs ( file ) : <EOL> file = where_is ( file , path , pathext ) <EOL> return file <EOL> def workdir_set ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if ( path != None ) : <EOL> if path == '<STR_LIT>' : <EOL> path = None <EOL> path = self . tempdir ( path ) <EOL> self . workdir = path <EOL> def workpath ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> return apply ( os . path . join , ( self . workdir , ) + tuple ( args ) ) <EOL> def readable ( self , top , read = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> return <EOL> if read : <EOL> def do_chmod ( fname ) : <EOL> try : st = os . stat ( fname ) <EOL> except OSError : pass <EOL> else : os . chmod ( fname , stat . S_IMODE ( st [ stat . ST_MODE ] | stat . S_IREAD ) ) <EOL> else : <EOL> def do_chmod ( fname ) : <EOL> try : st = os . stat ( fname ) <EOL> except OSError : pass <EOL> else : os . chmod ( fname , stat . S_IMODE ( st [ stat . ST_MODE ] & ~ stat . S_IREAD ) ) <EOL> if os . path . isfile ( top ) : <EOL> do_chmod ( top ) <EOL> elif read : <EOL> do_chmod ( top ) <EOL> def chmod_entries ( arg , dirname , names , do_chmod = do_chmod ) : <EOL> for n in names : <EOL> do_chmod ( os . path . join ( dirname , n ) ) <EOL> os . path . walk ( top , chmod_entries , None ) <EOL> else : <EOL> col = Collector ( top ) <EOL> os . path . walk ( top , col , None ) <EOL> col . entries . reverse ( ) <EOL> for d in col . entries : do_chmod ( d ) <EOL> def writable ( self , top , write = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> if write : <EOL> def do_chmod ( fname ) : <EOL> try : os . chmod ( fname , stat . S_IWRITE ) <EOL> except OSError : pass <EOL> else : <EOL> def do_chmod ( fname ) : <EOL> try : os . chmod ( fname , stat . S_IREAD ) <EOL> except OSError : pass <EOL> else : <EOL> if write : <EOL> def do_chmod ( fname ) : <EOL> try : st = os . stat ( fname ) <EOL> except OSError : pass <EOL> else : os . chmod ( fname , stat . S_IMODE ( st [ stat . ST_MODE ] | <NUM_LIT:0> <NUM_LIT:200> ) ) <EOL> else : <EOL> def do_chmod ( fname ) : <EOL> try : st = os . stat ( fname ) <EOL> except OSError : pass <EOL> else : os . chmod ( fname , stat . S_IMODE ( st [ stat . ST_MODE ] & ~ <NUM_LIT:0> <NUM_LIT:200> ) ) <EOL> if os . path . isfile ( top ) : <EOL> do_chmod ( top ) <EOL> else : <EOL> col = Collector ( top ) <EOL> os . path . walk ( top , col , None ) <EOL> for d in col . entries : do_chmod ( d ) <EOL> def executable ( self , top , execute = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> return <EOL> if execute : <EOL> def do_chmod ( fname ) : <EOL> try : st = os . stat ( fname ) <EOL> except OSError : pass <EOL> else : os . chmod ( fname , stat . S_IMODE ( st [ stat . ST_MODE ] | stat . S_IEXEC ) ) <EOL> else : <EOL> def do_chmod ( fname ) : <EOL> try : st = os . stat ( fname ) <EOL> except OSError : pass <EOL> else : os . chmod ( fname , stat . S_IMODE ( st [ stat . ST_MODE ] & ~ stat . S_IEXEC ) ) <EOL> if os . path . isfile ( top ) : <EOL> do_chmod ( top ) <EOL> elif execute : <EOL> do_chmod ( top ) <EOL> def chmod_entries ( arg , dirname , names , do_chmod = do_chmod ) : <EOL> for n in names : <EOL> do_chmod ( os . path . join ( dirname , n ) ) <EOL> os . path . walk ( top , chmod_entries , None ) <EOL> else : <EOL> col = Collector ( top ) <EOL> os . path . walk ( top , col , None ) <EOL> col . entries . reverse ( ) <EOL> for d in col . entries : do_chmod ( d ) <EOL> def write ( self , file , content , mode = '<STR_LIT:wb>' ) : <EOL> """<STR_LIT>""" <EOL> file = self . canonicalize ( file ) <EOL> if mode [ <NUM_LIT:0> ] != '<STR_LIT:w>' : <EOL> raise ValueError , "<STR_LIT>" <EOL> with open ( file , mode ) as f : <EOL> f . write ( content ) </s>
