Scyther.py: Improving Scyther python interface by giving options and filenames when reporting an error.

This commit is contained in:
Cas Cremers 2010-11-09 11:09:31 +01:00
parent fac14fc950
commit cbb66ea794
2 changed files with 51 additions and 3 deletions

View File

@ -36,11 +36,22 @@ class ScytherError(Error):
backend backend
""" """
def __init__(self, errorlist): def __init__(self, errorlist,filenames=None,options=None):
self.errorlist = errorlist self.errorlist = errorlist
self.filenames = filenames
self.options = options
def __str__(self): def __str__(self):
s = "Scyther backend reported the following errors:\n" s = "Scyther backend reported errors"
if len(self.filenames) == 0:
s = s + " for unknown files."
if len(self.filenames) == 1:
s = s + " for file %s" % (self.filenames)
if len(self.filenames) > 1:
s = s + " for files %s" % (self.filenames)
s = s + "\n"
s = s + "Options: '%s'\n\n" % (self.options)
S = s + "Error details:\n"
s = s + "\n".join(self.errorlist) s = s + "\n".join(self.errorlist)
return s return s

View File

@ -163,6 +163,7 @@ class Scyther(object):
self.program = getScytherBackend() self.program = getScytherBackend()
self.spdl = None self.spdl = None
self.inputfile = None self.inputfile = None
self.filenames = []
self.options = "" self.options = ""
self.claims = None self.claims = None
self.errors = None self.errors = None
@ -178,9 +179,11 @@ class Scyther(object):
def setInput(self,spdl): def setInput(self,spdl):
self.spdl = spdl self.spdl = spdl
self.inputfile = None self.inputfile = None
self.guessFileNames()
def setFile(self,filename): def setFile(self,filename):
self.inputfile = filename self.inputfile = filename
self.filenames = [self.inputfile]
self.spdl = "" self.spdl = ""
fp = open(filename,"r") fp = open(filename,"r")
for l in fp.readlines(): for l in fp.readlines():
@ -195,6 +198,37 @@ class Scyther(object):
for l in fp.readlines(): for l in fp.readlines():
self.spdl += l self.spdl += l
fp.close() fp.close()
self.guessFileNames()
def guessFileNames(self,spdl=None):
"""
Try to extract filenames (well, actually, protocol names) sloppily from some spdl script.
There are two modes:
[init] : If the spdl parameter is empty or None, we reset the filenames and extract from self.spdl
[add] : If the spdl parameter is non-empty, add the extracted filenames to an existing list
"""
if (spdl == None) or (len(spdl) == 0):
spdl = self.spdl
self.filenames = []
for sl in spdl.splitlines():
l = sl.strip()
prefix = "protocol "
postfix = "("
x = l.find(prefix)
if x >= 0:
# The prefix occurs
y = l.find(postfix,x+len(prefix))
if y >= 0:
gn = l[x+len(prefix):y]
# check for helper protocols
if not gn.startswith("@"):
if gn not in self.filenames:
self.filenames.append(gn)
def addArglist(self,arglist): def addArglist(self,arglist):
for arg in arglist: for arg in arglist:
@ -221,6 +255,9 @@ class Scyther(object):
# Scyther hickups on completely empty input # Scyther hickups on completely empty input
spdl = "\n" spdl = "\n"
# Extract filenames for error reporting later
self.guessFileNames(spdl=spdl)
# Generate temporary files for the output. # Generate temporary files for the output.
# Requires Python 2.3 though. # Requires Python 2.3 though.
(fde,fne) = tempfile.mkstemp() # errors (fde,fne) = tempfile.mkstemp() # errors
@ -307,7 +344,7 @@ class Scyther(object):
self.errorcount = len(self.errors) self.errorcount = len(self.errors)
if self.errorcount > 0: if self.errorcount > 0:
raise Error.ScytherError(self.errors) raise Error.ScytherError(self.errors,filenames=self.filenames,options=self.options)
# process output # process output
self.output = output self.output = output