#!/usr/bin/env python3 #===- symcov-report-server.py - Coverage Reports HTTP Serve --*- python -*--===# # # The LLVM Compiler Infrastructure # # This file is distributed under the University of Illinois Open Source # License. See LICENSE.TXT for details. # #===------------------------------------------------------------------------===# '''(EXPERIMENTAL) HTTP server to browse coverage reports from .symcov files. Coverage reports for big binaries are too huge, generating them statically makes no sense. Start the server and go to localhost:8001 instead. Usage: ./tools/sancov/symcov-report-server.py \ --symcov coverage_data.symcov \ --srcpath root_src_dir Other options: --port port_number - specifies the port to use (8001) --host host_name - host name to bind server to (127.0.0.1) ''' import argparse import http.server import json import socketserver import time import html import os import string import math INDEX_PAGE_TMPL = """
File | Coverage |
---|---|
Files with 0 coverage are not shown. |
$content""" class SymcovData: def __init__(self, symcov_json): self.covered_points = frozenset(symcov_json['covered-points']) self.point_symbol_info = symcov_json['point-symbol-info'] self.file_coverage = self.compute_filecoverage() def filenames(self): return self.point_symbol_info.keys() def has_file(self, filename): return filename in self.point_symbol_info def compute_linemap(self, filename): """Build a line_number->css_class map.""" points = self.point_symbol_info.get(filename, dict()) line_to_points = dict() for fn, points in points.items(): for point, loc in points.items(): line = int(loc.split(":")[0]) line_to_points.setdefault(line, []).append(point) result = dict() for line, points in line_to_points.items(): status = "covered" covered_points = self.covered_points & set(points) if not len(covered_points): status = "not-covered" elif len(covered_points) != len(points): status = "partially-covered" result[line] = status return result def compute_filecoverage(self): """Build a filename->pct coverage.""" result = dict() for filename, fns in self.point_symbol_info.items(): file_points = [] for fn, points in fns.items(): file_points.extend(points.keys()) covered_points = self.covered_points & set(file_points) result[filename] = int(math.ceil( len(covered_points) * 100 / len(file_points))) return result def format_pct(pct): pct_str = str(max(0, min(100, pct))) zeroes = '0' * (3 - len(pct_str)) if zeroes: zeroes = '{0}'.format(zeroes) return zeroes + pct_str class ServerHandler(http.server.BaseHTTPRequestHandler): symcov_data = None src_path = None def do_GET(self): if self.path == '/': self.send_response(200) self.send_header("Content-type", "text/html; charset=utf-8") self.end_headers() filelist = [] for filename in sorted(self.symcov_data.filenames()): file_coverage = self.symcov_data.file_coverage[filename] if not file_coverage: continue filelist.append( "