#!/usr/bin/env python

from __future__ import print_function
import sys
import os
import sqlite3

try:
    os.remove('yalcov.db')
except OSError:
    pass

conn = sqlite3.connect('yalcov.db')


class yalcov:
    def __init__(self, logfile):
        self.logfn = logfile
        logf = open(self.logfn, "r")
        self.parse_log(logf)
        self.report()

    def parse_log(self, logf):
        c = conn.cursor()

        # Create table
        c.execute('''CREATE TABLE cov
                       (filepath text, line int, hitcnt int)''')

        for line in logf.readlines():
            lineno = int(line.split(':')[1])
            path = line.split(':')[0]

            c.execute("SELECT hitcnt FROM cov WHERE filepath = '%s' AND line = '%d'" % (path, lineno))
            row = c.fetchone()
            if row is None:
                # Insert a row of data
                c.execute("INSERT INTO cov VALUES (?, ?, 1)", (path, lineno))
            else:
                c.execute("UPDATE cov SET hitcnt = hitcnt + 1 WHERE filepath = '%s' AND line = '%d'" % (path, lineno))

            # Save (commit) the changes
            conn.commit()

        # We can also close the connection if we are done with it.
        # Just be sure any changes have been committed or they will be lost.
        # conn.close()

    def report(self):
        c = conn.cursor()

        c.execute('''SELECT filepath FROM cov''')
        rows = c.fetchall()
        files = []
        for row in rows:
            files.append(row[0])

        for f in files:
            c.execute("SELECT * FROM cov WHERE filepath = '%s' ORDER BY line" % f)
            filecov = c.fetchall()

            linecov = {}
            for row in filecov:
                linecov[row[1]] = row[2]

            lineno = 1
            srcfile = open(f, "r")
            for line in srcfile:
                hitcnt = 0
                if lineno in linecov:
                    hitcnt = linecov[lineno]
                print('%4d|%4d|%s' % (lineno, hitcnt, line), end='')
                lineno = lineno + 1

            srcfile.close()


def main(argv):
    yalcov(argv[0])

if __name__ == '__main__':
    main(sys.argv[1:])

@@ -90,16 +90,51 @@
 sqlite3%0A
+from optparse import OptionParser%0A%0A
 %0Atry:%0A  
@@ -232,24 +232,46 @@
 ass yalcov:%0A
+    mask_cnt = False%0A%0A
     def __in
@@ -288,16 +288,20 @@
  logfile
+, mc
 ):%0A     
@@ -324,16 +324,43 @@
 logfile%0A
+        self.mask_cnt = mc%0A
         
@@ -2047,16 +2047,89 @@
 lineno%5D%0A
+                    if self.mask_cnt:%0A                        hitcnt = 1%0A
         
@@ -2280,22 +2280,323 @@
     
-yalcov(argv%5B0%5D
+parser = OptionParser()%0A    parser.add_option(%22-m%22, %22--mask-cnt%22, action=%22store_true%22, dest=%22mask_cnt%22, default=False,%0A                                        help=%22ignore hit count in report, set hit count to 1 for any value %3E= 1%22)%0A    (options, args) = parser.parse_args(argv)%0A    yalcov(args%5B0%5D, options.mask_cnt
 )%0A%0Ai

add option --mask-cnt to round any hitcnt >= 1 to 1