Skip to content

Instantly share code, notes, and snippets.

@lukemarsden
Created October 11, 2011 13:40
Show Gist options
  • Save lukemarsden/1278103 to your computer and use it in GitHub Desktop.
Save lukemarsden/1278103 to your computer and use it in GitHub Desktop.
gen-btt-stats.py
#!/usr/bin/env python
import re, sys
try:
filename, countLimit = sys.argv
except:
print "Usage: %s FILE" % (sys.argv[0],)
def main():
"""
Count how frequently entire tracebacks (defined in terms of their final 5
lines) occur.
"""
log = open("workonthis", "r")
lines = log.readlines()
#stats = {}
tracebacks = {}
preg = re.compile('^ File')
#linecounter = 0
traceback = []
for line in lines:
if not line.startswith(" "):
# This is a timestamp, we've hit the next one, so record the
# last five lines of the traceback.
tbText = "\n".join(traceback[-5:])
tracebacks[tbText] = tracebacks.get(tbText, 0) + 1
#linecounter = 0
else:
pass
#linecounter += 1
if not preg.match(line):
continue
traceback.append(line.strip())
#if linecounter < int(limit):
# continue
#line = line.strip()
#stats[line] = stats.get(line, 0) + 1
sorted_stats = []
for line, count in tracebacks.iteritems():
sorted_stats.append((count, line))
sorted_stats.sort()
print "\n".join([" ==> %s\n%s\n" % (count, line) for (count, line) in sorted_stats[::-1][:int(countLimit)]])
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment