<s> import os <EOL> import shutil <EOL> import subprocess <EOL> import sys <EOL> import tempfile <EOL> import warnings <EOL> from django import contrib <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , "<STR_LIT>" , <EOL> PendingDeprecationWarning , '<STR_LIT>' ) <EOL> CONTRIB_DIR_NAME = '<STR_LIT>' <EOL> MODEL_TESTS_DIR_NAME = '<STR_LIT>' <EOL> REGRESSION_TESTS_DIR_NAME = '<STR_LIT>' <EOL> TEST_TEMPLATE_DIR = '<STR_LIT>' <EOL> RUNTESTS_DIR = os . path . dirname ( __file__ ) <EOL> CONTRIB_DIR = os . path . dirname ( contrib . __file__ ) <EOL> MODEL_TEST_DIR = os . path . join ( RUNTESTS_DIR , MODEL_TESTS_DIR_NAME ) <EOL> REGRESSION_TEST_DIR = os . path . join ( RUNTESTS_DIR , REGRESSION_TESTS_DIR_NAME ) <EOL> TEMP_DIR = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = TEMP_DIR <EOL> REGRESSION_SUBDIRS_TO_SKIP = [ '<STR_LIT>' ] <EOL> ALWAYS_INSTALLED_APPS = [ <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> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def geodjango ( settings ) : <EOL> spatial_dbs = [ name for name , db_dict in settings . DATABASES . items ( ) <EOL> if db_dict [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) ] <EOL> return len ( spatial_dbs ) == len ( settings . DATABASES ) <EOL> def get_test_modules ( ) : <EOL> modules = [ ] <EOL> for loc , dirpath in ( <EOL> ( MODEL_TESTS_DIR_NAME , MODEL_TEST_DIR ) , <EOL> ( REGRESSION_TESTS_DIR_NAME , REGRESSION_TEST_DIR ) , <EOL> ( CONTRIB_DIR_NAME , CONTRIB_DIR ) ) : <EOL> for f in os . listdir ( dirpath ) : <EOL> if ( f . startswith ( '<STR_LIT>' ) or <EOL> f . startswith ( '<STR_LIT:.>' ) or <EOL> f . startswith ( '<STR_LIT>' ) or <EOL> os . path . basename ( f ) in REGRESSION_SUBDIRS_TO_SKIP ) : <EOL> continue <EOL> modules . append ( ( loc , f ) ) <EOL> return modules <EOL> def setup ( verbosity , test_labels ) : <EOL> from django . conf import settings <EOL> state = { <EOL> '<STR_LIT>' : settings . INSTALLED_APPS , <EOL> '<STR_LIT>' : getattr ( settings , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : settings . TEMPLATE_DIRS , <EOL> '<STR_LIT>' : settings . USE_I18N , <EOL> '<STR_LIT>' : settings . LOGIN_URL , <EOL> '<STR_LIT>' : settings . LANGUAGE_CODE , <EOL> '<STR_LIT>' : settings . MIDDLEWARE_CLASSES , <EOL> '<STR_LIT>' : settings . STATIC_URL , <EOL> '<STR_LIT>' : settings . STATIC_ROOT , <EOL> } <EOL> settings . INSTALLED_APPS = ALWAYS_INSTALLED_APPS <EOL> settings . ROOT_URLCONF = '<STR_LIT>' <EOL> settings . STATIC_URL = '<STR_LIT>' <EOL> settings . STATIC_ROOT = os . path . join ( TEMP_DIR , '<STR_LIT>' ) <EOL> settings . TEMPLATE_DIRS = ( os . path . join ( RUNTESTS_DIR , TEST_TEMPLATE_DIR ) , ) <EOL> settings . USE_I18N = True <EOL> settings . LANGUAGE_CODE = '<STR_LIT>' <EOL> settings . LOGIN_URL = '<STR_LIT>' <EOL> settings . MIDDLEWARE_CLASSES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> settings . SITE_ID = <NUM_LIT:1> <EOL> settings . MANAGERS = ( "<STR_LIT>" , ) <EOL> from django . db . models . loading import get_apps , load_app <EOL> get_apps ( ) <EOL> test_labels_set = set ( [ label . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] for label in test_labels ] ) <EOL> test_modules = get_test_modules ( ) <EOL> if geodjango ( settings ) : <EOL> from django . contrib . gis . tests import geo_apps <EOL> test_modules . extend ( geo_apps ( runtests = True ) ) <EOL> settings . INSTALLED_APPS . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for module_dir , module_name in test_modules : <EOL> module_label = '<STR_LIT:.>' . join ( [ module_dir , module_name ] ) <EOL> if not test_labels or module_name in test_labels_set : <EOL> if verbosity >= <NUM_LIT:2> : <EOL> print "<STR_LIT>" % module_name <EOL> mod = load_app ( module_label ) <EOL> if mod : <EOL> if module_label not in settings . INSTALLED_APPS : <EOL> settings . INSTALLED_APPS . append ( module_label ) <EOL> return state <EOL> def teardown ( state ) : <EOL> from django . conf import settings <EOL> shutil . rmtree ( unicode ( TEMP_DIR ) ) <EOL> for key , value in state . items ( ) : <EOL> setattr ( settings , key , value ) <EOL> def django_tests ( verbosity , interactive , failfast , test_labels ) : <EOL> from django . conf import settings <EOL> state = setup ( verbosity , test_labels ) <EOL> extra_tests = [ ] <EOL> if geodjango ( settings ) and ( not test_labels or '<STR_LIT>' in test_labels ) : <EOL> from django . contrib . gis . tests import geodjango_suite <EOL> extra_tests . append ( geodjango_suite ( apps = False ) ) <EOL> from django . test . utils import get_runner <EOL> if not hasattr ( settings , '<STR_LIT>' ) : <EOL> settings . TEST_RUNNER = '<STR_LIT>' <EOL> TestRunner = get_runner ( settings ) <EOL> test_runner = TestRunner ( verbosity = verbosity , interactive = interactive , <EOL> failfast = failfast ) <EOL> failures = test_runner . run_tests ( test_labels , extra_tests = extra_tests ) <EOL> teardown ( state ) <EOL> return failures <EOL> def bisect_tests ( bisection_label , options , test_labels ) : <EOL> state = setup ( int ( options . verbosity ) , test_labels ) <EOL> if not test_labels : <EOL> from django . db . models . loading import get_apps <EOL> test_labels = [ app . __name__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:2> ] for app in get_apps ( ) ] <EOL> print '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( test_labels ) <EOL> for label in [ bisection_label , '<STR_LIT>' ] : <EOL> try : <EOL> test_labels . remove ( label ) <EOL> except ValueError : <EOL> pass <EOL> subprocess_args = [ <EOL> sys . executable , __file__ , '<STR_LIT>' % options . settings ] <EOL> if options . failfast : <EOL> subprocess_args . append ( '<STR_LIT>' ) <EOL> if options . verbosity : <EOL> subprocess_args . append ( '<STR_LIT>' % options . verbosity ) <EOL> if not options . interactive : <EOL> subprocess_args . append ( '<STR_LIT>' ) <EOL> iteration = <NUM_LIT:1> <EOL> while len ( test_labels ) > <NUM_LIT:1> : <EOL> midpoint = len ( test_labels ) / <NUM_LIT:2> <EOL> test_labels_a = test_labels [ : midpoint ] + [ bisection_label ] <EOL> test_labels_b = test_labels [ midpoint : ] + [ bisection_label ] <EOL> print '<STR_LIT>' % iteration <EOL> print '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( test_labels_a ) <EOL> failures_a = subprocess . call ( subprocess_args + test_labels_a ) <EOL> print '<STR_LIT>' % iteration <EOL> print '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( test_labels_b ) <EOL> print <EOL> failures_b = subprocess . call ( subprocess_args + test_labels_b ) <EOL> if failures_a and not failures_b : <EOL> print "<STR_LIT>" <EOL> iteration = iteration + <NUM_LIT:1> <EOL> test_labels = test_labels_a [ : - <NUM_LIT:1> ] <EOL> elif failures_b and not failures_a : <EOL> print "<STR_LIT>" <EOL> iteration = iteration + <NUM_LIT:1> <EOL> test_labels = test_labels_b [ : - <NUM_LIT:1> ] <EOL> elif failures_a and failures_b : <EOL> print "<STR_LIT>" <EOL> break <EOL> else : <EOL> print "<STR_LIT>" <EOL> break <EOL> if len ( test_labels ) == <NUM_LIT:1> : <EOL> print "<STR_LIT>" , test_labels [ <NUM_LIT:0> ] <EOL> teardown ( state ) <EOL> def paired_tests ( paired_test , options , test_labels ) : <EOL> state = setup ( int ( options . verbosity ) , test_labels ) <EOL> if not test_labels : <EOL> print "<STR_LIT>" <EOL> from django . db . models . loading import get_apps <EOL> test_labels = [ app . __name__ . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:2> ] for app in get_apps ( ) ] <EOL> print '<STR_LIT>' <EOL> for label in [ paired_test , '<STR_LIT>' ] : <EOL> try : <EOL> test_labels . remove ( label ) <EOL> except ValueError : <EOL> pass <EOL> subprocess_args = [ <EOL> sys . executable , __file__ , '<STR_LIT>' % options . settings ] <EOL> if options . failfast : <EOL> subprocess_args . append ( '<STR_LIT>' ) <EOL> if options . verbosity : <EOL> subprocess_args . append ( '<STR_LIT>' % options . verbosity ) <EOL> if not options . interactive : <EOL> subprocess_args . append ( '<STR_LIT>' ) <EOL> for i , label in enumerate ( test_labels ) : <EOL> print '<STR_LIT>' % ( <EOL> i + <NUM_LIT:1> , len ( test_labels ) , label ) <EOL> failures = subprocess . call ( subprocess_args + [ label , paired_test ] ) <EOL> if failures : <EOL> print '<STR_LIT>' , label <EOL> return <EOL> print '<STR_LIT>' <EOL> teardown ( state ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from optparse import OptionParser <EOL> usage = "<STR_LIT>" <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = '<STR_LIT:1>' , <EOL> type = '<STR_LIT>' , choices = [ '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> options , args = parser . parse_args ( ) <EOL> if options . settings : <EOL> os . environ [ '<STR_LIT>' ] = options . settings <EOL> elif "<STR_LIT>" not in os . environ : <EOL> parser . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> options . settings = os . environ [ '<STR_LIT>' ] <EOL> if options . liveserver is not None : <EOL> os . environ [ '<STR_LIT>' ] = options . liveserver <EOL> if options . bisect : <EOL> bisect_tests ( options . bisect , options , args ) <EOL> elif options . pair : <EOL> paired_tests ( options . pair , options , args ) <EOL> else : <EOL> failures = django_tests ( int ( options . verbosity ) , options . interactive , <EOL> options . failfast , args ) <EOL> if failures : <EOL> sys . exit ( bool ( failures ) ) </s>
