home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1462 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.9 KB  |  160 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4.  
  5. def new_func_strip_path(func_name):
  6.     (filename, line, name) = func_name
  7.     if filename.endswith('__init__.py'):
  8.         return (os.path.basename(filename[:-12]) + filename[-12:], line, name)
  9.     return (os.path.basename(filename), line, name)
  10.  
  11.  
  12. try:
  13.     import profile
  14.     import pstats
  15.     pstats.func_strip_path = new_func_strip_path
  16. except ImportError:
  17.     profile = None
  18.     pstats = None
  19.     import warnings
  20.     msg = "Your installation of Python does not have a profile module. If you're on Debian, you can apt-get python2.4-profiler from non-free in a separate step. See http://www.cherrypy.org/wiki/ProfilingOnDebian for details."
  21.     warnings.warn(msg)
  22.  
  23. import os
  24. import os.path as os
  25. import sys
  26.  
  27. try:
  28.     import cStringIO as StringIO
  29. except ImportError:
  30.     import StringIO
  31.  
  32. _count = 0
  33.  
  34. class Profiler(object):
  35.     
  36.     def __init__(self, path = None):
  37.         if not path:
  38.             path = os.path.join(os.path.dirname(__file__), 'profile')
  39.         
  40.         self.path = path
  41.         if not os.path.exists(path):
  42.             os.makedirs(path)
  43.         
  44.  
  45.     
  46.     def run(self, func, *args, **params):
  47.         global _count
  48.         c = _count = _count + 1
  49.         path = os.path.join(self.path, 'cp_%04d.prof' % c)
  50.         prof = profile.Profile()
  51.         result = prof.runcall(func, *args, **params)
  52.         prof.dump_stats(path)
  53.         return result
  54.  
  55.     
  56.     def statfiles(self):
  57.         return _[1]
  58.  
  59.     
  60.     def stats(self, filename, sortby = 'cumulative'):
  61.         sio = StringIO.StringIO()
  62.         if sys.version_info >= (2, 5):
  63.             s = pstats.Stats(os.path.join(self.path, filename), stream = sio)
  64.             s.strip_dirs()
  65.             s.sort_stats(sortby)
  66.             s.print_stats()
  67.         else:
  68.             s = pstats.Stats(os.path.join(self.path, filename))
  69.             s.strip_dirs()
  70.             s.sort_stats(sortby)
  71.             oldout = sys.stdout
  72.             
  73.             try:
  74.                 sys.stdout = sio
  75.                 s.print_stats()
  76.             finally:
  77.                 sys.stdout = oldout
  78.  
  79.         response = sio.getvalue()
  80.         sio.close()
  81.         return response
  82.  
  83.     
  84.     def index(self):
  85.         return "<html>\n        <head><title>CherryPy profile data</title></head>\n        <frameset cols='200, 1*'>\n            <frame src='menu' />\n            <frame name='main' src='' />\n        </frameset>\n        </html>\n        "
  86.  
  87.     index.exposed = True
  88.     
  89.     def menu(self):
  90.         yield '<h2>Profiling runs</h2>'
  91.         yield '<p>Click on one of the runs below to see profiling data.</p>'
  92.         runs = self.statfiles()
  93.         runs.sort()
  94.         for i in runs:
  95.             yield "<a href='report?filename=%s' target='main'>%s</a><br />" % (i, i)
  96.         
  97.  
  98.     menu.exposed = True
  99.     
  100.     def report(self, filename):
  101.         import cherrypy
  102.         cherrypy.response.headers['Content-Type'] = 'text/plain'
  103.         return self.stats(filename)
  104.  
  105.     report.exposed = True
  106.  
  107.  
  108. class ProfileAggregator(Profiler):
  109.     
  110.     def __init__(self, path = None):
  111.         global _count
  112.         Profiler.__init__(self, path)
  113.         self.count = _count = _count + 1
  114.         self.profiler = profile.Profile()
  115.  
  116.     
  117.     def run(self, func, *args):
  118.         path = os.path.join(self.path, 'cp_%04d.prof' % self.count)
  119.         result = self.profiler.runcall(func, *args)
  120.         self.profiler.dump_stats(path)
  121.         return result
  122.  
  123.  
  124.  
  125. class make_app:
  126.     
  127.     def __init__(self, nextapp, path = None, aggregate = False):
  128.         self.nextapp = nextapp
  129.         self.aggregate = aggregate
  130.         if aggregate:
  131.             self.profiler = ProfileAggregator(path)
  132.         else:
  133.             self.profiler = Profiler(path)
  134.  
  135.     
  136.     def __call__(self, environ, start_response):
  137.         
  138.         def gather():
  139.             result = []
  140.             for line in self.nextapp(environ, start_response):
  141.                 result.append(line)
  142.             
  143.             return result
  144.  
  145.         return self.profiler.run(gather)
  146.  
  147.  
  148.  
  149. def serve(path = None, port = 8080):
  150.     import cherrypy
  151.     cherrypy.config.update({
  152.         'server.socket_port': int(port),
  153.         'server.thread_pool': 10,
  154.         'environment': 'production' })
  155.     cherrypy.quickstart(Profiler(path))
  156.  
  157. if __name__ == '__main__':
  158.     serve(*tuple(sys.argv[1:]))
  159.  
  160.