Anonymous avatar Anonymous committed 7f2c205

Update coverage support to 3.2. Should be a little bit faster.

Comments (0)

Files changed (2)

cherrypy/lib/covercp.py

 except ImportError:
     import StringIO
 
+the_coverage = None
 try:
-    from coverage import the_coverage as coverage
+    from coverage import coverage
+    the_coverage = coverage(data_file=localFile)
     def start(threadid=None):
-        coverage.start()
+        the_coverage.start()
+
+    def save(threadid=None):
+        the_coverage.save()
 except ImportError:
     # Setting coverage to None will raise errors
     # that need to be trapped downstream.
-    coverage = None
+    the_coverage = None
     
     import warnings
     warnings.warn("No code coverage will be performed; coverage.py could not be imported.")
     
     def start(threadid=None):
         pass
+    def save(threadid=None):
+        pass
 start.priority = 20
 
 # Guess initial depth to hide FIXME this doesn't work for non-cherrypy stuff
         return int(round(100.0 * e / s))
     return 0
 
-def _show_branch(root, base, path, pct=0, showpct=False, exclude=""):
+def _show_branch(root, base, path, pct=0, showpct=False, exclude="",
+                 coverage=the_coverage):
     
     # Show the directory name and any of our children
     dirs = [k for k, v in root.iteritems() if v]
             yield "<a class='directory' href='menu?base=%s&exclude=%s'>%s</a>\n" % \
                    (newpath, urllib.quote_plus(exclude), name)
         
-        for chunk in _show_branch(root[name], base, newpath, pct, showpct, exclude):
+        for chunk in _show_branch(root[name], base, newpath, pct, showpct, exclude, coverage=coverage):
             yield chunk
     
     # Now list the files
         if node:
             d = d.setdefault(node, {})
 
-def get_tree(base, exclude):
+def get_tree(base, exclude, coverage=the_coverage):
     """Return covered module names as a nested dict."""
     tree = {}
-    coverage.get_ready()
-    runs = coverage.cexecuted.keys()
-    if runs:
-        for path in runs:
-            if not _skip_file(path, exclude) and not os.path.isdir(path):
-                _graft(path, tree)
+    runs = coverage.data.executed_files()
+    for path in runs:
+        if not _skip_file(path, exclude) and not os.path.isdir(path):
+            _graft(path, tree)
     return tree
 
 class CoverStats(object):
+    def __init__(self, coverage):
+        self.coverage = coverage
     
     def index(self):
         return TEMPLATE_FRAMESET
         yield "<div id='tree'>"
         
         # Then display the tree
-        tree = get_tree(base, exclude)
+        tree = get_tree(base, exclude, self.coverage)
         if not tree:
             yield "<p>No modules covered.</p>"
         else:
             for chunk in _show_branch(tree, base, "/", pct,
-                                      showpct=='checked', exclude):
+                                      showpct=='checked', exclude, coverage=self.coverage):
                 yield chunk
         
         yield "</div>"
                 yield template % (lineno, cgi.escape(line))
     
     def report(self, name):
-        coverage.get_ready()
-        filename, statements, excluded, missing, _ = coverage.analysis2(name)
+        filename, statements, excluded, missing, _ = self.coverage.analysis2(name)
         pc = _percent(statements, missing)
         yield TEMPLATE_COVERAGE % dict(name=os.path.basename(name),
                                        fullpath=name,
 
 
 def serve(path=localFile, port=8080):
-    if coverage is None:
+    if the_coverage is None:
         raise ImportError("The coverage module could not be imported.")
-    coverage.cache_default = path
+    from coverage import coverage
+    cov = coverage(data_file = path)
+    cov.load()
     
     import cherrypy
     cherrypy.config.update({'server.socket_port': port,
                             'server.thread_pool': 10,
                             'environment': "production",
                             })
-    cherrypy.quickstart(CoverStats())
+    cherrypy.quickstart(CoverStats(cov))
 
 if __name__ == "__main__":
     serve(*tuple(sys.argv[1:]))

cherrypy/test/test.py

         coverage is silently(!) disabled.
         """
         try:
-            from coverage import the_coverage as coverage
-            c = os.path.join(os.path.dirname(__file__), "../lib/coverage.cache")
-            coverage.cache_default = c
-            if c and os.path.exists(c):
-                os.remove(c)
-            coverage.start()
+            from coverage import coverage
+            if self.basedir:
+                c = os.path.join(self.basedir, 'coverage.cache')
+            else:
+                c = os.path.join(os.path.dirname(__file__), "../lib/coverage.cache")
+            cov = coverage(data_file=c)
+            #start coverage before importing cherrypy
+            cov.erase()
+            cov.start()
             import cherrypy
             from cherrypy.lib import covercp
+            covercp.the_coverage = cov
             cherrypy.engine.subscribe('start', covercp.start)
             cherrypy.engine.subscribe('start_thread', covercp.start)
         except ImportError:
-            coverage = None
-        self.coverage = coverage
+            cov = None
+        self.coverage = cov
     
     def stop_coverage(self):
         """Stop the coverage tool, save results, and report."""
                 basedir = os.path.normpath(os.path.join(os.getcwd(), basedir))
         basedir = basedir.lower()
         
-        self.coverage.get_ready()
-        morfs = [x for x in self.coverage.cexecuted
+        self.coverage.load()
+        morfs = [x for x in self.coverage.data.executed_files()
                  if x.lower().startswith(basedir)]
         
         total_statements = 0
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.