Fix Issue 389 (Clicking list entry will not open new tab/window).
[Melange.git] / app / main.py
blob2e66a633f5d8ef3b48dc617eeeb952e197db6440
1 #!/usr/bin/python2.5
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.
17 """Main Melange module with profiling support.
18 """
20 __authors__ = [
21 # alphabetical order by last name, please
22 '"Augie Fackler" <durin42@gmail.com>',
26 import logging
27 import os
28 import sys
30 from google.appengine.ext.webapp import util
33 # Remove the standard version of Django.
34 for k in [k for k in sys.modules if k.startswith('django')]:
35 del sys.modules[k]
37 # Force sys.path to have our own directory first, in case we want to import
38 # from it. This lets us replace the built-in Django
39 sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
41 sys.path.insert(0, os.path.abspath('django.zip'))
43 ultimate_sys_path = None
45 # Force Django to reload its settings.
46 from django.conf import settings
47 settings._target = None
49 # Must set this env var before importing any part of Django
50 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
52 import django.core.handlers.wsgi
53 import django.core.signals
54 import django.db
56 # Log errors.
57 def log_exception(*args, **kwds):
58 """Function used for logging exceptions.
59 """
60 logging.exception('Exception in request:')
62 # Log all exceptions detected by Django.
63 django.core.signals.got_request_exception.connect(log_exception)
65 # Unregister the rollback event handler.
66 django.core.signals.got_request_exception.disconnect(
67 django.db._rollback_on_exception)
70 def profile_main_as_html():
71 """Main program for profiling. Profiling data added as HTML to the page.
72 """
73 import cProfile
74 import pstats
75 import StringIO
77 prof = cProfile.Profile()
78 prof = prof.runctx('real_main()', globals(), locals())
79 stream = StringIO.StringIO()
80 stats = pstats.Stats(prof, stream=stream)
81 # stats.strip_dirs() # Don't; too many modules are named __init__.py.
83 # 'time', 'cumulative' or 'calls'
84 stats.sort_stats('time')
86 # Optional arg: how many to print
87 stats.print_stats()
88 # The rest is optional.
89 # stats.print_callees()
90 # stats.print_callers()
91 print '\n<hr>'
92 print '<h1>Profile data</h1>'
93 print '<pre>'
94 print stream.getvalue()[:1000000]
95 print '</pre>'
98 def profile_main_as_logs():
99 """Main program for profiling. Profiling data logged.
101 import cProfile
102 import pstats
103 import StringIO
105 prof = cProfile.Profile()
106 prof = prof.runctx("real_main()", globals(), locals())
107 stream = StringIO.StringIO()
108 stats = pstats.Stats(prof, stream=stream)
109 stats.sort_stats('time') # Or cumulative
110 stats.print_stats(80) # 80 = how many to print
111 # The rest is optional.
112 # stats.print_callees()
113 # stats.print_callers()
114 logging.info("Profile data:\n%s", stream.getvalue())
117 def real_main():
118 """Main program without profiling.
120 global ultimate_sys_path
121 if ultimate_sys_path is None:
122 ultimate_sys_path = list(sys.path)
123 else:
124 sys.path[:] = ultimate_sys_path
126 # Create a Django application for WSGI.
127 application = django.core.handlers.wsgi.WSGIHandler()
129 # Run the WSGI CGI handler with that application.
130 util.run_wsgi_app(application)
132 main = real_main
134 if __name__ == '__main__':
135 main()