3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # alphabetical order by last name, please
19 '"Augie Fackler" <durin42@gmail.com>',
27 from google
.appengine
.ext
.webapp
import util
30 # Remove the standard version of Django.
31 for k
in [k
for k
in sys
.modules
if k
.startswith('django')]:
34 # Force sys.path to have our own directory first, in case we want to import
35 # from it. This lets us replace the built-in Django
36 sys
.path
.insert(0, os
.path
.abspath(os
.path
.dirname(__file__
)))
38 sys
.path
.insert(0, os
.path
.abspath('django.zip'))
40 ultimate_sys_path
= None
42 # Force Django to reload its settings.
43 from django
.conf
import settings
44 settings
._target
= None
46 # Must set this env var before importing any part of Django
47 os
.environ
['DJANGO_SETTINGS_MODULE'] = 'settings'
49 import django
.core
.handlers
.wsgi
50 import django
.core
.signals
54 def log_exception(*args
, **kwds
):
55 logging
.exception('Exception in request:')
57 # Log all exceptions detected by Django.
58 django
.core
.signals
.got_request_exception
.connect(log_exception
)
60 # Unregister the rollback event handler.
61 django
.core
.signals
.got_request_exception
.disconnect(
62 django
.db
._rollback
_on
_exception
)
65 def profile_main_as_html():
66 """Main program for profiling. Profiling data added as HTML to the page.
72 prof
= cProfile
.Profile()
73 prof
= prof
.runctx('real_main()', globals(), locals())
74 stream
= StringIO
.StringIO()
75 stats
= pstats
.Stats(prof
, stream
=stream
)
76 # stats.strip_dirs() # Don't; too many modules are named __init__.py.
78 # 'time', 'cumulative' or 'calls'
79 stats
.sort_stats('time')
81 # Optional arg: how many to print
83 # The rest is optional.
84 # stats.print_callees()
85 # stats.print_callers()
87 print '<h1>Profile data</h1>'
89 print stream
.getvalue()[:1000000]
93 def profile_main_as_logs():
94 """Main program for profiling. Profiling data logged.
100 prof
= cProfile
.Profile()
101 prof
= prof
.runctx("real_main()", globals(), locals())
102 stream
= StringIO
.StringIO()
103 stats
= pstats
.Stats(prof
, stream
=stream
)
104 stats
.sort_stats('time') # Or cumulative
105 stats
.print_stats(80) # 80 = how many to print
106 # The rest is optional.
107 # stats.print_callees()
108 # stats.print_callers()
109 logging
.info("Profile data:\n%s", stream
.getvalue())
113 """Main program without profiling.
115 global ultimate_sys_path
116 if ultimate_sys_path
is None:
117 ultimate_sys_path
= list(sys
.path
)
119 sys
.path
[:] = ultimate_sys_path
121 # Create a Django application for WSGI.
122 application
= django
.core
.handlers
.wsgi
.WSGIHandler()
124 # Run the WSGI CGI handler with that application.
125 util
.run_wsgi_app(application
)
129 if __name__
== '__main__':