<s> import tempfile <EOL> import sys <EOL> import subprocess <EOL> import shutil <EOL> import os <EOL> import hashlib <EOL> import contextlib <EOL> import gzip <EOL> import fnmatch <EOL> import tarfile <EOL> import zipfile <EOL> def generate_file_list ( directory ) : <EOL> for dirpath , dirnames , filenames in os . walk ( directory ) : <EOL> for filename in filenames : <EOL> yield os . path . join ( dirpath , filename ) <EOL> def sha1_file ( name , checksum = None ) : <EOL> CHUNKSIZE = <NUM_LIT> <EOL> if checksum is None : <EOL> checksum = hashlib . sha1 ( ) <EOL> if fnmatch . fnmatch ( name , "<STR_LIT>" ) : <EOL> opener = gzip . open <EOL> else : <EOL> opener = open <EOL> with contextlib . closing ( opener ( name , '<STR_LIT:rb>' ) ) as data : <EOL> chunk = data . read ( CHUNKSIZE ) <EOL> while len ( chunk ) == CHUNKSIZE : <EOL> checksum . update ( chunk ) <EOL> chunk = data . read ( CHUNKSIZE ) <EOL> else : <EOL> checksum . update ( chunk ) <EOL> return checksum <EOL> def calculate_result ( directory ) : <EOL> checksum = hashlib . sha1 ( ) <EOL> for filename in sorted ( generate_file_list ( directory ) ) : <EOL> if filename . endswith ( "<STR_LIT>" ) : <EOL> continue <EOL> sha1_file ( filename , checksum ) <EOL> return checksum . hexdigest ( ) <EOL> @ contextlib . contextmanager <EOL> def temporary_directory ( prefix = '<STR_LIT>' ) : <EOL> name = tempfile . mkdtemp ( prefix ) <EOL> try : <EOL> yield name <EOL> finally : <EOL> shutil . rmtree ( name ) <EOL> @ contextlib . contextmanager <EOL> def directory_clone ( src ) : <EOL> with temporary_directory ( '<STR_LIT>' ) as name : <EOL> subdir = os . path . join ( name , "<STR_LIT>" ) <EOL> shutil . copytree ( src , subdir ) <EOL> yield subdir <EOL> def launch_subprocess ( directory , arguments , env = None ) : <EOL> if env is None : <EOL> env = { } <EOL> newenv = { } <EOL> newenv . update ( os . environ ) <EOL> newenv . update ( env ) <EOL> proc = subprocess . Popen ( ( [ "<STR_LIT>" ] if sys . platform == "<STR_LIT:win32>" else [ ] ) + [ <EOL> "<STR_LIT>" , <EOL> directory ] + arguments , stdin = subprocess . PIPE , stdout = subprocess . PIPE , env = newenv ) <EOL> return proc <EOL> class RegressionError ( Exception ) : <EOL> pass <EOL> def do_test ( test_data , result_check , arguments = ( ) ) : <EOL> """<STR_LIT>""" <EOL> result_check = result_check . lower ( ) <EOL> env = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> if '<STR_LIT>' in os . environ : <EOL> env [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> with directory_clone ( test_data ) as directory : <EOL> proc = launch_subprocess ( directory , arguments , env ) <EOL> proc . stdin . close ( ) <EOL> proc . wait ( ) <EOL> if proc . returncode : <EOL> raise RegressionError ( "<STR_LIT>" ) <EOL> checksum = calculate_result ( directory ) . lower ( ) <EOL> if checksum != result_check . lower ( ) : <EOL> raise RegressionError ( "<STR_LIT>" . format ( checksum , result_check ) ) <EOL> print "<STR_LIT>" . format ( result_check ) <EOL> def do_test_match_output ( test_data , result_check , arguments = ( ) ) : <EOL> result_check = result_check . lower ( ) <EOL> env = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> with directory_clone ( test_data ) as directory : <EOL> proc = launch_subprocess ( directory , arguments , env ) <EOL> proc . stdin . close ( ) <EOL> output = proc . stdout . read ( ) <EOL> proc . wait ( ) <EOL> if proc . returncode : <EOL> raise RegressionError ( "<STR_LIT>" ) <EOL> print "<STR_LIT>" . format ( output ) <EOL> checksum = hashlib . sha1 ( ) <EOL> checksum . update ( output ) <EOL> checksum = checksum . hexdigest ( ) <EOL> if checksum != result_check . lower ( ) : <EOL> raise RegressionError ( "<STR_LIT>" . format ( checksum , result_check ) ) <EOL> print "<STR_LIT>" . format ( result_check ) <EOL> alpha_tests = [ <EOL> ( do_test , '<STR_LIT>' , '<STR_LIT>' , [ ] ) , <EOL> ( do_test , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> ( do_test_match_output , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> ( do_test , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> ( do_test , '<STR_LIT:replace>' , '<STR_LIT>' , [ '<STR_LIT:replace>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( do_test , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ( do_test , '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> ] <EOL> import optparse <EOL> parser = optparse . OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> def main ( argv ) : <EOL> options , args = parser . parse_args ( argv ) <EOL> if len ( args ) <= <NUM_LIT:1> : <EOL> do_these_regressions = [ '<STR_LIT:*>' ] <EOL> else : <EOL> do_these_regressions = args [ <NUM_LIT:1> : ] <EOL> with directory_clone ( "<STR_LIT>" ) as directory : <EOL> test_data = directory <EOL> passes = [ ] <EOL> fails = [ ] <EOL> for func , name , sha , args in alpha_tests : <EOL> print "<STR_LIT>" . format ( name , args ) <EOL> if any ( fnmatch . fnmatch ( name , x ) for x in do_these_regressions ) : <EOL> if options . profile : <EOL> print >> sys . stderr , "<STR_LIT>" % name <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' % name <EOL> try : <EOL> func ( test_data , sha , args ) <EOL> except RegressionError , e : <EOL> fails . append ( "<STR_LIT>" . format ( name , e ) ) <EOL> print fails [ - <NUM_LIT:1> ] <EOL> else : <EOL> passes . append ( "<STR_LIT>" . format ( name ) ) <EOL> print passes [ - <NUM_LIT:1> ] <EOL> print "<STR_LIT>" . format ( len ( passes ) ) <EOL> for line in fails : <EOL> print line <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( sys . argv ) ) </s>
