bbangert / dozer


WSGI middleware fork of Robert Brewer's Dowser
Clone URL : http://bitbucket.org/bbangert/dozer/ (size: 146.1 KB)
commit 39: bf668c368e99
parent 38: f2017ea56452
branch: trunk
nicer interface
zepolen@armor
4 months ago

Changed (Δ395 bytes):

raw changeset »

dozer/profile.py (24 lines added, 13 lines removed)

Up to file-list dozer/profile.py:

@@ -1,5 +1,6 @@ import cProfile
1
1
import cProfile
2
2
import cPickle
3
import errno
3
4
import time
4
5
import os
5
6
import re
@@ -73,32 +74,42 @@ class Profiler(object):
73
74
        return res
74
75
    show.exposed = True
75
76
76
    def all(self, req):
77
    def showall(self, req):
77
78
        dir_name = self.profile_path
78
        link = '<a href="/_profiler/show/%(pid)s">%(pid)s</a>'
79
79
        profiles = []
80
80
        for profile_file in os.listdir(dir_name):
81
81
            if profile_file.endswith('.pkl'):
82
                modified = os.stat(
83
                    os.path.join(self.profile_path, profile_file)
84
                    ).st_mtime
85
                profiles.append((modified, profile_file))
82
                path = os.path.join(self.profile_path, profile_file)
83
                modified = os.stat(path).st_mtime
84
                data = cPickle.load(open(path, 'rb'))
85
                environ = data['environ']
86
                profiles.append((modified, environ, profile_file[:-4]))
87
86
88
        profiles.sort(reverse=True)
87
89
        res = Response()
88
        if profiles:
89
            delete_link = ['<a href="/_profiler/delete">delete all profiles</a>']
90
            res.body = '<br>'.join(delete_link + [link % {'pid': name[:-4]} for (m, name) in profiles])
91
        else:
92
            res.body = 'no profiles'
90
        res.body = self.render('/list_profiles.mako', profiles=profiles,
91
                               now=time.time())
93
92
        return res
94
    all.exposed = True
93
    showall.exposed = True
95
94
96
95
    def delete(self, req):
96
        profile_id = req.path_info_pop()
97
        if profile_id: # this prob a security risk
98
            try:
99
                for ext in ('.gv', '.pkl'):
100
                    os.unlink(os.path.join(self.profile_path, profile_id + ext))
101
            except OSError, e:
102
                if e.errno == errno.ENOENT:
103
                    pass # allow a file not found exception
104
                else:
105
                    raise
106
            return Response('deleted %s' % profile_id)
107
97
108
        for filename in os.listdir(self.profile_path):
98
109
            if filename.endswith('.pkl') or filename.endswith('.gv'):
99
110
                os.unlink(os.path.join(self.profile_path, filename))
100
111
        res = Response()
101
        res.location = '/_profiler/all'
112
        res.location = '/_profiler/showall'
102
113
        res.status_int = 302
103
114
        return res
104
115
    delete.exposed = True