#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__authors__ = [
  # alphabetical order by last name, please
  '"Augie Fackler" <durin42@gmail.com>',
  ]


import logging
import os
import sys

from google.appengine.ext.webapp import util


# Remove the standard version of Django.
for k in [k for k in sys.modules if k.startswith('django')]:
  del sys.modules[k]

# Force sys.path to have our own directory first, in case we want to import
# from it. This lets us replace the built-in Django
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

sys.path.insert(0, os.path.abspath('django.zip'))

ultimate_sys_path = None

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
import django.core.signals
import django.db

# Log errors.
def log_exception(*args, **kwds):
  logging.exception('Exception in request:')

# Log all exceptions detected by Django.
django.core.signals.got_request_exception.connect(log_exception)

# Unregister the rollback event handler.
django.core.signals.got_request_exception.disconnect(
    django.db._rollback_on_exception)


def main():
  global ultimate_sys_path
  if ultimate_sys_path is None:
    ultimate_sys_path = list(sys.path)
  else:
    sys.path[:] = ultimate_sys_path

  # Create a Django application for WSGI.
  application = django.core.handlers.wsgi.WSGIHandler()

  # Run the WSGI CGI handler with that application.
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()

@@ -1813,15 +1813,1320 @@
 def 
-main():
+profile_main_as_html():%0A  %22%22%22Main program for profiling. Profiling data added as HTML to the page.%0A  %22%22%22%0A  import cProfile%0A  import pstats%0A  import StringIO%0A%0A  prof = cProfile.Profile()%0A  prof = prof.runctx('real_main()', globals(), locals())%0A  stream = StringIO.StringIO()%0A  stats = pstats.Stats(prof, stream=stream)%0A  # stats.strip_dirs()  # Don't; too many modules are named __init__.py.%0A  %0A  # 'time', 'cumulative' or 'calls'%0A  stats.sort_stats('time')  %0A  %0A  # Optional arg: how many to print%0A  stats.print_stats() %0A  # The rest is optional.%0A  # stats.print_callees()%0A  # stats.print_callers()%0A  print '%5Cn%3Chr%3E'%0A  print '%3Ch1%3EProfile data%3C/h1%3E'%0A  print '%3Cpre%3E'%0A  print stream.getvalue()%5B:1000000%5D%0A  print '%3C/pre%3E'%0A%0A%0Adef profile_main_as_logs():%0A  %22%22%22Main program for profiling. Profiling data logged.%0A  %22%22%22%0A  import cProfile%0A  import pstats%0A  import StringIO%0A  %0A  prof = cProfile.Profile()%0A  prof = prof.runctx(%22real_main()%22, globals(), locals())%0A  stream = StringIO.StringIO()%0A  stats = pstats.Stats(prof, stream=stream)%0A  stats.sort_stats('time')  # Or cumulative%0A  stats.print_stats(80)  # 80 = how many to print%0A  # The rest is optional.%0A  # stats.print_callees()%0A  # stats.print_callers()%0A  logging.info(%22Profile data:%5Cn%25s%22, stream.getvalue())%0A%0A%0Adef real_main():%0A  %22%22%22Main program without profiling.%0A  %22%22%22
 %0A  g
@@ -3450,16 +3450,33 @@
 ation)%0A%0A
+main = real_main%0A
 %0Aif __na

Add profiling support to Melange. By assigning profile_main_as_logs or profile_main_as_html to main variable you can turn on profiling. profile_main_as_logs will log profile data to App Engine console logs, profile_main_as_html will show profile data as html at the bottom of the page. If you want to profile app on deployed app just set the profiling function and deploy it.