blob: bcb08cb0889136dc2f748f1d974f4c00f630bea1 [file] [log] [blame]
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +00001#!/usr/bin/env python
2#-*- coding: utf-8 -*-
3# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
11"""This script grabs and reports coverage information.
12
13 It grabs coverage information from the latest Linux 32-bit build and
14 pushes it to the coverage tracker, enabling us to track code coverage
15 over time. This script is intended to run on the 32-bit Linux slave.
16
17 This script requires an access.token file in the current directory, as
18 generated by the request_oauth_permission.py script. It also expects a file
19 customer.secret with a single line containing the customer secret. The
20 customer secret is an OAuth concept and is received when one registers the
21 application with the App Engine running the dashboard.
22
23 The script assumes that all coverage data is stored under
24 /home/<build bot user>/www.
25"""
26
27__author__ = 'phoglund@webrtc.org (Patrik Höglund)'
28
29import os
30import re
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000031import time
32
33import constants
34import dashboard_connection
35
36
37class FailedToParseCoverageHtml(Exception):
38 pass
39
40
41class CouldNotFindCoverageDirectory(Exception):
42 pass
43
44
45def _find_latest_32bit_debug_build(www_directory_contents, coverage_www_dir):
46 """Finds the latest 32-bit coverage directory in the directory listing.
47
48 Coverage directories have the form Linux32bitDBG_<number>. There may be
49 other directories in the list though, for instance for other build
50 configurations.
51 """
52
53 # This sort ensures we will encounter the directory with the highest number
54 # first.
55 www_directory_contents.sort(reverse=True)
56
57 for entry in www_directory_contents:
58 match = re.match('Linux32bitDBG_\d+', entry)
59 if match is not None:
60 return entry
61
62 raise CouldNotFindCoverageDirectory('Error: Found no 32-bit '
63 'debug build in directory %s.' %
64 coverage_www_dir)
65
66
67def _grab_coverage_percentage(label, index_html_contents):
68 """Extracts coverage from a LCOV coverage report.
69
70 Grabs coverage by assuming that the label in the coverage HTML report
71 is close to the actual number and that the number is followed by a space
72 and a percentage sign.
73 """
74 match = re.search('<td[^>]*>' + label + '</td>.*?(\d+\.\d) %',
75 index_html_contents, re.DOTALL)
76 if match is None:
77 raise FailedToParseCoverageHtml('Missing coverage at label "%s".' % label)
78
79 try:
80 return float(match.group(1))
81 except ValueError:
82 raise FailedToParseCoverageHtml('%s is not a float.' % match.group(1))
83
84
85def _report_coverage_to_dashboard(dashboard, now, line_coverage,
86 function_coverage):
87 parameters = {'date': '%d' % now,
88 'line_coverage': '%f' % line_coverage,
89 'function_coverage': '%f' % function_coverage
90 }
91
phoglund@webrtc.org86ce46d2012-02-06 10:55:12 +000092 dashboard.send_post_request(constants.ADD_COVERAGE_DATA_URL, parameters)
phoglund@webrtc.orgd4f0a0e2012-02-01 10:59:23 +000093
94
95def _main():
96 dashboard = dashboard_connection.DashboardConnection(constants.CONSUMER_KEY)
97 dashboard.read_required_files(constants.CONSUMER_SECRET_FILE,
98 constants.ACCESS_TOKEN_FILE)
99
100 coverage_www_dir = os.path.join('/home', constants.BUILD_BOT_USER, 'www')
101
102 www_dir_contents = os.listdir(coverage_www_dir)
103 latest_build_directory = _find_latest_32bit_debug_build(www_dir_contents,
104 coverage_www_dir)
105
106 index_html_path = os.path.join(coverage_www_dir, latest_build_directory,
107 'index.html')
108 index_html_file = open(index_html_path)
109 whole_file = index_html_file.read()
110
111 line_coverage = _grab_coverage_percentage('Lines:', whole_file)
112 function_coverage = _grab_coverage_percentage('Functions:', whole_file)
113 now = int(time.time())
114
115 _report_coverage_to_dashboard(dashboard, now, line_coverage,
116 function_coverage)
117
118
119if __name__ == '__main__':
120 _main()
121