Ajouté les instructions pour patcher les permissions des eggs
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / middleware.py
1 import sys
2 import tempfile
3 import hotshot
4 import hotshot.stats
5 from django.conf import settings
6 from cStringIO import StringIO
7
8 class ProfileMiddleware(object):
9 """
10 Displays hotshot profiling for any view.
11 http://yoursite.com/yourview/?prof
12
13 Add the "prof" key to query string by appending ?prof (or &prof=)
14 and you'll see the profiling results in your browser.
15 It's set up to only be available in django's debug mode,
16 but you really shouldn't add this middleware to any production configuration.
17 * Only tested on Linux
18 """
19 def process_request(self, request):
20 if settings.DEBUG and request.GET.has_key('prof'):
21 self.tmpfile = tempfile.NamedTemporaryFile()
22 self.prof = hotshot.Profile(self.tmpfile.name)
23
24 def process_view(self, request, callback, callback_args, callback_kwargs):
25 if settings.DEBUG and request.GET.has_key('prof'):
26 return self.prof.runcall(callback, request, *callback_args, **callback_kwargs)
27
28 def process_response(self, request, response):
29 if settings.DEBUG and request.GET.has_key('prof'):
30 self.prof.close()
31
32 out = StringIO()
33 old_stdout = sys.stdout
34 sys.stdout = out
35
36 stats = hotshot.stats.load(self.tmpfile.name)
37 #stats.strip_dirs()
38 stats.sort_stats('cumulative', 'calls')
39 stats.print_stats()
40
41 sys.stdout = old_stdout
42 stats_str = out.getvalue()
43
44 if response and response.content and stats_str:
45 response.content = "<pre>" + stats_str + "</pre>"
46
47 return response