<s> import argparse <EOL> import django <EOL> import pep8 <EOL> import sys <EOL> import time <EOL> import traceback <EOL> import unittest <EOL> from coverage import coverage , misc <EOL> from distutils import log <EOL> from django . utils . six import StringIO <EOL> class LabelException ( Exception ) : <EOL> pass <EOL> class SetupTestSuite ( unittest . TestSuite ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . cov = coverage ( ) <EOL> self . cov . start ( ) <EOL> self . configure ( ) <EOL> self . packages = self . resolve_packages ( ) <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , const = True , default = False , ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , const = True , default = False , ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:label>' ) <EOL> self . options = vars ( parser . parse_args ( sys . argv [ <NUM_LIT:2> : ] ) ) <EOL> sys . argv = sys . argv [ : <NUM_LIT:2> ] <EOL> runner_options = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> if django . VERSION >= ( <NUM_LIT:1> , <NUM_LIT:8> ) : <EOL> from django . test . runner import DiscoverRunner <EOL> self . test_runner = DiscoverRunner ( ** runner_options ) <EOL> tests = self . test_runner . build_suite ( ) <EOL> else : <EOL> from django . test . simple import DjangoTestSuiteRunner <EOL> self . test_runner = DjangoTestSuiteRunner ( ** runner_options ) <EOL> tests = self . build_tests ( ) <EOL> super ( SetupTestSuite , self ) . __init__ ( tests = tests , * args , ** kwargs ) <EOL> if django . VERSION < ( <NUM_LIT:1> , <NUM_LIT:7> ) : <EOL> try : <EOL> from south . management . commands import patch_for_test_db_setup <EOL> patch_for_test_db_setup ( ) <EOL> except ImportError : <EOL> pass <EOL> self . test_runner . setup_test_environment ( ) <EOL> self . old_config = self . test_runner . setup_databases ( ) <EOL> def handle_label_exception ( self , exception ) : <EOL> """<STR_LIT>""" <EOL> markers = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> if any ( marker in exception . message for marker in markers ) : <EOL> log . info ( exception ) <EOL> raise LabelException ( exception ) <EOL> else : <EOL> raise exception <EOL> def build_tests ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . test . simple import build_suite , build_test <EOL> try : <EOL> from django . apps import apps <EOL> get_app = apps . get_app_config <EOL> except ImportError : <EOL> from django . db . models import get_app <EOL> tests = [ ] <EOL> packages = [ self . options [ '<STR_LIT:label>' ] , ] if self . options [ '<STR_LIT:label>' ] else self . packages <EOL> for package in packages : <EOL> try : <EOL> if not self . options [ '<STR_LIT>' ] : <EOL> if self . options [ '<STR_LIT:label>' ] : <EOL> try : <EOL> tests . append ( build_test ( package ) ) <EOL> except ( ImproperlyConfigured , ValueError ) as e : <EOL> self . handle_label_exception ( e ) <EOL> else : <EOL> app = get_app ( package ) <EOL> tests . append ( build_suite ( app ) ) <EOL> else : <EOL> exception = None <EOL> while True : <EOL> try : <EOL> if self . options [ '<STR_LIT:label>' ] : <EOL> try : <EOL> tests . append ( build_test ( package ) ) <EOL> except ( ImproperlyConfigured , ValueError ) as e : <EOL> self . handle_label_exception ( e ) <EOL> else : <EOL> app = get_app ( package ) <EOL> tests . append ( build_suite ( app ) ) <EOL> break <EOL> except LabelException : <EOL> raise <EOL> except Exception as e : <EOL> if exception != str ( e ) : <EOL> traceback . print_exc ( ) <EOL> exception = str ( e ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> except ImproperlyConfigured as e : <EOL> log . info ( "<STR_LIT>" % traceback . format_exc ( ) ) <EOL> except ImportError as e : <EOL> log . info ( "<STR_LIT>" % traceback . format_exc ( ) ) <EOL> return tests <EOL> def configure ( self ) : <EOL> """<STR_LIT>""" <EOL> import django <EOL> from django . conf import settings <EOL> try : <EOL> from django . utils . importlib import import_module <EOL> except ImportError : <EOL> from importlib import import_module <EOL> try : <EOL> test_settings = import_module ( '<STR_LIT>' ) <EOL> except ImportError as e : <EOL> log . info ( '<STR_LIT>' % e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> setting_attrs = { } <EOL> for attr in dir ( test_settings ) : <EOL> if '<STR_LIT>' not in attr : <EOL> setting_attrs [ attr ] = getattr ( test_settings , attr ) <EOL> if not settings . configured : <EOL> settings . configure ( ** setting_attrs ) <EOL> if hasattr ( django , '<STR_LIT>' ) : <EOL> django . setup ( ) <EOL> def coverage_report ( self ) : <EOL> """<STR_LIT>""" <EOL> verbose = '<STR_LIT>' not in sys . argv <EOL> self . cov . stop ( ) <EOL> if verbose : <EOL> log . info ( "<STR_LIT>" ) <EOL> try : <EOL> include = [ '<STR_LIT>' % package for package in self . packages ] <EOL> omit = [ '<STR_LIT>' ] <EOL> self . cov . report ( include = include , omit = omit ) <EOL> self . cov . save ( ) <EOL> self . cov . xml_report ( include = include , omit = omit ) <EOL> except misc . CoverageException as e : <EOL> log . info ( "<STR_LIT>" % e ) <EOL> def resolve_packages ( self ) : <EOL> """<STR_LIT>""" <EOL> f = sys . _getframe ( ) <EOL> while f : <EOL> if '<STR_LIT>' in f . f_locals : <EOL> locals_self = f . f_locals [ '<STR_LIT>' ] <EOL> py_modules = getattr ( locals_self , '<STR_LIT>' , None ) <EOL> packages = getattr ( locals_self , '<STR_LIT>' , None ) <EOL> top_packages = [ ] <EOL> if py_modules or packages : <EOL> if py_modules : <EOL> for module in py_modules : <EOL> if '<STR_LIT:.>' not in module : <EOL> top_packages . append ( module ) <EOL> if packages : <EOL> for package in packages : <EOL> if '<STR_LIT:.>' not in package : <EOL> top_packages . append ( package ) <EOL> return list ( set ( top_packages ) ) <EOL> f = f . f_back <EOL> def pep8_report ( self ) : <EOL> """<STR_LIT>""" <EOL> verbose = '<STR_LIT>' not in sys . argv <EOL> if verbose : <EOL> old_stdout = sys . stdout <EOL> sys . stdout = mystdout = StringIO ( ) <EOL> pep8_style = pep8 . StyleGuide ( ) <EOL> pep8_style . options . exclude . append ( '<STR_LIT>' ) <EOL> pep8_style . options . exclude . append ( '<STR_LIT>' ) <EOL> pep8_style . check_files ( self . packages ) <EOL> sys . stdout = old_stdout <EOL> result = mystdout . getvalue ( ) <EOL> output = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> output . write ( result ) <EOL> output . close ( ) <EOL> if result : <EOL> log . info ( "<STR_LIT>" ) <EOL> log . info ( result ) <EOL> def run ( self , result , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> result . failfast = self . options [ '<STR_LIT>' ] <EOL> result = super ( SetupTestSuite , self ) . run ( result , * args , ** kwargs ) <EOL> self . test_runner . teardown_databases ( self . old_config ) <EOL> self . test_runner . teardown_test_environment ( ) <EOL> self . coverage_report ( ) <EOL> self . pep8_report ( ) <EOL> return result </s>
