Skip to content

Instantly share code, notes, and snippets.

@dnozay
Created December 28, 2011 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnozay/1530119 to your computer and use it in GitHub Desktop.
Save dnozay/1530119 to your computer and use it in GitHub Desktop.
merge LCOV file counters
#!/usr/bin/python
# merge LCOV file counters
import sys
filedict = {}
currentitem = {}
currentfile = ''
for line in sys.stdin:
subline = line[3:][:-1]
if line.startswith('SF:'):
executed = 0
try:
currentitem = filedict[subline]
except KeyError:
currentitem = {}
currentitem['FN'] = {}
currentitem['DA'] = {}
filedict[subline] = currentitem
currentfile = subline
elif line.startswith('FN:'):
currentitem['FN'][subline] = 1
elif line.startswith('DA:'):
elems = subline.split(',')
try:
try:
currentitem['DA'][elems[0]] += int(elems[1])
except KeyError:
currentitem['DA'][elems[0]] = int(elems[1])
except:
continue
if currentitem['DA'][elems[0]]:
executed += 1
elif line.startswith('LF:'):
currentitem['LF'] = int(subline)
elif line.startswith('LH:'):
try:
currentitem['LH'] = max(currentitem['LH'],int(subline))
except KeyError:
currentitem['LH'] = int(subline)
except ValueError:
currentitem['LH'] = executed
elif line.startswith('end_of_record'):
# sys.stderr.write('updated ' + currentfile + '\n')
pass
for (sourcefile,item) in filedict.items():
print 'SF:'+sourcefile
for function in item['FN'].keys():
print 'FN:'+function
for (linenum, cov) in item['DA'].items():
print 'DA:'+linenum+','+str(cov)
print 'LF:'+str(item['LF'])
print 'LH:'+str(item['LH'])
print 'end_of_record'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment