2006-08-02 13:59:57 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Scyther interface
|
|
|
|
#
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import externals """
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import StringIO
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import scyther components """
|
|
|
|
import XMLReader
|
|
|
|
from Misc import *
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class Scyther(object):
|
|
|
|
def __init__ ( self):
|
2006-08-04 23:00:22 +01:00
|
|
|
|
|
|
|
# Where is my executable?
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
""" Windows """
|
|
|
|
# TODO hardcoded for now, bad
|
|
|
|
scyther.program = "c:\\Scyther.exe"
|
|
|
|
if not os.path.isfile(scyther.program):
|
|
|
|
print "I can't find the Scyther executable %s" % (scyther.program)
|
|
|
|
else:
|
|
|
|
""" Non-windows """
|
|
|
|
self.program = "scyther"
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
self.options = ""
|
|
|
|
self.spdl = None
|
2006-08-02 14:44:45 +01:00
|
|
|
self.inputfile = None
|
2006-08-02 13:59:57 +01:00
|
|
|
self.claims = None
|
|
|
|
|
|
|
|
def setInput(self,spdl):
|
|
|
|
self.spdl = spdl
|
2006-08-02 14:44:45 +01:00
|
|
|
self.inputfile = None
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def setFile(self,filename):
|
2006-08-02 14:44:45 +01:00
|
|
|
self.inputfile = filename
|
2006-08-02 13:59:57 +01:00
|
|
|
self.spdl = ""
|
|
|
|
fp = open(filename,"r")
|
|
|
|
for l in fp.readlines():
|
|
|
|
self.spdl += l
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def verify(self):
|
|
|
|
|
2006-08-02 14:10:38 +01:00
|
|
|
# Run Scyther on temp file
|
|
|
|
self.cmd = "%s --dot-output --xml-output --plain %s" % (self.program,self.options)
|
|
|
|
|
|
|
|
# If we are on windows, we don't get stderr. Maybe we need a
|
|
|
|
# switch to enforce this.
|
|
|
|
if sys.platform.startswith('linux'):
|
|
|
|
cmdline = "%s 2>/dev/null" % (self.cmd)
|
|
|
|
else:
|
|
|
|
# Non-linux does not generate stderr anyway
|
|
|
|
cmdline = "%s" % (self.cmd)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-08-02 14:51:24 +01:00
|
|
|
pw,pr = os.popen2(cmdline)
|
2006-08-02 14:10:38 +01:00
|
|
|
if self.spdl:
|
2006-08-02 14:51:24 +01:00
|
|
|
pw.write(self.spdl)
|
|
|
|
pw.close()
|
|
|
|
xmlinput = pr.read()
|
|
|
|
pr.close()
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-08-02 14:10:38 +01:00
|
|
|
xmlfile = StringIO.StringIO(xmlinput)
|
|
|
|
reader = XMLReader.XMLReader()
|
|
|
|
self.claims = reader.readXML(xmlfile)
|
2006-08-02 13:59:57 +01:00
|
|
|
|
2006-08-02 14:10:38 +01:00
|
|
|
return self.claims
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.claims:
|
|
|
|
s = ""
|
|
|
|
for cl in self.claims:
|
|
|
|
s += str(cl) + "\n"
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
return "Scyther has not been run yet."
|
|
|
|
|
|
|
|
|
2006-08-02 14:44:45 +01:00
|
|
|
def basicTest():
|
2006-08-02 13:59:57 +01:00
|
|
|
# Some basic testing
|
2006-08-02 14:44:45 +01:00
|
|
|
#if sys.platform.startswith('win'):
|
|
|
|
# print "Dir test"
|
|
|
|
# p = os.popen("dir")
|
|
|
|
# print p.read()
|
|
|
|
# print p.close()
|
|
|
|
# confirm("See the dir?")
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
# Scyther
|
|
|
|
x = Scyther()
|
|
|
|
|
|
|
|
if sys.platform.startswith('win'):
|
2006-08-02 14:44:45 +01:00
|
|
|
x.program = "Scyther.exe"
|
2006-08-02 13:59:57 +01:00
|
|
|
if not os.path.isfile(x.program):
|
|
|
|
print "I can't find the Scyther executable %s" % (x.program)
|
2006-08-02 14:51:24 +01:00
|
|
|
pw,pr = os.popen2("%s --help" % x.program)
|
|
|
|
pw.close()
|
|
|
|
print pr.read()
|
|
|
|
confirm("Do you see the help?")
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
x.setFile("ns3.spdl")
|
|
|
|
x.verify()
|
2006-08-02 14:10:38 +01:00
|
|
|
print x
|
2006-08-02 14:44:45 +01:00
|
|
|
confirm("See the output?")
|
|
|
|
|
|
|
|
def simpleRun(args):
|
|
|
|
x = Scyther()
|
|
|
|
x.options = args
|
|
|
|
x.verify()
|
|
|
|
return x
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2006-08-02 14:10:38 +01:00
|
|
|
pars = sys.argv[1:]
|
|
|
|
if len(pars) == 0:
|
2006-08-02 14:44:45 +01:00
|
|
|
basicTest()
|
2006-08-02 14:10:38 +01:00
|
|
|
else:
|
2006-08-02 14:44:45 +01:00
|
|
|
print simpleRun(" ".join(pars))
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
|