"""
* ====================================================================
* Copyright (c) 2001 Jon Travis. All rights reserved.
* See the file COPYING for information on usage and redistribution.
* ====================================================================
"""
import cgi
import mod_snake
import sys
import time
import StringIO
import pstats
class SnakeInfo:
def __init__(self, module):
directives = { "SnakeInfoURI" : (mod_snake.RSRC_CONF, mod_snake.TAKE1,
self.cmd_SnakeInfoURI)
}
module.add_directives(directives)
hooks = { "create_svr_config" : self.create_svr_config,
"content_handler" : self.content_handler,
}
for hook in hooks.keys():
module.add_hook(hook, hooks[hook])
def create_svr_config(self, svr):
return { 'server' : svr,
'infouri' : '/mod_snake_info' }
def cmd_SnakeInfoURI(self, per_dir, per_svr, take1):
per_svr['infouri'] = take1
def content_handler(self, per_dir, per_svr, req):
if req.uri != per_svr['infouri'] or req.method != 'GET':
return mod_snake.DECLINED
req.content_type = 'text/html'
req.send_http_header()
self.send_header(req)
args = cgi.parse_qs(req.args or "")
if not len(args):
self.send_loaded_module_list(req, per_svr['infouri'])
if args.has_key('mode'):
for key in args['mode']:
if key == 'profile':
self.send_profile(req, args)
if key == 'hooks':
self.send_hooks(req, args)
self.send_trailer(req)
return mod_snake.OK
def find_mod_by_name(self, modname):
for mod in mod_snake.get_modules():
if mod.name == modname:
return mod
def reformulate_uri(self, req, args):
res = req.uri + '?'
for key in args.keys():
for val in args[key]:
res = '%s%s=%s&' % (res, key, val)
if res[-1] == '&': return res[:-1]
return res
def get_profile_sort_methods(self):
methods = pstats.Stats.sort_arg_dict_default
descs = []
res = {}
for method in methods.keys():
if methods[method][1] in descs: continue
res[method] = methods[method]
descs.append(methods[method][1])
return res
def send_hooks(self, req, args):
req.rwrite('
Module Hooks
\n')
if not args.has_key('modname'): return
for mod in args['modname']:
req.rwrite('%s
\n' % mod)
module = self.find_mod_by_name(mod)
req.rwrite('| HookName | \n')
req.rwrite('Called Function |
\n')
hooks = module.get_hooks()
for hook in hooks.keys():
req.rwrite('| %s | %s |
\n' %
(hook, hooks[hook].__name__))
req.rwrite('
\n')
def send_profile(self, req, args):
req.rwrite('Profile Information
\n')
if not args.has_key('modname'): return
# Loop through the specificities given to us, checking each one
specs = []
req.rwrite('Current Sort
')
if args.has_key('profspec'):
for key in args['profspec']:
if key in self.get_profile_sort_methods().keys():
specs.append(key)
del args['profspec']
new_profspec = self.reformulate_uri(req, args)
for spec in specs:
req.rwrite('[%s] ' %
(new_profspec,
self.get_profile_sort_methods()[spec][1]))
new_profspec = '%s&profspec=%s' % (new_profspec, spec)
req.rwrite('
Next Sort
')
args['profspec'] = specs
save_profspec = new_profspec = self.reformulate_uri(req, args)
for spec in self.get_profile_sort_methods().keys():
if spec in specs: continue
new_profspec = '%s&profspec=%s' % (save_profspec, spec)
req.rwrite('[%s] ' %
(new_profspec,
self.get_profile_sort_methods()[spec][1]))
for mod in args['modname']:
req.rwrite('%s
\n' % mod)
profile_obj = self.find_mod_by_name(mod).profile_obj
if not profile_obj:
req.rwrite('Profiling disabled for this module\n')
continue
req.rwrite('')
fakefile = StringIO.StringIO()
sys.stdout, save = fakefile, sys.stdout
stats = pstats.Stats(profile_obj).strip_dirs()
for spec in specs:
stats.sort_stats(spec)
stats.print_stats()
sys.stdout = save
req.rwrite(fakefile.getvalue())
req.rwrite('')
def send_loaded_module_list(self, req, uri):
req.rwrite('Loaded Modules
\n')
req.rwrite('')
req.rwrite('| Name |
')
for mod in mod_snake.get_modules():
req.rwrite('| %s | ' % mod.name)
req.rwrite('[profile] | ' %
(uri + '?mode=profile&modname=' + mod.name))
req.rwrite('[hooks] | ' %
(uri + '?mode=hooks&modname=' + mod.name))
req.rwrite('
')
req.rwrite('
\n')
def send_header(self, req):
req.rwrite('Mod Snake Info\n')
req.rwrite('Mod Snake Info
\n')
req.rwrite('Server Time: %s
\n' % time.ctime(time.time()))
def send_trailer(self, req):
req.rwrite('\n')