def parse(self, url): self.linenum = 0 self.stacktrace = StackTrace() self.errors = 0 self.warnings = 0 data = urllib.urlopen(url) try: self.state = State.findGoroutine for line in data: self.linenum += 1 self.processLine(line.strip()) if self.state != State.findGoroutine: self.finishGoroutine(True) finally: self.parsed = True
class Parser: def __init__(self): self.parsed = False self.filepath = None self.errors = 0 self.warnings = 0 def parse(self, url): self.linenum = 0 self.stacktrace = StackTrace() self.errors = 0 self.warnings = 0 data = urllib.urlopen(url) try: self.state = State.findGoroutine for line in data: self.linenum += 1 self.processLine(line.strip()) if self.state != State.findGoroutine: self.finishGoroutine(True) finally: self.parsed = True def processLine(self, line): if self.state == State.findGoroutine: if line.startswith('goroutine'): self.processGoroutineLine(line) # Otherwise, still searching... elif self.state == State.getFunction: self.processFunctionLine(line) elif self.state == State.getFile: self.processFileLine(line) elif self.state == State.fileEnd: # Blank line indicates end of this goroutine if not line: self.finishGoroutine(True) # Non-blank line starting with 'goroutine' indicates start of next goroutine elif line.startswith('goroutine'): self.finishGoroutine(True) self.processGoroutineLine(line) # Other non-blank line should be another stack frame else: self.state == State.getFunction self.processFunctionLine(line) def processGoroutineLine(self, line): try: self.currentGoroutine = Goroutine() warnings = self.currentGoroutine.parseLine(line) self.reportWarnings(warnings, line) self.state = State.getFunction except Exception as exc: self.reportError(str(exc), line) # Skip to next goroutine self.finishGoroutine(False) def processFunctionLine(self, line): try: self.currentStackFrame = StackFrame() warnings = self.currentStackFrame.parseFunctionLine(line) self.reportWarnings(warnings, line) self.state = State.getFile except Exception as exc: self.reportError(str(exc), line) # Skip to next goroutine self.finishStackFrame(False) self.finishGoroutine(True) def processFileLine(self, line): if self.currentStackFrame is None: raise InternalError('No StackFrame instance available!') try: warnings = self.currentStackFrame.parseFileLine(line) self.reportWarnings(warnings, line) self.finishStackFrame(True) # Sets self.state except Exception as exc: self.reportError(str(exc), line) # Skip to next goroutine self.finishStackFrame(False) self.finishGoroutine(True) def finishGoroutine(self, keepIt): if keepIt and self.currentGoroutine is not None: self.stacktrace.addGoroutine(self.currentGoroutine) self.currentGoroutine = None self.state = State.findGoroutine def finishStackFrame(self, keepIt): if keepIt and self.currentStackFrame is not None: self.currentGoroutine.addFrame(self.currentStackFrame) self.currentStackFrame = None self.state = State.fileEnd def addLineNumber(self, msg): return 'Line {0}: {1}'.format(self.linenum, msg) def reportWarnings(self, warnings, line): if len(warnings) > 0: for warning in warnings: self.reportWarning(warning) print(' {0}'.format(self.addLineNumber(line))) def reportWarning(self, msg): self.warnings += 1 print('WARNING: {0}'.format(self.addLineNumber(msg)), file=sys.stderr) def reportError(self, msg, line): self.errors += 1 print('ERROR : {0}'.format(self.addLineNumber(msg)), file=sys.stderr) print(' : {0}'.format(self.addLineNumber(line)))
def test_fromFile7(self): stg = StackTrace.from_file("traces/jira-7.txt") self.assertEqual( stg.signature, "CE4rOkG7fOBL7JF//MvLYOqdncMhlTEaFCvLYOqdncMhlTEaFtdGWakIJmsIdGWR")
def test_simple(self): stg = StackTrace("hello good buddy") self.assertEqual(stg.signature, 'iKFCP')
def test_fromFile6(self): stg = StackTrace.from_file("traces/jira-6.txt") self.assertEqual(stg.signature, "PzUoD9HFYp/NELHcfeHq9HMrYpcGobGeksIXF1lwAvCZ0r")
def test_fromFile5(self): stg = StackTrace.from_file("traces/jira-5.txt") self.assertEqual( stg.signature, "iXwg242Q2t2CCt7qqDqy4tBBavTtY202gwoiOw6G5ED3XXOJHluRuiw79qOA")
def test_fromFile4(self): stg = StackTrace.from_file("traces/jira-4.txt") self.assertEqual(stg.signature, "YXB0tKVmLXgM/HMTRYyME1ROPpMTRbMCu")
def test_fromFile3(self): stg = StackTrace.from_file("traces/jira-3.txt") self.assertEqual(stg.signature, "YX7/F0hrIRHF8ILKLC5CdLfwHnKvL3oIJG6WMY2GEGYVNGEEu")
'''import_traces.py''' from glob import glob import sys from stacktrace import StackTrace from stacktrace_gateway import STGateway FILES = glob('traces/*.txt') if len(FILES) == 0: print "Nothing matched traces/*.txt" sys.exit(0) ST = STGateway() ST.delete() ST.create() for filename in FILES: issue = filename[7:-4] stack = open(filename, "r").read() trace = StackTrace(stack) print "sig is {0} chars".format(len(trace.signature)) ST.insert(issue, stack, trace.signature)