Merge with Pawel's head.
[Melange.git] / app / main.py
blob036eb26931084b0846f87d53750568924300bb79
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>',
23 '"Pawel Solyga" <pawel.solyga@gmail.com>',
27 import logging
29 from google.appengine.ext.webapp import util
31 # pylint: disable-msg=W0611
32 import gae_django
35 def profile_main_as_html():
36 """Main program for profiling. Profiling data added as HTML to the page.
37 """
38 import cProfile
39 import pstats
40 import StringIO
42 prof = cProfile.Profile()
43 prof = prof.runctx('real_main()', globals(), locals())
44 stream = StringIO.StringIO()
45 stats = pstats.Stats(prof, stream=stream)
46 # stats.strip_dirs() # Don't; too many modules are named __init__.py.
48 # 'time', 'cumulative' or 'calls'
49 stats.sort_stats('time')
51 # Optional arg: how many to print
52 stats.print_stats()
53 # The rest is optional.
54 # stats.print_callees()
55 # stats.print_callers()
56 print '\n<hr>'
57 print '<h1>Profile data</h1>'
58 print '<pre>'
59 print stream.getvalue()[:1000000]
60 print '</pre>'
63 def profile_main_as_logs():
64 """Main program for profiling. Profiling data logged.
65 """
66 import cProfile
67 import pstats
68 import StringIO
70 prof = cProfile.Profile()
71 prof = prof.runctx("real_main()", globals(), locals())
72 stream = StringIO.StringIO()
73 stats = pstats.Stats(prof, stream=stream)
74 stats.sort_stats('time') # Or cumulative
75 stats.print_stats(80) # 80 = how many to print
76 # The rest is optional.
77 # stats.print_callees()
78 # stats.print_callers()
79 logging.info("Profile data:\n%s", stream.getvalue())
82 def real_main():
83 """Main program without profiling.
84 """
85 import django.core.handlers.wsgi
87 # Create a Django application for WSGI.
88 application = django.core.handlers.wsgi.WSGIHandler()
90 from soc.modules import callback
91 from soc.modules import core
93 callback.registerCore(core.Core())
94 callback.getCore().registerModuleCallbacks()
96 # Run the WSGI CGI handler with that application.
97 util.run_wsgi_app(application)
99 main = real_main
101 if __name__ == '__main__':
102 main()