<s> from __future__ import print_function <EOL> import multiprocessing <EOL> import pkgutil <EOL> import pyclbr <EOL> import subprocess <EOL> import os <EOL> import sys <EOL> import warnings <EOL> from docopt import docopt <EOL> import dj_database_url <EOL> from django import VERSION <EOL> from django . utils import autoreload <EOL> from django . conf import global_settings <EOL> from django . conf import settings <EOL> from djeasytests . tmpdir import temp_dir <EOL> class GlobalSettingsWrapper : <EOL> def __init__ ( self , settings , default_settings ) : <EOL> self . settings = settings <EOL> self . default_settings = default_settings <EOL> def __getattr__ ( self , setting ) : <EOL> try : <EOL> return getattr ( self . settings , setting ) <EOL> except AttributeError : <EOL> return getattr ( self . default_settings , setting ) <EOL> def _split ( itr , num ) : <EOL> split = [ ] <EOL> size = int ( len ( itr ) / num ) <EOL> for index in range ( num ) : <EOL> split . append ( itr [ size * index : size * ( index + <NUM_LIT:1> ) ] ) <EOL> return split <EOL> def _get_test_labels ( test_modules ) : <EOL> test_labels = [ ] <EOL> for test_module in test_modules : <EOL> for module in [ name for _ , name , _ in pkgutil . iter_modules ( [ os . path . join ( test_module , "<STR_LIT>" ) ] ) ] : <EOL> clsmembers = pyclbr . readmodule ( "<STR_LIT>" % ( test_module , module ) ) <EOL> for clsname , cls in clsmembers . items ( ) : <EOL> for method , _ in cls . methods . items ( ) : <EOL> if method . startswith ( '<STR_LIT>' ) : <EOL> test_labels . append ( '<STR_LIT>' % ( test_module , clsname , method ) ) <EOL> return test_labels <EOL> def _test_run_worker ( test_labels , test_settings , failfast = False , test_runner = '<STR_LIT>' ) : <EOL> warnings . filterwarnings ( <EOL> '<STR_LIT:error>' , r"<STR_LIT>" , <EOL> RuntimeWarning , r'<STR_LIT>' ) <EOL> test_settings . TEST_RUNNER = test_runner <EOL> from django . test . utils import get_runner <EOL> TestRunner = get_runner ( test_settings ) <EOL> test_runner = TestRunner ( verbosity = <NUM_LIT:1> , interactive = False , failfast = failfast ) <EOL> failures = test_runner . run_tests ( test_labels ) <EOL> return failures <EOL> def _test_in_subprocess ( args ) : <EOL> test_label , script , migrate = args <EOL> return subprocess . call ( [ '<STR_LIT>' , script ] + ( migrate and [ '<STR_LIT>' ] or [ ] ) + [ '<STR_LIT:test>' , test_label ] ) <EOL> def _test_run_worker_settings ( tests ) : <EOL> test_labels , test_settings = tests <EOL> return _test_run_worker ( test_labels , test_settings ) <EOL> class TestSetup ( object ) : <EOL> __doc__ = '''<STR_LIT>''' <EOL> default_settings = None <EOL> def __init__ ( self , appname = '<STR_LIT>' , test_settings = { } , test_modules = None , fallback_settings = None , default_settings = global_settings , version = None ) : <EOL> self . version = version <EOL> self . test_modules = test_modules or [ appname ] <EOL> if fallback_settings : <EOL> if default_settings : <EOL> self . default_settings = GlobalSettingsWrapper ( fallback_settings , default_settings ) <EOL> else : <EOL> self . default_settings = fallback_settings <EOL> else : <EOL> self . default_settings = default_settings <EOL> self . new_settings = test_settings <EOL> self . appname = appname <EOL> def get_doc ( self ) : <EOL> return self . __doc__ % { '<STR_LIT:filename>' : self . filename } <EOL> def get_args ( self ) : <EOL> return docopt ( self . get_doc ( ) , version = self . version , options_first = True ) <EOL> def run ( self , thefile ) : <EOL> self . path = os . path . abspath ( thefile ) <EOL> self . dirname = os . path . dirname ( self . path ) <EOL> self . filename = os . path . basename ( self . path ) <EOL> testing = os . path . join ( self . dirname , '<STR_LIT>' ) <EOL> if not testing in sys . path : <EOL> sys . path . append ( testing ) <EOL> self . args = self . get_args ( ) <EOL> if self . args . get ( '<STR_LIT:test>' , False ) : <EOL> if self . args . get ( '<STR_LIT>' , False ) : <EOL> failures = self . isolated ( ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if failures : <EOL> for failure in failures : <EOL> print ( "<STR_LIT>" % failure ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> num_failures = len ( failures ) <EOL> elif self . args . get ( '<STR_LIT>' , False ) : <EOL> num_failures = self . timed ( ) <EOL> else : <EOL> num_failures = self . test ( ) <EOL> sys . exit ( num_failures ) <EOL> elif self . args . get ( '<STR_LIT>' , False ) : <EOL> self . server ( bind = self . args . get ( '<STR_LIT>' ) , port = int ( self . args . get ( '<STR_LIT>' ) ) ) <EOL> elif self . args . get ( '<STR_LIT>' , False ) : <EOL> self . shell ( ) <EOL> elif self . args . get ( '<STR_LIT>' , False ) : <EOL> self . compilemessages ( ) <EOL> elif self . args . get ( '<STR_LIT>' , False ) : <EOL> self . makemessages ( ) <EOL> elif self . args . get ( '<STR_LIT>' , False ) : <EOL> self . manage ( ) <EOL> def server ( self , bind = '<STR_LIT:127.0.0.1>' , port = <NUM_LIT> ) : <EOL> self . configure ( ) <EOL> if os . environ . get ( "<STR_LIT>" ) != "<STR_LIT:true>" : <EOL> from django . contrib . auth . models import User <EOL> if not User . objects . filter ( is_superuser = True ) . exists ( ) : <EOL> usr = User ( ) <EOL> usr . username = '<STR_LIT>' <EOL> usr . email = '<STR_LIT>' <EOL> usr . set_password ( '<STR_LIT>' ) <EOL> usr . is_superuser = True <EOL> usr . is_staff = True <EOL> usr . is_active = True <EOL> usr . save ( ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( '<STR_LIT>' ) <EOL> from django . contrib . staticfiles . management . commands import runserver <EOL> rs = runserver . Command ( ) <EOL> rs . stdout = sys . stdout <EOL> rs . stderr = sys . stderr <EOL> rs . use_ipv6 = False <EOL> rs . _raw_ipv6 = False <EOL> rs . addr = bind <EOL> rs . port = port <EOL> autoreload . main ( rs . inner_run , ( ) , { <EOL> '<STR_LIT>' : '<STR_LIT>' % ( bind , port ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } ) <EOL> def isolated ( self ) : <EOL> parallel = self . args . get ( '<STR_LIT>' , False ) <EOL> test_labels = self . args . get ( '<STR_LIT>' , '<STR_LIT>' ) or _get_test_labels ( self . test_modules ) <EOL> if parallel : <EOL> pool = multiprocessing . Pool ( ) <EOL> mapper = pool . map <EOL> else : <EOL> mapper = map <EOL> migrate = self . args . get ( '<STR_LIT>' , False ) <EOL> results = mapper ( _test_in_subprocess , ( ( test_label , self . path , migrate ) for test_label in test_labels ) ) <EOL> failures = [ test_label for test_label , return_code in zip ( test_labels , results ) if return_code != <NUM_LIT:0> ] <EOL> return failures <EOL> def timed ( self ) : <EOL> test_labels = self . args . get ( '<STR_LIT>' , '<STR_LIT>' ) or _get_test_labels ( self . test_modules ) <EOL> test_settings = self . configure ( ) <EOL> return _test_run_worker ( test_labels , test_settings , test_runner = '<STR_LIT>' ) <EOL> def test ( self ) : <EOL> parallel = self . args . get ( '<STR_LIT>' , False ) <EOL> failfast = self . args . get ( '<STR_LIT>' , False ) <EOL> test_labels = self . args . get ( '<STR_LIT>' , '<STR_LIT>' ) or _get_test_labels ( self . test_modules ) <EOL> test_settings = self . configure ( ) <EOL> if parallel : <EOL> worker_tests = _split ( test_labels , multiprocessing . cpu_count ( ) ) <EOL> pool = multiprocessing . Pool ( ) <EOL> failures = sum ( pool . map ( _test_run_worker_settings , ( [ test , test_settings ] for test in worker_tests ) ) ) <EOL> return failures <EOL> else : <EOL> return _test_run_worker ( test_labels , test_settings , failfast = failfast ) <EOL> def compilemessages ( self ) : <EOL> self . configure ( ) <EOL> from django . core . management import call_command <EOL> os . chdir ( self . appname ) <EOL> call_command ( '<STR_LIT>' , all = True ) <EOL> def makemessages ( self ) : <EOL> self . configure ( ) <EOL> from django . core . management import call_command <EOL> os . chdir ( self . appname ) <EOL> call_command ( '<STR_LIT>' , all = True ) <EOL> def shell ( self ) : <EOL> self . configure ( ) <EOL> from django . core . management import call_command <EOL> call_command ( '<STR_LIT>' ) <EOL> def manage ( self ) : <EOL> self . configure ( ) <EOL> from django . core . management import execute_from_command_line <EOL> execute_from_command_line ( [ self . filename ] + self . args . get ( '<STR_LIT>' , [ ] ) ) <EOL> def configure ( self , ** kwargs ) : <EOL> migrate = self . args . get ( '<STR_LIT>' , False ) <EOL> kwargs [ '<STR_LIT>' ] = migrate <EOL> if not '<STR_LIT>' in self . new_settings : <EOL> default_name = '<STR_LIT>' if self . args . get ( '<STR_LIT:test>' , False ) else '<STR_LIT>' <EOL> db_url = os . environ . get ( "<STR_LIT>" , "<STR_LIT>" % default_name ) <EOL> kwargs [ '<STR_LIT>' ] = { '<STR_LIT:default>' : dj_database_url . parse ( db_url ) } <EOL> tmp_dir_prefix = '<STR_LIT>' % self . appname <EOL> with temp_dir ( prefix = tmp_dir_prefix ) as STATIC_ROOT : <EOL> with temp_dir ( prefix = tmp_dir_prefix ) as MEDIA_ROOT : <EOL> if not '<STR_LIT>' in self . new_settings : <EOL> kwargs [ '<STR_LIT>' ] = MEDIA_ROOT <EOL> if not '<STR_LIT>' in self . new_settings : <EOL> kwargs [ '<STR_LIT>' ] = STATIC_ROOT <EOL> kwargs . update ( self . new_settings ) <EOL> settings . configure ( default_settings = self . default_settings , ** kwargs ) <EOL> return settings </s>
