- Some code refactoring.
This commit is contained in:
parent
7786e0d65f
commit
aac06d3c43
@ -35,6 +35,17 @@ class BinaryError(Error):
|
|||||||
return "Could not find Scyther executable at '%s'" % (self.file)
|
return "Could not find Scyther executable at '%s'" % (self.file)
|
||||||
|
|
||||||
|
|
||||||
|
class NoBinaryError(Error):
|
||||||
|
"""Raised when the Scyther executable is not defined.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
None.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Scyther class attribute 'program' was not defined."
|
||||||
|
|
||||||
|
|
||||||
class UnknownPlatformError(Error):
|
class UnknownPlatformError(Error):
|
||||||
"""Raised when the platform is not supported yet.
|
"""Raised when the platform is not supported yet.
|
||||||
|
|
||||||
@ -47,3 +58,6 @@ class UnknownPlatformError(Error):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "The %s platform is currently unsupported." % self.platform
|
return "The %s platform is currently unsupported." % self.platform
|
||||||
|
|
||||||
|
|
||||||
|
# vim: set ts=4 sw=4 et list lcs=tab\:>-:
|
||||||
|
@ -109,6 +109,7 @@ def getScytherBackend():
|
|||||||
program = os.path.join(getBinDir(),scythername)
|
program = os.path.join(getBinDir(),scythername)
|
||||||
return program
|
return program
|
||||||
|
|
||||||
|
|
||||||
#---------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
|
|
||||||
class Scyther(object):
|
class Scyther(object):
|
||||||
@ -149,21 +150,30 @@ class Scyther(object):
|
|||||||
self.spdl += l
|
self.spdl += l
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
def verify(self):
|
def doScytherCommand(self, spdl, args):
|
||||||
""" Should return a list of results """
|
"""
|
||||||
|
Run Scyther backend on the input
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
spdl -- string describing the spdl text
|
||||||
|
args -- arguments for the command-line
|
||||||
|
Returns:
|
||||||
|
(output,errors)
|
||||||
|
output -- string which is the real output
|
||||||
|
errors -- string which captures the errors
|
||||||
|
"""
|
||||||
|
|
||||||
if self.program == None:
|
if self.program == None:
|
||||||
return []
|
raise Error.NoBinaryError
|
||||||
|
|
||||||
# Generate a temproary file for the error output
|
# Generate a temproary file for the error output
|
||||||
errorfile = tempfile.NamedTemporaryFile()
|
errorfile = tempfile.NamedTemporaryFile()
|
||||||
|
|
||||||
# Generate command line for the Scyther process
|
# Generate command line for the Scyther process
|
||||||
self.cmd = "\"%s\"" % self.program
|
self.cmd = ""
|
||||||
if self.xml:
|
self.cmd += "\"%s\"" % self.program
|
||||||
self.cmd += " --dot-output --xml-output --plain"
|
|
||||||
self.cmd += " --append-errors=%s" % (errorfile.name)
|
self.cmd += " --append-errors=%s" % (errorfile.name)
|
||||||
self.cmd += " " + self.options
|
self.cmd += " %s %s" % (self.options, args)
|
||||||
|
|
||||||
# Start the process, push input, get output
|
# Start the process, push input, get output
|
||||||
(stdin,stdout) = os.popen2(self.cmd)
|
(stdin,stdout) = os.popen2(self.cmd)
|
||||||
@ -174,40 +184,55 @@ class Scyther(object):
|
|||||||
stdout.close()
|
stdout.close()
|
||||||
|
|
||||||
# get errors
|
# get errors
|
||||||
# filter out any non-errors (say maybe only claim etc) and count
|
|
||||||
# them.
|
|
||||||
self.errors = []
|
self.errors = []
|
||||||
errorfile.seek(0)
|
errorfile.seek(0)
|
||||||
for l in errorfile.readlines():
|
errors = errorfile.read()
|
||||||
if not l.startswith("claim\t"):
|
|
||||||
self.errors.append(l.strip())
|
|
||||||
|
|
||||||
self.errorcount = len(self.errors)
|
|
||||||
|
|
||||||
# close
|
|
||||||
errorfile.close()
|
errorfile.close()
|
||||||
|
|
||||||
|
return (output,errors)
|
||||||
|
|
||||||
|
def verify(self):
|
||||||
|
""" Should return a list of results """
|
||||||
|
|
||||||
|
# prepare arguments
|
||||||
|
args = ""
|
||||||
|
if self.xml:
|
||||||
|
args += " --dot-output --xml-output --plain"
|
||||||
|
args += " %s" % self.options
|
||||||
|
|
||||||
|
# execute
|
||||||
|
(output,errors) = self.doScytherCommand(self.spdl, args)
|
||||||
|
self.run = True
|
||||||
|
|
||||||
|
# process errors
|
||||||
|
self.errors = []
|
||||||
|
for l in errors.splitlines():
|
||||||
|
# filter out any non-errors (say maybe only claim etc) and count
|
||||||
|
# them.
|
||||||
|
if not l.startswith("claim\t"):
|
||||||
|
self.errors.append(l.strip())
|
||||||
|
self.errorcount = len(self.errors)
|
||||||
|
|
||||||
|
# process output
|
||||||
|
self.output = output
|
||||||
|
self.validxml = False
|
||||||
|
self.claims = []
|
||||||
if self.xml:
|
if self.xml:
|
||||||
self.validxml = False
|
|
||||||
if len(output) > 0:
|
if len(output) > 0:
|
||||||
if output.startswith("<scyther>"):
|
if output.startswith("<scyther>"):
|
||||||
|
|
||||||
|
# whoohee, xml
|
||||||
self.validxml = True
|
self.validxml = True
|
||||||
|
|
||||||
if self.validxml:
|
xmlfile = StringIO.StringIO(output)
|
||||||
xmlfile = StringIO.StringIO(output)
|
reader = XMLReader.XMLReader()
|
||||||
reader = XMLReader.XMLReader()
|
self.claims = reader.readXML(xmlfile)
|
||||||
self.claims = reader.readXML(xmlfile)
|
|
||||||
else:
|
|
||||||
# no xml output... store whatever comes out
|
|
||||||
self.claims = []
|
|
||||||
self.output = output
|
|
||||||
result = self.claims
|
|
||||||
else:
|
|
||||||
self.output = output
|
|
||||||
result = self.output
|
|
||||||
|
|
||||||
self.run = True
|
# Determine what should be the result
|
||||||
return result
|
if self.xml:
|
||||||
|
return self.claims
|
||||||
|
else:
|
||||||
|
return self.output
|
||||||
|
|
||||||
def getClaim(self,claimid):
|
def getClaim(self,claimid):
|
||||||
if self.claims:
|
if self.claims:
|
||||||
@ -231,4 +256,4 @@ class Scyther(object):
|
|||||||
else:
|
else:
|
||||||
return "Scyther has not been run yet."
|
return "Scyther has not been run yet."
|
||||||
|
|
||||||
|
# vim: set ts=4 sw=4 et list lcs=tab\:>-:
|
||||||
|
Loading…
Reference in New Issue
Block a user