def _init(self, module_name, module): source_file, stmts, excluded, missed, missed_display = coverage.analysis2(module) executed = list(set(stmts).difference(missed)) total = list(set(stmts).union(excluded)) total.sort() title = module.__name__ total_count = len(total) executed_count = len(executed) excluded_count = len(excluded) missed_count = len(missed) try: percent_covered = float(len(executed))/len(stmts)*100 except ZeroDivisionError: percent_covered = 100 test_timestamp = time.strftime('%a %Y-%m-%d %H:%M %Z') severity = 'normal' if percent_covered < 75: severity = 'warning' if percent_covered < 50: severity = 'critical' for k, v in locals().items(): setattr(self, k, v)
def main(args): parser = optparse.OptionParser() parser.usage = "%prog [-o <output file>] [-t <testsuite file>] " "<testsuite root> [<alias>=]<covered path>+" parser.add_option("-o", "--output", dest="output_file", help="Write coverage data to FILE", metavar="FILE") parser.add_option( "-t", "--testsuite", dest="testsuite_file", help="Testsuite file FILE in which EXCLUDED_PATHS may be found", metavar="FILE", ) parser.set_defaults(testsuite_file="testsuite.py") (options, args) = parser.parse_args(args) if len(args) < 2: parser.error("Need a testsuite root and at least one covered path") test_root, path_pairs = args[0], args[1:] # Try to extract excluded paths from testsuite.py excludePaths = ["test", "setup.py"] found = False testsuite_path = os.path.join(test_root, options.testsuite_file) if os.path.exists(testsuite_path): testsuite_contents = open(testsuite_path).read() match = re.search("EXCLUDED_PATHS = (\[.*?\])", testsuite_contents, re.M | re.S) if match: excludePaths = eval(match.group(1)) found = True if not found: print >> sys.stderr, "EXCLUDED_PATHS not found in %s" % testsuite_path # Create mapping of coverage base dirs and their aliases baseDirs = [] trees = {} for path_pair in path_pairs: if "=" in path_pair: tree, path = path_pair.split("=", 1) else: tree, path = "__main__", path_pair path = os.path.abspath(path) baseDirs.append(path) if tree in trees: print >> sys.stderr, "Warning: path %s is replacing path %s " "in tree %s" % (path, trees[tree], tree) trees[tree] = path # Find all files to cover files, notExists = getFilesToAnnotate(baseDirs, exclude=excludePaths) # Collect data from this coverage run global coverage coverage = coverage.the_coverage coverage.cacheDir = os.path.join(test_root, ".coverage") coverage.restoreDir() # Analyze data and map files to sets of total and missing statements cover_data = {} for file in files: _, statements, _, missing, _ = coverage.analysis2(file) tree = tree_path = None for name, path in trees.iteritems(): if file.startswith(path + os.path.sep): tree, tree_path = name, path break if tree: subpath = file[len(tree_path) + 1 :] if tree == "__main__": path = subpath else: path = tree + "/" + subpath cover_data[path] = (statements, missing) else: print >> sys.stderr, "File %s is part of the coverage report, " "but not part of any configured tree" # Dump a pickle in the requested format if options.output_file: cPickle.dump(cover_data, open(options.output_file, "wb"), 2) else: out = cPickle.dumps(cover_data, 2) out = bz2.compress(out) out = out.encode("base64").strip() print "-----BEGIN COVERAGE DATA-----" print out print "-----END COVERAGE DATA-----"