- Refactoring stuff into a package.

This commit is contained in:
ccremers 2006-08-08 15:54:00 +00:00
parent 0b21755928
commit 166f618cb9
17 changed files with 81 additions and 63 deletions

View File

@ -1,41 +0,0 @@
#!/usr/bin/python
#---------------------------------------------------------------------------
""" Import externals """
import os
#---------------------------------------------------------------------------
""" Import scyther-gui components """
import Tempfile
#---------------------------------------------------------------------------
class AttackImage:
def __init__(self,dotdata):
self.dotdata = dotdata
self.png = ""
self.MakeImage()
def MakeImage(self):
""" Sets png """
(fd,fpname) = Tempfile.tempcleaned(".dot")
fp = os.fdopen(fd, "w")
fp.write(self.dotdata)
fp.close()
(fd2,fpname2) = Tempfile.tempcleaned(".png")
os.system("dot %s -Tpng >%s" % (fpname, fpname2))
self.png = fpname2
Tempfile.tempcleanearly((fd,fpname))
def GetImage(self):
return self.png
#---------------------------------------------------------------------------

View File

@ -20,13 +20,13 @@ except ImportError:
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
""" Import scyther components """ """ Import scyther components """
import XMLReader import Scyther.XMLReader as XMLReader
import Scyther.Claim as Claim
import Scyther.Scyther as Scyther
""" Import scyther-gui components """ """ Import scyther-gui components """
import Tempfile import Tempfile
import Claim
import Preference import Preference
import Scyther
import Attackwindow import Attackwindow
import Icon import Icon

45
gui/Scyther/Misc.py Normal file
View File

@ -0,0 +1,45 @@
#
# Misc.py
# Various helper functions
#---------------------------------------------------------------------------
""" Import externals """
import os.path
#---------------------------------------------------------------------------
def confirm(question):
answer = ''
while answer not in ('y','n'):
print question,
answer = raw_input().lower()
return answer == 'y'
def exists(func,list):
return len(filter(func,list)) > 0
def forall(func,list):
return len(filter(func,list)) == len(list)
def uniq(li):
result = []
for elem in li:
if (not elem in result):
result.append(elem)
return result
# Return a sorted copy of a list
def sorted(li):
result = li[:]
result.sort()
return result
# path
def mypath(file):
""" Construct a file path relative to the scyther-gui main directory
"""
basedir = os.path.dirname(__file__)
return os.path.join(basedir,file)

View File

@ -34,14 +34,17 @@ class Scyther(object):
""" Non-windows """ """ Non-windows """
self.program = os.path.join("bin","scyther") self.program = os.path.join("bin","scyther")
# defaults # Init
self.options = ""
self.spdl = None self.spdl = None
self.inputfile = None self.inputfile = None
self.claims = None self.claims = None
self.errors = None self.errors = None
self.errorcount = 0 self.errorcount = 0
self.run = False self.run = False
self.output = None
# defaults
self.xml = True # this results in a claim end, otherwise we simply get the output
def setInput(self,spdl): def setInput(self,spdl):
self.spdl = spdl self.spdl = spdl
@ -67,7 +70,10 @@ class Scyther(object):
def verify(self): def verify(self):
# Run Scyther on temp file # Run Scyther on temp file
self.cmd = "%s --dot-output --xml-output --plain %s" % (self.program,self.options) self.cmd = self.program
if self.xml:
self.cmd += " --dot-output --xml-output --plain"
self.cmd += " " + self.options
(stdin,stdout,stderr) = os.popen3(self.cmd) (stdin,stdout,stderr) = os.popen3(self.cmd)
if self.spdl: if self.spdl:
@ -80,7 +86,7 @@ class Scyther(object):
# TODO this is annoying: we would like to determine progress # TODO this is annoying: we would like to determine progress
# from the error output (maybe this can also be done by flushing # from the error output (maybe this can also be done by flushing
# the XML at certain points...) # the XML at certain points...)
xmlinput = stdout.read() output = stdout.read()
errlines = stderr.readlines() errlines = stderr.readlines()
# filter out any non-errors (say maybe only claim etc) and count # filter out any non-errors (say maybe only claim etc) and count
@ -96,16 +102,21 @@ class Scyther(object):
stdout.close() stdout.close()
stderr.close() stderr.close()
if len(xmlinput) > 0: if self.xml:
xmlfile = StringIO.StringIO(xmlinput) if len(output) > 0:
reader = XMLReader.XMLReader() xmlfile = StringIO.StringIO(output)
self.claims = reader.readXML(xmlfile) reader = XMLReader.XMLReader()
self.claims = reader.readXML(xmlfile)
else:
# no output...
self.claims = []
result = self.claims
else: else:
# no output... self.output = output
self.claims = [] result = self.output
self.run = True self.run = True
return self.claims return result
def getClaim(self,claimid): def getClaim(self,claimid):
if self.claims: if self.claims:
@ -119,10 +130,13 @@ class Scyther(object):
if self.errorcount > 0: if self.errorcount > 0:
return "%i errors:\n%s" % (self.errorcount, "\n".join(self.errors)) return "%i errors:\n%s" % (self.errorcount, "\n".join(self.errors))
else: else:
s = "Claim results:\n" if self.xml:
for cl in self.claims: s = "Claim results:\n"
s += str(cl) + "\n" for cl in self.claims:
return s s += str(cl) + "\n"
return s
else:
return self.output
else: else:
return "Scyther has not been run yet." return "Scyther has not been run yet."

View File

@ -6,7 +6,7 @@ Test script to execute multi-protocol attacks on some test set.
""" """
import Scyther import Scyther.Scyther as Scyther
def MyScyther(protocollist,filter=None): def MyScyther(protocollist,filter=None):
""" """

View File

@ -10,9 +10,9 @@ from optparse import OptionParser, SUPPRESS_HELP
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
""" Import scyther-gui components """ """ Import scyther-gui components """
import Preference import Gui.Preference as Preference
import Mainwindow import Gui.Mainwindow as Mainwindow
import Misc import Gui.Misc as Misc
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------