app/main.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 07 Apr 2009 08:41:58 +0000
changeset 2112 9a2c9354468c
parent 2066 1855c783934f
child 2333 221482a54238
child 2335 366e64ecba91
permissions -rw-r--r--
Added 2 new access checks to deal with StudentProjects. In preparation for the view updates regarding StudentProject. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
2066
1855c783934f Add missing docstrings to main.py and settings.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1015
diff changeset
    17
"""Main Melange module with profiling support.
1855c783934f Add missing docstrings to main.py and settings.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1015
diff changeset
    18
"""
1855c783934f Add missing docstrings to main.py and settings.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1015
diff changeset
    19
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  # alphabetical order by last name, please
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  '"Augie Fackler" <durin42@gmail.com>',
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
  ]
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
import logging
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
import os
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
import sys
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
from google.appengine.ext.webapp import util
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
# Remove the standard version of Django.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
for k in [k for k in sys.modules if k.startswith('django')]:
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
  del sys.modules[k]
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
# Force sys.path to have our own directory first, in case we want to import
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
# from it. This lets us replace the built-in Django
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
144
53d8b8064019 Release shell script and associated changes. Set svn:ignore property on /app/django.zip file and /release folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    41
sys.path.insert(0, os.path.abspath('django.zip'))
53d8b8064019 Release shell script and associated changes. Set svn:ignore property on /app/django.zip file and /release folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 54
diff changeset
    42
684
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
    43
ultimate_sys_path = None
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
    44
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
# Force Django to reload its settings.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
from django.conf import settings
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
settings._target = None
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
# Must set this env var before importing any part of Django
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
import django.core.handlers.wsgi
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
import django.core.signals
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
import django.db
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
# Log errors.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
def log_exception(*args, **kwds):
2066
1855c783934f Add missing docstrings to main.py and settings.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1015
diff changeset
    58
  """Function used for logging exceptions.
1855c783934f Add missing docstrings to main.py and settings.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1015
diff changeset
    59
  """
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
  logging.exception('Exception in request:')
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
324
05e21c089be6 Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 144
diff changeset
    62
# Log all exceptions detected by Django.
05e21c089be6 Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 144
diff changeset
    63
django.core.signals.got_request_exception.connect(log_exception)
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
# Unregister the rollback event handler.
324
05e21c089be6 Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 144
diff changeset
    66
django.core.signals.got_request_exception.disconnect(
05e21c089be6 Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 144
diff changeset
    67
    django.db._rollback_on_exception)
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
1015
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    70
def profile_main_as_html():
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    71
  """Main program for profiling. Profiling data added as HTML to the page.
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    72
  """
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    73
  import cProfile
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    74
  import pstats
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    75
  import StringIO
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    76
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    77
  prof = cProfile.Profile()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    78
  prof = prof.runctx('real_main()', globals(), locals())
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    79
  stream = StringIO.StringIO()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    80
  stats = pstats.Stats(prof, stream=stream)
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    81
  # stats.strip_dirs()  # Don't; too many modules are named __init__.py.
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    82
  
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    83
  # 'time', 'cumulative' or 'calls'
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    84
  stats.sort_stats('time')  
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    85
  
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    86
  # Optional arg: how many to print
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    87
  stats.print_stats() 
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    88
  # The rest is optional.
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    89
  # stats.print_callees()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    90
  # stats.print_callers()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    91
  print '\n<hr>'
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    92
  print '<h1>Profile data</h1>'
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    93
  print '<pre>'
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    94
  print stream.getvalue()[:1000000]
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    95
  print '</pre>'
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    96
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    97
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    98
def profile_main_as_logs():
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
    99
  """Main program for profiling. Profiling data logged.
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   100
  """
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   101
  import cProfile
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   102
  import pstats
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   103
  import StringIO
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   104
  
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   105
  prof = cProfile.Profile()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   106
  prof = prof.runctx("real_main()", globals(), locals())
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   107
  stream = StringIO.StringIO()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   108
  stats = pstats.Stats(prof, stream=stream)
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   109
  stats.sort_stats('time')  # Or cumulative
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   110
  stats.print_stats(80)  # 80 = how many to print
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   111
  # The rest is optional.
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   112
  # stats.print_callees()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   113
  # stats.print_callers()
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   114
  logging.info("Profile data:\n%s", stream.getvalue())
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   115
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   116
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   117
def real_main():
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   118
  """Main program without profiling.
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   119
  """
684
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
   120
  global ultimate_sys_path
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
   121
  if ultimate_sys_path is None:
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
   122
    ultimate_sys_path = list(sys.path)
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
   123
  else:
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
   124
    sys.path[:] = ultimate_sys_path
896672e44e03 Fix for randomely ocurring bug
Sverre Rabbelier <srabbelier@gmail.com>
parents: 324
diff changeset
   125
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
  # Create a Django application for WSGI.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
  application = django.core.handlers.wsgi.WSGIHandler()
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   128
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   129
  # Run the WSGI CGI handler with that application.
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
  util.run_wsgi_app(application)
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   131
1015
b9d51be5104a 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.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 684
diff changeset
   132
main = real_main
31
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   133
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   134
if __name__ == '__main__':
8b43c541afa7 First iteration of a "prototype" Melange app, along with an initial Person
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
  main()