blob: a46ddfc6e9bfda3050d2918cc1ce66ad08b7a7cc [file] [log] [blame]
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS. All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.
"""This script grabs and reports coverage information.
It grabs coverage information from the latest Linux 32-bit build and
pushes it to the coverage tracker, enabling us to track code coverage
over time. This script is intended to run on the 32-bit Linux slave.
This script requires an access.token file in the current directory, as
generated by the script. It also expects a file
customer.secret with a single line containing the customer secret. The
customer secret is an OAuth concept and is received when one registers the
application with the App Engine running the dashboard.
The script assumes that all coverage data is stored under
/home/<build bot user>/www.
__author__ = ' (Patrik Höglund)'
import os
import re
import time
import constants
import dashboard_connection
class FailedToParseCoverageHtml(Exception):
class CouldNotFindCoverageDirectory(Exception):
def _find_latest_32bit_debug_build(www_directory_contents, coverage_www_dir):
"""Finds the latest 32-bit coverage directory in the directory listing.
Coverage directories have the form Linux32bitDBG_<number>. There may be
other directories in the list though, for instance for other build
# This sort ensures we will encounter the directory with the highest number
# first.
for entry in www_directory_contents:
match = re.match('Linux32DBG_\d+', entry)
if match is not None:
return entry
raise CouldNotFindCoverageDirectory('Error: Found no 32-bit '
'debug build in directory %s.' %
def _grab_coverage_percentage(label, index_html_contents):
"""Extracts coverage from a LCOV coverage report.
Grabs coverage by assuming that the label in the coverage HTML report
is close to the actual number and that the number is followed by a space
and a percentage sign.
match ='<td[^>]*>' + label + '</td>.*?(\d+\.\d) %',
index_html_contents, re.DOTALL)
if match is None:
raise FailedToParseCoverageHtml('Missing coverage at label "%s".' % label)
return float(
except ValueError:
raise FailedToParseCoverageHtml('%s is not a float.' %
def _report_coverage_to_dashboard(dashboard, now, line_coverage,
parameters = {'date': '%d' % now,
'line_coverage': '%f' % line_coverage,
'function_coverage': '%f' % function_coverage
dashboard.send_post_request(constants.ADD_COVERAGE_DATA_URL, parameters)
def _main():
dashboard = dashboard_connection.DashboardConnection(constants.CONSUMER_KEY)
www_dir_contents = os.listdir(BUILD_BOT_COVERAGE_WWW_DIRECTORY)
latest_build_directory = _find_latest_32bit_debug_build(www_dir_contents,
index_html_path = os.path.join(coverage_www_dir, latest_build_directory,
index_html_file = open(index_html_path)
whole_file =
line_coverage = _grab_coverage_percentage('Lines:', whole_file)
function_coverage = _grab_coverage_percentage('Functions:', whole_file)
now = int(time.time())
_report_coverage_to_dashboard(dashboard, now, line_coverage,
if __name__ == '__main__':