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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import sys
  6. import cgi
  7. import urllib
  8. import os
  9. import os.path as os
  10. localFile = os.path.join(os.path.dirname(__file__), 'coverage.cache')
  11.  
  12. try:
  13.     import cStringIO as StringIO
  14. except ImportError:
  15.     import StringIO
  16.  
  17.  
  18. try:
  19.     from coverage import the_coverage as coverage
  20.     
  21.     def start(threadid = None):
  22.         coverage.start()
  23.  
  24. except ImportError:
  25.     coverage = None
  26.     import warnings
  27.     warnings.warn('No code coverage will be performed; coverage.py could not be imported.')
  28.     
  29.     def start(threadid = None):
  30.         pass
  31.  
  32.  
  33. start.priority = 20
  34. import cherrypy
  35. initial_base = os.path.dirname(cherrypy.__file__)
  36. TEMPLATE_MENU = '<html>\n<head>\n    <title>CherryPy Coverage Menu</title>\n    <style>\n        body {font: 9pt Arial, serif;}\n        #tree {\n            font-size: 8pt;\n            font-family: Andale Mono, monospace;\n            white-space: pre;\n            }\n        #tree a:active, a:focus {\n            background-color: black;\n            padding: 1px;\n            color: white;\n            border: 0px solid #9999FF;\n            -moz-outline-style: none;\n            }\n        .fail { color: red;}\n        .pass { color: #888;}\n        #pct { text-align: right;}\n        h3 {\n            font-size: small;\n            font-weight: bold;\n            font-style: italic;\n            margin-top: 5px; \n            }\n        input { border: 1px solid #ccc; padding: 2px; }\n        .directory {\n            color: #933;\n            font-style: italic;\n            font-weight: bold;\n            font-size: 10pt;\n            }\n        .file {\n            color: #400;\n            }\n        a { text-decoration: none; }\n        #crumbs {\n            color: white;\n            font-size: 8pt;\n            font-family: Andale Mono, monospace;\n            width: 100%;\n            background-color: black;\n            }\n        #crumbs a {\n            color: #f88;\n            }\n        #options {\n            line-height: 2.3em;\n            border: 1px solid black;\n            background-color: #eee;\n            padding: 4px;\n            }\n        #exclude {\n            width: 100%;\n            margin-bottom: 3px;\n            border: 1px solid #999;\n            }\n        #submit {\n            background-color: black;\n            color: white;\n            border: 0;\n            margin-bottom: -9px;\n            }\n    </style>\n</head>\n<body>\n<h2>CherryPy Coverage</h2>'
  37. TEMPLATE_FORM = '\n<div id="options">\n<form action=\'menu\' method=GET>\n    <input type=\'hidden\' name=\'base\' value=\'%(base)s\' />\n    Show percentages <input type=\'checkbox\' %(showpct)s name=\'showpct\' value=\'checked\' /><br />\n    Hide files over <input type=\'text\' id=\'pct\' name=\'pct\' value=\'%(pct)s\' size=\'3\' />%%<br />\n    Exclude files matching<br />\n    <input type=\'text\' id=\'exclude\' name=\'exclude\' value=\'%(exclude)s\' size=\'20\' />\n    <br />\n\n    <input type=\'submit\' value=\'Change view\' id="submit"/>\n</form>\n</div>'
  38. TEMPLATE_FRAMESET = "<html>\n<head><title>CherryPy coverage data</title></head>\n<frameset cols='250, 1*'>\n    <frame src='menu?base=%s' />\n    <frame name='main' src='' />\n</frameset>\n</html>\n" % initial_base.lower()
  39. TEMPLATE_COVERAGE = '<html>\n<head>\n    <title>Coverage for %(name)s</title>\n    <style>\n        h2 { margin-bottom: .25em; }\n        p { margin: .25em; }\n        .covered { color: #000; background-color: #fff; }\n        .notcovered { color: #fee; background-color: #500; }\n        .excluded { color: #00f; background-color: #fff; }\n         table .covered, table .notcovered, table .excluded\n             { font-family: Andale Mono, monospace;\n               font-size: 10pt; white-space: pre; }\n\n         .lineno { background-color: #eee;}\n         .notcovered .lineno { background-color: #000;}\n         table { border-collapse: collapse;\n    </style>\n</head>\n<body>\n<h2>%(name)s</h2>\n<p>%(fullpath)s</p>\n<p>Coverage: %(pc)s%%</p>'
  40. TEMPLATE_LOC_COVERED = '<tr class="covered">\n    <td class="lineno">%s </td>\n    <td>%s</td>\n</tr>\n'
  41. TEMPLATE_LOC_NOT_COVERED = '<tr class="notcovered">\n    <td class="lineno">%s </td>\n    <td>%s</td>\n</tr>\n'
  42. TEMPLATE_LOC_EXCLUDED = '<tr class="excluded">\n    <td class="lineno">%s </td>\n    <td>%s</td>\n</tr>\n'
  43. TEMPLATE_ITEM = "%s%s<a class='file' href='report?name=%s' target='main'>%s</a>\n"
  44.  
  45. def _percent(statements, missing):
  46.     s = len(statements)
  47.     e = s - len(missing)
  48.     if s > 0:
  49.         return int(round(100 * e / s))
  50.     return 0
  51.  
  52.  
  53. def _show_branch(root, base, path, pct = 0, showpct = False, exclude = ''):
  54.     dirs = _[1]
  55.     dirs.sort()
  56.     for name in dirs:
  57.         newpath = os.path.join(path, name)
  58.         for chunk in _show_branch(root[name], base, newpath, pct, showpct, exclude):
  59.             yield chunk
  60.             [] if newpath.lower().startswith(base) else []
  61.         
  62.     
  63.     if path.lower().startswith(base):
  64.         relpath = path[len(base):]
  65.         files = _[2]
  66.         files.sort()
  67.         for name in files:
  68.             newpath = os.path.join(path, name)
  69.             pc_str = ''
  70.             yield TEMPLATE_ITEM % ('| ' * (relpath.count(os.sep) + 1), pc_str, newpath, name)
  71.             None if showpct else []
  72.         
  73.     
  74.  
  75.  
  76. def _skip_file(path, exclude):
  77.     if exclude:
  78.         return bool(re.search(exclude, path))
  79.  
  80.  
  81. def _graft(path, tree):
  82.     d = tree
  83.     p = path
  84.     atoms = []
  85.     while True:
  86.         (p, tail) = os.path.split(p)
  87.         if not tail:
  88.             break
  89.         
  90.         atoms.append(tail)
  91.     atoms.append(p)
  92.     if p != '/':
  93.         atoms.append('/')
  94.     
  95.     atoms.reverse()
  96.     for node in atoms:
  97.         if node:
  98.             d = d.setdefault(node, { })
  99.             continue
  100.     
  101.  
  102.  
  103. def get_tree(base, exclude):
  104.     tree = { }
  105.     coverage.get_ready()
  106.     runs = coverage.cexecuted.keys()
  107.     if runs:
  108.         for path in runs:
  109.             if not _skip_file(path, exclude) and not os.path.isdir(path):
  110.                 _graft(path, tree)
  111.                 continue
  112.         
  113.     
  114.     return tree
  115.  
  116.  
  117. class CoverStats(object):
  118.     
  119.     def index(self):
  120.         return TEMPLATE_FRAMESET
  121.  
  122.     index.exposed = True
  123.     
  124.     def menu(self, base = '/', pct = '50', showpct = '', exclude = 'python\\d\\.\\d|test|tut\\d|tutorial'):
  125.         base = base.lower().rstrip(os.sep)
  126.         yield TEMPLATE_MENU
  127.         yield TEMPLATE_FORM % locals()
  128.         yield "<div id='crumbs'>"
  129.         path = ''
  130.         atoms = base.split(os.sep)
  131.         atoms.pop()
  132.         for atom in atoms:
  133.             path += atom + os.sep
  134.             yield "<a href='menu?base=%s&exclude=%s'>%s</a> %s" % (path, urllib.quote_plus(exclude), atom, os.sep)
  135.         
  136.         yield '</div>'
  137.         yield "<div id='tree'>"
  138.         tree = get_tree(base, exclude)
  139.         if not tree:
  140.             yield '<p>No modules covered.</p>'
  141.         else:
  142.             for chunk in _show_branch(tree, base, '/', pct, showpct == 'checked', exclude):
  143.                 yield chunk
  144.             
  145.         yield '</div>'
  146.         yield '</body></html>'
  147.  
  148.     menu.exposed = True
  149.     
  150.     def annotated_file(self, filename, statements, excluded, missing):
  151.         source = open(filename, 'r')
  152.         buffer = []
  153.         for lineno, line in enumerate(source.readlines()):
  154.             lineno += 1
  155.             line = line.strip('\n\r')
  156.             empty_the_buffer = True
  157.             if lineno in excluded:
  158.                 template = TEMPLATE_LOC_EXCLUDED
  159.             elif lineno in missing:
  160.                 template = TEMPLATE_LOC_NOT_COVERED
  161.             elif lineno in statements:
  162.                 template = TEMPLATE_LOC_COVERED
  163.             else:
  164.                 empty_the_buffer = False
  165.                 buffer.append((lineno, line))
  166.             if empty_the_buffer:
  167.                 for lno, pastline in buffer:
  168.                     yield template % (lno, cgi.escape(pastline))
  169.                 
  170.                 buffer = []
  171.                 yield template % (lineno, cgi.escape(line))
  172.                 continue
  173.         
  174.  
  175.     
  176.     def report(self, name):
  177.         coverage.get_ready()
  178.         (filename, statements, excluded, missing, _) = coverage.analysis2(name)
  179.         pc = _percent(statements, missing)
  180.         yield TEMPLATE_COVERAGE % dict(name = os.path.basename(name), fullpath = name, pc = pc)
  181.         yield '<table>\n'
  182.         for line in self.annotated_file(filename, statements, excluded, missing):
  183.             yield line
  184.         
  185.         yield '</table>'
  186.         yield '</body>'
  187.         yield '</html>'
  188.  
  189.     report.exposed = True
  190.  
  191.  
  192. def serve(path = localFile, port = 8080):
  193.     if coverage is None:
  194.         raise ImportError('The coverage module could not be imported.')
  195.     coverage is None
  196.     coverage.cache_default = path
  197.     import cherrypy
  198.     cherrypy.config.update({
  199.         'server.socket_port': port,
  200.         'server.thread_pool': 10,
  201.         'environment': 'production' })
  202.     cherrypy.quickstart(CoverStats())
  203.  
  204. if __name__ == '__main__':
  205.     serve(*tuple(sys.argv[1:]))
  206.  
  207.