Skip to content

Commit

Permalink
runprofileserver can now uses the staticfiles app (code copied from r…
Browse files Browse the repository at this point in the history
…unserver_plus)
  • Loading branch information
yassam committed Apr 16, 2012
1 parent 89875a7 commit 1680b68
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions django_extensions/management/commands/runprofileserver.py
Expand Up @@ -12,10 +12,17 @@
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
from datetime import datetime
from django.conf import settings
import os
import sys
import time

try:
from django.contrib.staticfiles.handlers import StaticFilesHandler
USE_STATICFILES = 'django.contrib.staticfiles' in settings.INSTALLED_APPS
except ImportError, e:
USE_STATICFILES = False

try:
any
except NameError:
Expand Down Expand Up @@ -118,6 +125,13 @@ class Command(BaseCommand):
make_option('--kcachegrind', action='store_true', dest='use_lsprof', default=False,
help='Create kcachegrind compatible lsprof files, this requires and automatically enables cProfile.'),
)
if USE_STATICFILES:
option_list += (
make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
make_option('--insecure', action="store_true", dest='insecure_serving', default=False,
help='Allows serving static files even if DEBUG is False.'),
)
help = "Starts a lightweight Web server with profiling enabled."
args = '[optional port number, or ipaddr:port]'

Expand All @@ -128,6 +142,7 @@ def handle(self, addrport='', *args, **options):
import django
from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
from django.core.handlers.wsgi import WSGIHandler

if args:
raise CommandError('Usage is runserver %s' % self.args)
if not addrport:
Expand All @@ -150,8 +165,6 @@ def handle(self, addrport='', *args, **options):
quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

def inner_run():
from django.conf import settings

import os
import time
import hotshot
Expand Down Expand Up @@ -228,7 +241,14 @@ def handler(environ, start_response):
else:
path = os.path.join(django.__path__[0], 'contrib/admin/media')
try:
handler = make_profiler_handler(AdminMediaHandler(WSGIHandler(), path))
handler = AdminMediaHandler(WSGIHandler(), path)
if USE_STATICFILES:
use_static_handler = options.get('use_static_handler', True)
insecure_serving = options.get('insecure_serving', False)
if (use_static_handler and
(settings.DEBUG or insecure_serving)):
handler = StaticFilesHandler(handler)
handler = make_profiler_handler(handler)
run(addr, int(port), handler)
except WSGIServerException, e:
# Use helpful error messages instead of ugly tracebacks.
Expand Down

0 comments on commit 1680b68

Please sign in to comment.