<s> import os <EOL> import sys <EOL> from djangoappengine . main import application <EOL> from google . appengine . ext . webapp . util import run_wsgi_app <EOL> from djangoappengine . boot import setup_logging , env_ext <EOL> from django . conf import settings <EOL> path_backup = None <EOL> def real_main ( ) : <EOL> global path_backup <EOL> try : <EOL> sys . path = path_backup [ : ] <EOL> except : <EOL> path_backup = sys . path [ : ] <EOL> os . environ . update ( env_ext ) <EOL> setup_logging ( ) <EOL> run_wsgi_app ( application ) <EOL> def profile_main ( func ) : <EOL> from cStringIO import StringIO <EOL> import cProfile <EOL> import logging <EOL> import pstats <EOL> import random <EOL> only_forced_profile = getattr ( settings , '<STR_LIT>' , False ) <EOL> profile_percentage = getattr ( settings , '<STR_LIT>' , None ) <EOL> if ( only_forced_profile and <EOL> '<STR_LIT>' not in os . environ . get ( '<STR_LIT>' ) ) or ( not only_forced_profile and profile_percentage and <EOL> float ( profile_percentage ) / <NUM_LIT> <= random . random ( ) ) : <EOL> return func ( ) <EOL> prof = cProfile . Profile ( ) <EOL> prof = prof . runctx ( '<STR_LIT>' , globals ( ) , locals ( ) ) <EOL> stream = StringIO ( ) <EOL> stats = pstats . Stats ( prof , stream = stream ) <EOL> sort_by = getattr ( settings , '<STR_LIT>' , '<STR_LIT:time>' ) <EOL> if not isinstance ( sort_by , ( list , tuple ) ) : <EOL> sort_by = ( sort_by , ) <EOL> stats . sort_stats ( * sort_by ) <EOL> restrictions = [ ] <EOL> profile_pattern = getattr ( settings , '<STR_LIT>' , None ) <EOL> if profile_pattern : <EOL> restrictions . append ( profile_pattern ) <EOL> max_results = getattr ( settings , '<STR_LIT>' , <NUM_LIT> ) <EOL> if max_results and max_results != '<STR_LIT:all>' : <EOL> restrictions . append ( max_results ) <EOL> stats . print_stats ( * restrictions ) <EOL> extra_output = getattr ( settings , '<STR_LIT>' , None ) or ( ) <EOL> if not isinstance ( sort_by , ( list , tuple ) ) : <EOL> extra_output = ( extra_output , ) <EOL> if '<STR_LIT>' in extra_output : <EOL> stats . print_callees ( ) <EOL> if '<STR_LIT>' in extra_output : <EOL> stats . print_callers ( ) <EOL> logging . info ( '<STR_LIT>' , stream . getvalue ( ) ) <EOL> def make_profileable ( func ) : <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> return lambda : profile_main ( func ) <EOL> return func <EOL> main = make_profileable ( real_main ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
