<s> """<STR_LIT>""" <EOL> import logging , sys , os , signal , time , errno <EOL> import json <EOL> from socket import gethostname <EOL> import django . contrib . admin <EOL> from django . core . management . base import BaseCommand <EOL> from django . core . handlers . wsgi import WSGIHandler <EOL> from django . conf import settings <EOL> from wsgiserver import mediahandler <EOL> error_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> CPWSGI_HELP = r"""<STR_LIT>""" <EOL> CPWSGI_OPTIONS = { <EOL> '<STR_LIT:host>' : '<STR_LIT:127.0.0.1>' , <EOL> '<STR_LIT>' : '<STR_LIT:localhost>' , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> args = "<STR_LIT>" <EOL> def handle ( self , * args , ** options ) : <EOL> from django . conf import settings <EOL> from django . utils import translation <EOL> try : <EOL> translation . activate ( settings . LANGUAGE_CODE ) <EOL> except AttributeError : <EOL> pass <EOL> runwsgiserver ( args ) <EOL> def usage ( self , subcommand ) : <EOL> return CPWSGI_HELP <EOL> def change_uid_gid ( uid , gid = None ) : <EOL> """<STR_LIT>""" <EOL> if not os . geteuid ( ) == <NUM_LIT:0> : <EOL> return <EOL> ( uid , gid ) = get_uid_gid ( uid , gid ) <EOL> os . setgid ( gid ) <EOL> os . setuid ( uid ) <EOL> def get_uid_gid ( uid , gid = None ) : <EOL> """<STR_LIT>""" <EOL> import pwd , grp <EOL> uid , default_grp = pwd . getpwnam ( uid ) [ <NUM_LIT:2> : <NUM_LIT:4> ] <EOL> if gid is None : <EOL> gid = default_grp <EOL> else : <EOL> try : <EOL> gid = grp . getgrnam ( gid ) [ <NUM_LIT:2> ] <EOL> except KeyError : <EOL> gid = default_grp <EOL> return ( uid , gid ) <EOL> def poll_process ( pid ) : <EOL> """<STR_LIT>""" <EOL> for n in range ( <NUM_LIT:10> ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> try : <EOL> os . kill ( pid , <NUM_LIT:0> ) <EOL> except OSError , e : <EOL> if e [ <NUM_LIT:0> ] == errno . ESRCH : <EOL> return False <EOL> else : <EOL> raise Exception <EOL> return True <EOL> def stop_server ( pidfile ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( pidfile ) : <EOL> pid = int ( open ( pidfile ) . read ( ) ) <EOL> try : <EOL> os . kill ( pid , signal . SIGTERM ) <EOL> except OSError : <EOL> os . remove ( pidfile ) <EOL> return <EOL> if poll_process ( pid ) : <EOL> os . kill ( pid , signal . SIGKILL ) <EOL> if poll_process ( pid ) : <EOL> raise OSError , "<STR_LIT>" <EOL> os . remove ( pidfile ) <EOL> class LoggingWSGIHandler ( WSGIHandler ) : <EOL> def handle_uncaught_exception ( self , request , resolver , exc_info , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> error_logger . error ( '<STR_LIT>' , request . path , <EOL> exc_info = exc_info , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : request <EOL> } <EOL> ) <EOL> return super ( LoggingWSGIHandler , self ) . handle_uncaught_exception ( request , resolver , exc_info , * args , ** kwargs ) <EOL> def start_server_servestatic ( * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> options [ '<STR_LIT>' ] = '<STR_LIT:/>' + settings . MOUNT <EOL> splunkdj_port = settings . DJANGO_PORT <EOL> options [ '<STR_LIT:port>' ] = int ( options . get ( '<STR_LIT:port>' , splunkdj_port ) ) <EOL> print '<STR_LIT>' % json . dumps ( options , sort_keys = True , indent = <NUM_LIT:2> ) <EOL> if options [ '<STR_LIT>' ] and options [ '<STR_LIT>' ] and options [ '<STR_LIT>' ] : <EOL> change_uid_gid ( options [ '<STR_LIT>' ] , options [ '<STR_LIT>' ] ) <EOL> from wsgiserver . server import CherryPyWSGIServer , WSGIPathInfoDispatcher <EOL> app = LoggingWSGIHandler ( ) <EOL> if options [ '<STR_LIT>' ] : <EOL> import django . core . servers . basehttp <EOL> app = django . core . servers . basehttp . AdminMediaHandler ( app ) <EOL> path = { <EOL> options [ '<STR_LIT>' ] : app , <EOL> settings . STATIC_URL : mediahandler . MediaHandler ( settings . STATIC_ROOT ) , <EOL> } <EOL> dispatcher = WSGIPathInfoDispatcher ( path ) <EOL> server = CherryPyWSGIServer ( <EOL> ( options [ '<STR_LIT:host>' ] , int ( options [ '<STR_LIT:port>' ] ) ) , <EOL> dispatcher , <EOL> int ( options [ '<STR_LIT>' ] ) , <EOL> options [ '<STR_LIT>' ] <EOL> ) <EOL> if options [ '<STR_LIT>' ] and options [ '<STR_LIT>' ] : <EOL> server . ssl_certificate = options [ '<STR_LIT>' ] <EOL> server . ssl_private_key = options [ '<STR_LIT>' ] <EOL> try : <EOL> server . start ( ) <EOL> except KeyboardInterrupt : <EOL> server . stop ( ) <EOL> def runwsgiserver ( argset = [ ] , ** kwargs ) : <EOL> options = CPWSGI_OPTIONS . copy ( ) <EOL> options . update ( kwargs ) <EOL> for x in argset : <EOL> if "<STR_LIT:=>" in x : <EOL> k , v = x . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> else : <EOL> k , v = x , True <EOL> if v == '<STR_LIT:False>' or v == '<STR_LIT:false>' : <EOL> v = False <EOL> options [ k . lower ( ) ] = v <EOL> if "<STR_LIT>" in options : <EOL> print CPWSGI_HELP <EOL> return <EOL> if "<STR_LIT>" in options : <EOL> stop_server ( options [ '<STR_LIT>' ] ) <EOL> return True <EOL> if options [ '<STR_LIT>' ] : <EOL> if not options [ '<STR_LIT>' ] : <EOL> options [ '<STR_LIT>' ] = '<STR_LIT>' % options [ '<STR_LIT:port>' ] <EOL> stop_server ( options [ '<STR_LIT>' ] ) <EOL> from django . utils . daemonize import become_daemon <EOL> if options [ '<STR_LIT>' ] : <EOL> become_daemon ( our_home_dir = options [ '<STR_LIT>' ] ) <EOL> else : <EOL> become_daemon ( ) <EOL> fp = open ( options [ '<STR_LIT>' ] , '<STR_LIT:w>' ) <EOL> fp . write ( "<STR_LIT>" % os . getpid ( ) ) <EOL> fp . close ( ) <EOL> if options [ '<STR_LIT>' ] : <EOL> from django . utils import autoreload <EOL> if options [ '<STR_LIT>' ] : <EOL> autoreload . main ( start_server_servestatic , ( ) , options ) <EOL> else : <EOL> start_server_servestatic ( options ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> runwsgiserver ( sys . argv [ <NUM_LIT:1> : ] ) </s>
