Changeset 2569
- Timestamp:
- 01/28/10 13:13:46
- Files:
-
- branches/cherrypy-3.1.x/cherrypy/lib/covercp.py (modified) (9 diffs)
- branches/cherrypy-3.1.x/cherrypy/test/test.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cherrypy-3.1.x/cherrypy/lib/covercp.py
r1814 r2569 31 31 import StringIO 32 32 33 the_coverage = None 33 34 try: 34 from coverage import the_coverage as coverage 35 from coverage import coverage 36 the_coverage = coverage(data_file=localFile) 35 37 def start(threadid=None): 36 coverage.start() 38 the_coverage.start() 39 40 def save(threadid=None): 41 the_coverage.save() 37 42 except ImportError: 38 43 # Setting coverage to None will raise errors 39 44 # that need to be trapped downstream. 40 coverage = None45 the_coverage = None 41 46 42 47 import warnings … … 44 49 45 50 def start(threadid=None): 51 pass 52 def save(threadid=None): 46 53 pass 47 54 start.priority = 20 … … 188 195 return 0 189 196 190 def _show_branch(root, base, path, pct=0, showpct=False, exclude=""): 197 def _show_branch(root, base, path, pct=0, showpct=False, exclude="", 198 coverage=the_coverage): 191 199 192 200 # Show the directory name and any of our children … … 202 210 (newpath, urllib.quote_plus(exclude), name) 203 211 204 for chunk in _show_branch(root[name], base, newpath, pct, showpct, exclude ):212 for chunk in _show_branch(root[name], base, newpath, pct, showpct, exclude, coverage=coverage): 205 213 yield chunk 206 214 … … 254 262 d = d.setdefault(node, {}) 255 263 256 def get_tree(base, exclude ):264 def get_tree(base, exclude, coverage=the_coverage): 257 265 """Return covered module names as a nested dict.""" 258 266 tree = {} 259 coverage.get_ready() 260 runs = coverage.cexecuted.keys() 261 if runs: 262 for path in runs: 263 if not _skip_file(path, exclude) and not os.path.isdir(path): 264 _graft(path, tree) 267 runs = coverage.data.executed_files() 268 for path in runs: 269 if not _skip_file(path, exclude) and not os.path.isdir(path): 270 _graft(path, tree) 265 271 return tree 266 272 267 273 class CoverStats(object): 274 def __init__(self, coverage): 275 self.coverage = coverage 268 276 269 277 def index(self): … … 294 302 295 303 # Then display the tree 296 tree = get_tree(base, exclude )304 tree = get_tree(base, exclude, self.coverage) 297 305 if not tree: 298 306 yield "<p>No modules covered.</p>" 299 307 else: 300 308 for chunk in _show_branch(tree, base, "/", pct, 301 showpct=='checked', exclude ):309 showpct=='checked', exclude, coverage=self.coverage): 302 310 yield chunk 303 311 … … 329 337 330 338 def report(self, name): 331 coverage.get_ready() 332 filename, statements, excluded, missing, _ = coverage.analysis2(name) 339 filename, statements, excluded, missing, _ = self.coverage.analysis2(name) 333 340 pc = _percent(statements, missing) 334 341 yield TEMPLATE_COVERAGE % dict(name=os.path.basename(name), … … 346 353 347 354 def serve(path=localFile, port=8080): 348 if coverage is None:355 if the_coverage is None: 349 356 raise ImportError("The coverage module could not be imported.") 350 coverage.cache_default = path 357 from coverage import coverage 358 cov = coverage(data_file = path) 359 cov.load() 351 360 352 361 import cherrypy … … 355 364 'environment': "production", 356 365 }) 357 cherrypy.quickstart(CoverStats( ))366 cherrypy.quickstart(CoverStats(cov)) 358 367 359 368 if __name__ == "__main__": branches/cherrypy-3.1.x/cherrypy/test/test.py
r2483 r2569 230 230 """ 231 231 try: 232 from coverage import the_coverage as coverage 233 c = os.path.join(os.path.dirname(__file__), "../lib/coverage.cache") 234 coverage.cache_default = c 235 if c and os.path.exists(c): 236 os.remove(c) 237 coverage.start() 232 from coverage import coverage 233 if self.basedir: 234 c = os.path.join(self.basedir, 'coverage.cache') 235 else: 236 c = os.path.join(os.path.dirname(__file__), "../lib/coverage.cache") 237 cov = coverage(data_file=c) 238 #start coverage before importing cherrypy 239 cov.erase() 240 cov.start() 238 241 import cherrypy 239 242 from cherrypy.lib import covercp 243 covercp.the_coverage = cov 240 244 cherrypy.engine.subscribe('start', covercp.start) 241 245 cherrypy.engine.subscribe('start_thread', covercp.start) 242 246 except ImportError: 243 cov erage= None244 self.coverage = cov erage247 cov = None 248 self.coverage = cov 245 249 246 250 def stop_coverage(self): … … 268 272 basedir = basedir.lower() 269 273 270 self.coverage. get_ready()271 morfs = [x for x in self.coverage. cexecuted274 self.coverage.load() 275 morfs = [x for x in self.coverage.data.executed_files() 272 276 if x.lower().startswith(basedir)] 273 277

