| 1 |
#!/usr/bin/env python |
|---|
| 2 |
# |
|---|
| 3 |
# Convert coverage output to HTML table. |
|---|
| 4 |
# |
|---|
| 5 |
# Copyright (c) 2006 Michal Kwiatkowski <ruby@joker.linuxstuff.pl> |
|---|
| 6 |
# |
|---|
| 7 |
# Redistribution and use in source and binary forms, with or without |
|---|
| 8 |
# modification, are permitted provided that the following conditions |
|---|
| 9 |
# are met: |
|---|
| 10 |
# |
|---|
| 11 |
# * Redistributions of source code must retain the above copyright |
|---|
| 12 |
# notice, this list of conditions and the following disclaimer. |
|---|
| 13 |
# |
|---|
| 14 |
# * Redistributions in binary form must reproduce the above copyright |
|---|
| 15 |
# notice, this list of conditions and the following disclaimer in the |
|---|
| 16 |
# documentation and/or other materials provided with the distribution. |
|---|
| 17 |
# |
|---|
| 18 |
# * Neither the name of the author nor the names of his contributors |
|---|
| 19 |
# may be used to endorse or promote products derived from this software |
|---|
| 20 |
# without specific prior written permission. |
|---|
| 21 |
# |
|---|
| 22 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 23 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 24 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 25 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 26 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 27 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|---|
| 28 |
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 29 |
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|---|
| 30 |
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 31 |
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 32 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 33 |
# |
|---|
| 34 |
|
|---|
| 35 |
import re |
|---|
| 36 |
import sys |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
def sort_by_cover(lines): |
|---|
| 40 |
def get_percent(obj): |
|---|
| 41 |
return int(obj[3][:-1]) |
|---|
| 42 |
|
|---|
| 43 |
def compare(x, y): |
|---|
| 44 |
return cmp(get_percent(x), get_percent(y)) |
|---|
| 45 |
|
|---|
| 46 |
lines.sort(compare) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
def make_row(line, header=False, emphasis=False): |
|---|
| 50 |
result = [] |
|---|
| 51 |
|
|---|
| 52 |
tag = 'td' |
|---|
| 53 |
if header: |
|---|
| 54 |
tag = 'th' |
|---|
| 55 |
|
|---|
| 56 |
result.append('<tr>\n') |
|---|
| 57 |
|
|---|
| 58 |
for field in line: |
|---|
| 59 |
if emphasis: |
|---|
| 60 |
result.append('<%(tag)s><strong>%(field)s</strong></%(tag)s>\n' % \ |
|---|
| 61 |
{'tag': tag, 'field': field}) |
|---|
| 62 |
else: |
|---|
| 63 |
result.append('<%(tag)s>%(field)s</%(tag)s>\n' % \ |
|---|
| 64 |
{'tag': tag, 'field': field}) |
|---|
| 65 |
|
|---|
| 66 |
result.append('</tr>\n') |
|---|
| 67 |
|
|---|
| 68 |
return ''.join(result) |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
def cover2html(text): |
|---|
| 72 |
text_lines = map(lambda x: re.split(r'\s+', x, 4), text.splitlines()) |
|---|
| 73 |
text_lines = filter(lambda x: x[0].strip('-'), text_lines) |
|---|
| 74 |
|
|---|
| 75 |
title_line = text_lines.pop(0) |
|---|
| 76 |
summary_line = text_lines.pop() |
|---|
| 77 |
|
|---|
| 78 |
sort_by_cover(text_lines) |
|---|
| 79 |
|
|---|
| 80 |
result = [] |
|---|
| 81 |
result.append('<table border="1">\n') |
|---|
| 82 |
result.append(make_row(title_line, header=True)) |
|---|
| 83 |
|
|---|
| 84 |
for line in text_lines: |
|---|
| 85 |
result.append(make_row(line)) |
|---|
| 86 |
|
|---|
| 87 |
result.append(make_row(summary_line, emphasis=True)) |
|---|
| 88 |
result.append('</table>\n') |
|---|
| 89 |
|
|---|
| 90 |
return ''.join(result) |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
if __name__ == '__main__': |
|---|
| 94 |
print cover2html(sys.stdin.read()) |
|---|