<s> """<STR_LIT>""" <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from optparse import make_option <EOL> from datetime import datetime <EOL> from django . conf import settings <EOL> import os <EOL> import sys <EOL> import time <EOL> try : <EOL> from django . contrib . staticfiles . handlers import StaticFilesHandler <EOL> USE_STATICFILES = '<STR_LIT>' in settings . INSTALLED_APPS <EOL> except ImportError , e : <EOL> USE_STATICFILES = False <EOL> try : <EOL> any <EOL> except NameError : <EOL> def any ( iterable ) : <EOL> for element in iterable : <EOL> if element : <EOL> return True <EOL> return False <EOL> def label ( code ) : <EOL> if isinstance ( code , str ) : <EOL> return ( '<STR_LIT>' , <NUM_LIT:0> , code ) <EOL> else : <EOL> return '<STR_LIT>' % ( code . co_name , <EOL> code . co_filename , <EOL> code . co_firstlineno ) <EOL> class KCacheGrind ( object ) : <EOL> def __init__ ( self , profiler ) : <EOL> self . data = profiler . getstats ( ) <EOL> self . out_file = None <EOL> def output ( self , out_file ) : <EOL> self . out_file = out_file <EOL> print >> out_file , '<STR_LIT>' <EOL> self . _print_summary ( ) <EOL> for entry in self . data : <EOL> self . _entry ( entry ) <EOL> def _print_summary ( self ) : <EOL> max_cost = <NUM_LIT:0> <EOL> for entry in self . data : <EOL> totaltime = int ( entry . totaltime * <NUM_LIT:1000> ) <EOL> max_cost = max ( max_cost , totaltime ) <EOL> print >> self . out_file , '<STR_LIT>' % ( max_cost , ) <EOL> def _entry ( self , entry ) : <EOL> out_file = self . out_file <EOL> code = entry . code <EOL> if isinstance ( code , str ) : <EOL> print >> out_file , '<STR_LIT>' <EOL> else : <EOL> print >> out_file , '<STR_LIT>' % ( code . co_filename , ) <EOL> print >> out_file , '<STR_LIT>' % ( label ( code ) , ) <EOL> inlinetime = int ( entry . inlinetime * <NUM_LIT:1000> ) <EOL> if isinstance ( code , str ) : <EOL> print >> out_file , '<STR_LIT>' , inlinetime <EOL> else : <EOL> print >> out_file , '<STR_LIT>' % ( code . co_firstlineno , inlinetime ) <EOL> if entry . calls : <EOL> calls = entry . calls <EOL> else : <EOL> calls = [ ] <EOL> if isinstance ( code , str ) : <EOL> lineno = <NUM_LIT:0> <EOL> else : <EOL> lineno = code . co_firstlineno <EOL> for subentry in calls : <EOL> self . _subentry ( lineno , subentry ) <EOL> print >> out_file <EOL> def _subentry ( self , lineno , subentry ) : <EOL> out_file = self . out_file <EOL> code = subentry . code <EOL> print >> out_file , '<STR_LIT>' % ( label ( code ) , ) <EOL> if isinstance ( code , str ) : <EOL> print >> out_file , '<STR_LIT>' <EOL> print >> out_file , '<STR_LIT>' % ( subentry . callcount , ) <EOL> else : <EOL> print >> out_file , '<STR_LIT>' % ( code . co_filename , ) <EOL> print >> out_file , '<STR_LIT>' % ( <EOL> subentry . callcount , code . co_firstlineno ) <EOL> totaltime = int ( subentry . totaltime * <NUM_LIT:1000> ) <EOL> print >> out_file , '<STR_LIT>' % ( lineno , totaltime ) <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , default = True , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , default = '<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:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <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> 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> def handle ( self , addrport = '<STR_LIT>' , * args , ** options ) : <EOL> import django <EOL> from django . core . servers . basehttp import run , WSGIServerException <EOL> try : <EOL> from django . core . servers . basehttp import AdminMediaHandler <EOL> HAS_ADMINMEDIAHANDLER = True <EOL> except ImportError : <EOL> HAS_ADMINMEDIAHANDLER = False <EOL> from django . core . handlers . wsgi import WSGIHandler <EOL> if args : <EOL> raise CommandError ( '<STR_LIT>' % self . args ) <EOL> if not addrport : <EOL> addr = '<STR_LIT>' <EOL> port = '<STR_LIT>' <EOL> else : <EOL> try : <EOL> addr , port = addrport . split ( '<STR_LIT::>' ) <EOL> except ValueError : <EOL> addr , port = '<STR_LIT>' , addrport <EOL> if not addr : <EOL> addr = '<STR_LIT:127.0.0.1>' <EOL> if not port . isdigit ( ) : <EOL> raise CommandError ( "<STR_LIT>" % port ) <EOL> use_reloader = options . get ( '<STR_LIT>' , True ) <EOL> shutdown_message = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> no_media = options . get ( '<STR_LIT>' , False ) <EOL> quit_command = ( sys . platform == '<STR_LIT:win32>' ) and '<STR_LIT>' or '<STR_LIT>' <EOL> def inner_run ( ) : <EOL> import os <EOL> import time <EOL> import hotshot <EOL> USE_CPROFILE = options . get ( '<STR_LIT>' , False ) <EOL> USE_LSPROF = options . get ( '<STR_LIT>' , False ) <EOL> if USE_LSPROF : <EOL> USE_CPROFILE = True <EOL> if USE_CPROFILE : <EOL> try : <EOL> import cProfile <EOL> USE_CPROFILE = True <EOL> except ImportError : <EOL> print "<STR_LIT>" <EOL> USE_CPROFILE = False <EOL> if USE_LSPROF and not USE_CPROFILE : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> prof_path = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_exclude_paths ( ) : <EOL> exclude_paths = [ ] <EOL> media_url = getattr ( settings , '<STR_LIT>' , None ) <EOL> if media_url : <EOL> exclude_paths . append ( media_url ) <EOL> static_url = getattr ( settings , '<STR_LIT>' , None ) <EOL> if static_url : <EOL> exclude_paths . append ( static_url ) <EOL> admin_media_prefix = getattr ( settings , '<STR_LIT>' , None ) <EOL> if admin_media_prefix : <EOL> exclude_paths . append ( admin_media_prefix ) <EOL> return exclude_paths <EOL> def make_profiler_handler ( inner_handler ) : <EOL> def handler ( environ , start_response ) : <EOL> path_info = environ [ '<STR_LIT>' ] <EOL> if no_media and any ( path_info . startswith ( p ) for p in get_exclude_paths ( ) ) : <EOL> return inner_handler ( environ , start_response ) <EOL> path_name = path_info . strip ( "<STR_LIT:/>" ) . replace ( '<STR_LIT:/>' , '<STR_LIT:.>' ) or "<STR_LIT:root>" <EOL> profname = "<STR_LIT>" % ( path_name , time . time ( ) ) <EOL> profname = os . path . join ( prof_path , profname ) <EOL> if USE_CPROFILE : <EOL> prof = cProfile . Profile ( ) <EOL> else : <EOL> prof = hotshot . Profile ( profname ) <EOL> start = datetime . now ( ) <EOL> try : <EOL> return prof . runcall ( inner_handler , environ , start_response ) <EOL> finally : <EOL> elap = datetime . now ( ) - start <EOL> elapms = elap . seconds * <NUM_LIT> + elap . microseconds / <NUM_LIT> <EOL> if USE_LSPROF : <EOL> kg = KCacheGrind ( prof ) <EOL> kg . output ( file ( profname , '<STR_LIT:w>' ) ) <EOL> elif USE_CPROFILE : <EOL> prof . dump_stats ( profname ) <EOL> profname2 = "<STR_LIT>" % ( path_name , elapms , time . time ( ) ) <EOL> profname2 = os . path . join ( prof_path , profname2 ) <EOL> if not USE_CPROFILE : <EOL> prof . close ( ) <EOL> os . rename ( profname , profname2 ) <EOL> return handler <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>" % ( addr , port ) <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> try : <EOL> handler = WSGIHandler ( ) <EOL> if HAS_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 <EOL> ( settings . DEBUG or insecure_serving ) ) : <EOL> handler = StaticFilesHandler ( handler ) <EOL> handler = make_profiler_handler ( handler ) <EOL> run ( addr , int ( port ) , handler ) <EOL> except WSGIServerException , e : <EOL> ERRORS = { <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> } <EOL> try : <EOL> error_text = ERRORS [ e . args [ <NUM_LIT:0> ] . args [ <NUM_LIT:0> ] ] <EOL> except ( AttributeError , KeyError ) : <EOL> error_text = str ( e ) <EOL> sys . stderr . write ( self . style . ERROR ( "<STR_LIT>" % error_text ) + '<STR_LIT:\n>' ) <EOL> os . _exit ( <NUM_LIT:1> ) <EOL> except KeyboardInterrupt : <EOL> if shutdown_message : <EOL> print shutdown_message <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if use_reloader : <EOL> from django . utils import autoreload <EOL> autoreload . main ( inner_run ) <EOL> else : <EOL> inner_run ( ) </s>
