Commit e87d3fa3 authored by Vincent Pelletier's avatar Vincent Pelletier

Add Zope-friendly subclasses for pprofile.

parent 443da0e4
import pprofile
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.encoders import encode_quopri
from cStringIO import StringIO
import os
_allsep = os.sep + (os.altsep or '')
def _relpath(name):
return os.path.normpath(os.path.splitdrive(name)[1]).lstrip(_allsep)
class ZopeBase(object):
__allow_access_to_unprotected_subobjects__ = 1
def _getFilename(self, filename, f_globals):
if 'Script (Python)' in filename:
try:
script = f_globals['script']
except KeyError:
pass
else:
filename = script.id
return filename
def asMIMEString(self):
"""
Return a mime-multipart representation of both callgrind profiling
statistics and all involved source code.
Avoids relying on a tempfile, as a zipfile/tarfile would require.
To unpack resulting file, see "unpack a MIME message" in
http://docs.python.org/2/library/email-examples.html
"""
result = MIMEMultipart()
out = StringIO()
self.callgrind(out, relative_path=True)
profile = MIMEApplication(out.getvalue(), 'x-kcachegrind', encode_quopri)
profile.add_header(
'Content-Disposition',
'attachment',
filename='cachegrind.out.pprofile',
)
result.attach(profile)
for name, lines in self.iterSource():
lines = ''.join(lines)
if lines:
pyfile = MIMEText(lines, 'x-python')
pyfile.add_header(
'Content-Disposition',
'attachment',
filename=_relpath(name),
)
result.attach(pyfile)
return result.as_string(), result['content-type']
class ZopeProfiler(ZopeBase, pprofile.Profile):
pass
class ZopeStatisticalProfile(ZopeBase, pprofile.StatisticalProfile):
pass
class ZopeStatisticalThread(pprofile.StatisticalThread):
__allow_access_to_unprotected_subobjects__ = 1
def getProfiler(verbose=False, **kw):
"""
Get a Zope-friendly pprofile.Profile instance.
"""
return ZopeProfiler(**kw)
def getStatisticalProfilerAndThread(**kw):
"""
Get Zope-friendly pprofile.StatisticalProfile and pprofile.StatisticalThread
instances. Arguments are forwarded to StatisticalThread.__init__ .
"""
profiler = ZopeStatisticalProfile()
return profiler, ZopeStatisticalThread(
profiler=profiler,
**kw
)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Extension Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_recorded_property_dict</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>default_reference</string> </key>
<value> <string>pprofile</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string>Zope-friendlyness layer for pprofile</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>extension.erp5.pprofile</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Extension Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.patches.WorkflowTool"/>
</pickle>
<pickle>
<tuple>
<none/>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>actor</string> </key>
<value> <string>vincent.pelletier</string> </value>
</item>
<item>
<key> <string>comment</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>time</string> </key>
<value>
<object>
<klass>
<global name="DateTime" module="DateTime.DateTime"/>
</klass>
<tuple>
<none/>
</tuple>
<state>
<tuple>
<float>1387376214.83</float>
<string>GMT+1</string>
</tuple>
</state>
</object>
</value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</tuple>
</pickle>
</record>
</ZopeData>
157
\ No newline at end of file
158
\ No newline at end of file
extension.erp5.ERP5Administration
extension.erp5.Security
\ No newline at end of file
extension.erp5.Security
extension.erp5.pprofile
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment