<s> import os <EOL> import re <EOL> import socket <EOL> import sys <EOL> import time <EOL> from optparse import make_option <EOL> from django . conf import settings <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django_extensions . management . utils import setup_logger , RedirectHandler , signalcommand <EOL> from django_extensions . management . technical_response import null_technical_500_response <EOL> try : <EOL> if '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> from django . contrib . staticfiles . handlers import StaticFilesHandler <EOL> USE_STATICFILES = True <EOL> elif '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> from staticfiles . handlers import StaticFilesHandler <EOL> USE_STATICFILES = True <EOL> else : <EOL> USE_STATICFILES = False <EOL> except ImportError : <EOL> USE_STATICFILES = False <EOL> naiveip_re = re . compile ( r"""<STR_LIT>""" , re . X ) <EOL> DEFAULT_PORT = "<STR_LIT>" <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> if USE_STATICFILES : <EOL> option_list += ( <EOL> make_option ( '<STR_LIT>' , action = "<STR_LIT>" , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = "<STR_LIT:store_true>" , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> help = "<STR_LIT>" <EOL> args = '<STR_LIT>' <EOL> requires_model_validation = False <EOL> @ signalcommand <EOL> def handle ( self , addrport = '<STR_LIT>' , * args , ** options ) : <EOL> import django <EOL> if hasattr ( self . stderr , '<STR_LIT>' ) : <EOL> self . stderr . ending = None <EOL> setup_logger ( logger , self . stderr , filename = options . get ( '<STR_LIT>' , None ) ) <EOL> logredirect = RedirectHandler ( __name__ ) <EOL> werklogger = logging . getLogger ( '<STR_LIT>' ) <EOL> werklogger . setLevel ( logging . INFO ) <EOL> werklogger . addHandler ( logredirect ) <EOL> werklogger . propagate = False <EOL> if options . get ( "<STR_LIT>" , False ) : <EOL> try : <EOL> from django . db . backends import utils <EOL> except ImportError : <EOL> from django . db . backends import util as utils <EOL> try : <EOL> import sqlparse <EOL> except ImportError : <EOL> sqlparse = None <EOL> class PrintQueryWrapper ( utils . CursorDebugWrapper ) : <EOL> def execute ( self , sql , params = ( ) ) : <EOL> starttime = time . time ( ) <EOL> try : <EOL> return self . cursor . execute ( sql , params ) <EOL> finally : <EOL> raw_sql = self . db . ops . last_executed_query ( self . cursor , sql , params ) <EOL> execution_time = time . time ( ) - starttime <EOL> therest = '<STR_LIT>' % ( execution_time , self . db . alias ) <EOL> if sqlparse : <EOL> logger . info ( sqlparse . format ( raw_sql , reindent = True ) + therest ) <EOL> else : <EOL> logger . info ( raw_sql + therest ) <EOL> utils . CursorDebugWrapper = PrintQueryWrapper <EOL> try : <EOL> from django . core . servers . basehttp import AdminMediaHandler <EOL> USE_ADMINMEDIAHANDLER = True <EOL> except ImportError : <EOL> USE_ADMINMEDIAHANDLER = False <EOL> try : <EOL> from django . core . servers . basehttp import get_internal_wsgi_application as WSGIHandler <EOL> except ImportError : <EOL> from django . core . handlers . wsgi import WSGIHandler <EOL> try : <EOL> from werkzeug import run_simple , DebuggedApplication <EOL> if settings . DEBUG : <EOL> try : <EOL> set_werkzeug_log_color ( ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> pass <EOL> except ImportError : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> from django . views import debug <EOL> debug . technical_500_response = null_technical_500_response <EOL> self . use_ipv6 = options . get ( '<STR_LIT>' ) <EOL> if self . use_ipv6 and not socket . has_ipv6 : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> self . _raw_ipv6 = False <EOL> if not addrport : <EOL> try : <EOL> addrport = settings . RUNSERVERPLUS_SERVER_ADDRESS_PORT <EOL> except AttributeError : <EOL> pass <EOL> if not addrport : <EOL> self . addr = '<STR_LIT>' <EOL> self . port = DEFAULT_PORT <EOL> else : <EOL> m = re . match ( naiveip_re , addrport ) <EOL> if m is None : <EOL> raise CommandError ( '<STR_LIT>' <EOL> '<STR_LIT>' % addrport ) <EOL> self . addr , _ipv4 , _ipv6 , _fqdn , self . port = m . groups ( ) <EOL> if not self . port . isdigit ( ) : <EOL> raise CommandError ( "<STR_LIT>" % <EOL> self . port ) <EOL> if self . addr : <EOL> if _ipv6 : <EOL> self . addr = self . addr [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> self . use_ipv6 = True <EOL> self . _raw_ipv6 = True <EOL> elif self . use_ipv6 and not _fqdn : <EOL> raise CommandError ( '<STR_LIT>' <EOL> % self . addr ) <EOL> if not self . addr : <EOL> self . addr = '<STR_LIT>' if self . use_ipv6 else '<STR_LIT:127.0.0.1>' <EOL> threaded = options . get ( '<STR_LIT>' , True ) <EOL> use_reloader = options . get ( '<STR_LIT>' , True ) <EOL> open_browser = options . get ( '<STR_LIT>' , False ) <EOL> cert_path = options . get ( "<STR_LIT>" ) <EOL> quit_command = ( sys . platform == '<STR_LIT:win32>' ) and '<STR_LIT>' or '<STR_LIT>' <EOL> bind_url = "<STR_LIT>" % ( <EOL> self . addr if not self . _raw_ipv6 else '<STR_LIT>' % self . addr , self . port ) <EOL> def inner_run ( ) : <EOL> print ( "<STR_LIT>" ) <EOL> self . validate ( display_num_errors = True ) <EOL> print ( "<STR_LIT>" % ( django . get_version ( ) , settings . SETTINGS_MODULE ) ) <EOL> print ( "<STR_LIT>" % ( bind_url , ) ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % quit_command ) <EOL> path = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not path : <EOL> admin_media_path = os . path . join ( django . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> if os . path . isdir ( admin_media_path ) : <EOL> path = admin_media_path <EOL> else : <EOL> path = os . path . join ( django . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> handler = WSGIHandler ( ) <EOL> if USE_ADMINMEDIAHANDLER : <EOL> handler = AdminMediaHandler ( handler , path ) <EOL> if USE_STATICFILES : <EOL> use_static_handler = options . get ( '<STR_LIT>' , True ) <EOL> insecure_serving = options . get ( '<STR_LIT>' , False ) <EOL> if use_static_handler and ( settings . DEBUG or insecure_serving ) : <EOL> handler = StaticFilesHandler ( handler ) <EOL> if open_browser : <EOL> import webbrowser <EOL> webbrowser . open ( bind_url ) <EOL> if cert_path : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import OpenSSL <EOL> except ImportError : <EOL> raise CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> dir_path , cert_file = os . path . split ( cert_path ) <EOL> if not dir_path : <EOL> dir_path = os . getcwd ( ) <EOL> root , ext = os . path . splitext ( cert_file ) <EOL> certfile = os . path . join ( dir_path , root + "<STR_LIT>" ) <EOL> keyfile = os . path . join ( dir_path , root + "<STR_LIT>" ) <EOL> try : <EOL> from werkzeug . serving import make_ssl_devcert <EOL> if os . path . exists ( certfile ) and os . path . exists ( keyfile ) : <EOL> ssl_context = ( certfile , keyfile ) <EOL> else : <EOL> ssl_context = make_ssl_devcert ( <EOL> os . path . join ( dir_path , root ) , host = '<STR_LIT:localhost>' ) <EOL> except ImportError : <EOL> print ( "<STR_LIT>" ) <EOL> ssl_context = "<STR_LIT>" <EOL> else : <EOL> ssl_context = None <EOL> run_simple ( <EOL> self . addr , <EOL> int ( self . port ) , <EOL> DebuggedApplication ( handler , True ) , <EOL> use_reloader = use_reloader , <EOL> use_debugger = True , <EOL> threaded = threaded , <EOL> ssl_context = ssl_context <EOL> ) <EOL> inner_run ( ) <EOL> def set_werkzeug_log_color ( ) : <EOL> """<STR_LIT>""" <EOL> from django . core . management . color import color_style <EOL> from werkzeug . serving import WSGIRequestHandler <EOL> from werkzeug . _internal import _log <EOL> _style = color_style ( ) <EOL> _orig_log = WSGIRequestHandler . log <EOL> def werk_log ( self , type , message , * args ) : <EOL> try : <EOL> msg = '<STR_LIT>' % ( <EOL> self . address_string ( ) , <EOL> self . log_date_time_string ( ) , <EOL> message % args , <EOL> ) <EOL> http_code = str ( args [ <NUM_LIT:1> ] ) <EOL> except : <EOL> return _orig_log ( type , message , * args ) <EOL> if http_code [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> msg = _style . HTTP_SUCCESS ( msg ) <EOL> elif http_code [ <NUM_LIT:0> ] == '<STR_LIT:1>' : <EOL> msg = _style . HTTP_INFO ( msg ) <EOL> elif http_code == '<STR_LIT>' : <EOL> msg = _style . HTTP_NOT_MODIFIED ( msg ) <EOL> elif http_code [ <NUM_LIT:0> ] == '<STR_LIT:3>' : <EOL> msg = _style . HTTP_REDIRECT ( msg ) <EOL> elif http_code == '<STR_LIT>' : <EOL> msg = _style . HTTP_NOT_FOUND ( msg ) <EOL> elif http_code [ <NUM_LIT:0> ] == '<STR_LIT:4>' : <EOL> msg = _style . HTTP_BAD_REQUEST ( msg ) <EOL> else : <EOL> msg = _style . HTTP_SERVER_ERROR ( msg ) <EOL> _log ( type , msg ) <EOL> WSGIRequestHandler . log = werk_log </s>
