<s> import logging <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import sys <EOL> import tempfile <EOL> import warnings <EOL> from django import contrib <EOL> from django import VERSION <EOL> try : <EOL> from django . utils . _os import upath <EOL> except : <EOL> def upath ( path ) : <EOL> return path <EOL> from django . utils import six <EOL> CONTRIB_MODULE_PATH = '<STR_LIT>' <EOL> TEST_TEMPLATE_DIR = '<STR_LIT>' <EOL> DJANGO_VERSION_DIR = '<STR_LIT>' . format ( * VERSION [ : <NUM_LIT:2> ] ) <EOL> RUNTESTS_DIR = os . path . abspath ( os . path . dirname ( upath ( __file__ ) ) ) + DJANGO_VERSION_DIR <EOL> sys . path . append ( RUNTESTS_DIR ) <EOL> CONTRIB_DIR = os . path . dirname ( upath ( contrib . __file__ ) ) <EOL> TEMP_DIR = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> os . environ [ '<STR_LIT>' ] = TEMP_DIR <EOL> DJANGO_PYODBC_DIR = os . path . abspath ( os . path . join ( RUNTESTS_DIR , '<STR_LIT>' ) ) <EOL> if DJANGO_PYODBC_DIR not in sys . path : <EOL> sys . path . append ( DJANGO_PYODBC_DIR ) <EOL> ALWAYS_INSTALLED_APPS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def get_test_modules ( ) : <EOL> modules = [ ] <EOL> for modpath , dirpath in ( <EOL> ( None , RUNTESTS_DIR ) , <EOL> ( CONTRIB_MODULE_PATH , CONTRIB_DIR ) ) : <EOL> for f in os . listdir ( dirpath ) : <EOL> if ( '<STR_LIT:.>' in f or <EOL> f == '<STR_LIT>' or <EOL> f . startswith ( '<STR_LIT>' ) or <EOL> os . path . isfile ( f ) ) : <EOL> continue <EOL> modules . append ( ( modpath , f ) ) <EOL> return modules <EOL> def get_installed ( ) : <EOL> from django . db . models . loading import get_apps <EOL> return [ app . __name__ . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] for app in get_apps ( ) if not app . __name__ . startswith ( '<STR_LIT>' ) ] <EOL> def setup ( verbosity , test_labels ) : <EOL> from django . conf import settings <EOL> from django . db . models . loading import get_apps , load_app <EOL> from django . test . testcases import TransactionTestCase , TestCase <EOL> def no_available_apps ( self ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> TransactionTestCase . available_apps = property ( no_available_apps ) <EOL> TestCase . available_apps = None <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 . LANGUAGE_CODE , <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 . LANGUAGE_CODE = '<STR_LIT>' <EOL> settings . SITE_ID = <NUM_LIT:1> <EOL> if verbosity > <NUM_LIT:0> : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> handler = logging . StreamHandler ( ) <EOL> logger . addHandler ( handler ) <EOL> with warnings . catch_warnings ( ) : <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , '<STR_LIT>' , PendingDeprecationWarning ) <EOL> get_apps ( ) <EOL> test_modules = get_test_modules ( ) <EOL> test_labels_set = set ( ) <EOL> for label in test_labels : <EOL> bits = label . split ( '<STR_LIT:.>' ) <EOL> if bits [ : <NUM_LIT:2> ] == [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> bits = bits [ : <NUM_LIT:3> ] <EOL> else : <EOL> bits = bits [ : <NUM_LIT:1> ] <EOL> test_labels_set . add ( '<STR_LIT:.>' . join ( bits ) ) <EOL> for modpath , module_name in test_modules : <EOL> if modpath : <EOL> module_label = '<STR_LIT:.>' . join ( [ modpath , module_name ] ) <EOL> else : <EOL> module_label = module_name <EOL> if not test_labels : <EOL> module_found_in_labels = True <EOL> else : <EOL> match = lambda label : ( <EOL> module_label == label or <EOL> module_label . startswith ( label + '<STR_LIT:.>' ) <EOL> ) <EOL> module_found_in_labels = any ( match ( l ) for l in test_labels_set ) <EOL> if module_found_in_labels : <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> try : <EOL> shutil . rmtree ( six . text_type ( TEMP_DIR ) ) <EOL> except OSError : <EOL> print ( '<STR_LIT>' % 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> 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 ( <EOL> verbosity = verbosity , <EOL> interactive = interactive , <EOL> failfast = failfast , <EOL> ) <EOL> failures = test_runner . run_tests ( <EOL> test_labels or get_installed ( ) , 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> test_labels = test_labels or get_installed ( ) <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 , upath ( __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 ( '<STR_LIT>' ) <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> test_labels = test_labels or get_installed ( ) <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 , upath ( __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> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<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 . selenium : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:1>' <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>
