From 166f618cb9c3d3555498b8754e2d9e7343abcbff Mon Sep 17 00:00:00 2001 From: ccremers Date: Tue, 8 Aug 2006 15:54:00 +0000 Subject: [PATCH] - Refactoring stuff into a package. --- gui/Attackimage.py | 41 ------------------------------- gui/{ => Gui}/Attackwindow.py | 0 gui/{ => Gui}/Icon.py | 0 gui/{ => Gui}/Mainwindow.py | 0 gui/{ => Gui}/Misc.py | 0 gui/{ => Gui}/Preference.py | 0 gui/{ => Gui}/Scytherthread.py | 6 ++--- gui/{ => Gui}/Tempfile.py | 0 gui/{ => Scyther}/Attack.py | 0 gui/{ => Scyther}/Claim.py | 0 gui/Scyther/Misc.py | 45 ++++++++++++++++++++++++++++++++++ gui/{ => Scyther}/Scyther.py | 44 +++++++++++++++++++++------------ gui/{ => Scyther}/Term.py | 0 gui/{ => Scyther}/Trace.py | 0 gui/{ => Scyther}/XMLReader.py | 0 gui/mpa.py | 2 +- gui/scyther-gui.py | 6 ++--- 17 files changed, 81 insertions(+), 63 deletions(-) delete mode 100644 gui/Attackimage.py rename gui/{ => Gui}/Attackwindow.py (100%) rename gui/{ => Gui}/Icon.py (100%) rename gui/{ => Gui}/Mainwindow.py (100%) rename gui/{ => Gui}/Misc.py (100%) rename gui/{ => Gui}/Preference.py (100%) rename gui/{ => Gui}/Scytherthread.py (99%) rename gui/{ => Gui}/Tempfile.py (100%) rename gui/{ => Scyther}/Attack.py (100%) rename gui/{ => Scyther}/Claim.py (100%) create mode 100644 gui/Scyther/Misc.py rename gui/{ => Scyther}/Scyther.py (78%) rename gui/{ => Scyther}/Term.py (100%) rename gui/{ => Scyther}/Trace.py (100%) rename gui/{ => Scyther}/XMLReader.py (100%) diff --git a/gui/Attackimage.py b/gui/Attackimage.py deleted file mode 100644 index 950ffb3..0000000 --- a/gui/Attackimage.py +++ /dev/null @@ -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 - -#--------------------------------------------------------------------------- - diff --git a/gui/Attackwindow.py b/gui/Gui/Attackwindow.py similarity index 100% rename from gui/Attackwindow.py rename to gui/Gui/Attackwindow.py diff --git a/gui/Icon.py b/gui/Gui/Icon.py similarity index 100% rename from gui/Icon.py rename to gui/Gui/Icon.py diff --git a/gui/Mainwindow.py b/gui/Gui/Mainwindow.py similarity index 100% rename from gui/Mainwindow.py rename to gui/Gui/Mainwindow.py diff --git a/gui/Misc.py b/gui/Gui/Misc.py similarity index 100% rename from gui/Misc.py rename to gui/Gui/Misc.py diff --git a/gui/Preference.py b/gui/Gui/Preference.py similarity index 100% rename from gui/Preference.py rename to gui/Gui/Preference.py diff --git a/gui/Scytherthread.py b/gui/Gui/Scytherthread.py similarity index 99% rename from gui/Scytherthread.py rename to gui/Gui/Scytherthread.py index 28301dd..5ea306a 100644 --- a/gui/Scytherthread.py +++ b/gui/Gui/Scytherthread.py @@ -20,13 +20,13 @@ except ImportError: #--------------------------------------------------------------------------- """ 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 Tempfile -import Claim import Preference -import Scyther import Attackwindow import Icon diff --git a/gui/Tempfile.py b/gui/Gui/Tempfile.py similarity index 100% rename from gui/Tempfile.py rename to gui/Gui/Tempfile.py diff --git a/gui/Attack.py b/gui/Scyther/Attack.py similarity index 100% rename from gui/Attack.py rename to gui/Scyther/Attack.py diff --git a/gui/Claim.py b/gui/Scyther/Claim.py similarity index 100% rename from gui/Claim.py rename to gui/Scyther/Claim.py diff --git a/gui/Scyther/Misc.py b/gui/Scyther/Misc.py new file mode 100644 index 0000000..4346491 --- /dev/null +++ b/gui/Scyther/Misc.py @@ -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) + diff --git a/gui/Scyther.py b/gui/Scyther/Scyther.py similarity index 78% rename from gui/Scyther.py rename to gui/Scyther/Scyther.py index 58b4013..d0a9294 100755 --- a/gui/Scyther.py +++ b/gui/Scyther/Scyther.py @@ -34,14 +34,17 @@ class Scyther(object): """ Non-windows """ self.program = os.path.join("bin","scyther") - # defaults - self.options = "" + # Init self.spdl = None self.inputfile = None self.claims = None self.errors = None self.errorcount = 0 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): self.spdl = spdl @@ -67,7 +70,10 @@ class Scyther(object): def verify(self): # 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) if self.spdl: @@ -80,7 +86,7 @@ class Scyther(object): # TODO this is annoying: we would like to determine progress # from the error output (maybe this can also be done by flushing # the XML at certain points...) - xmlinput = stdout.read() + output = stdout.read() errlines = stderr.readlines() # filter out any non-errors (say maybe only claim etc) and count @@ -96,16 +102,21 @@ class Scyther(object): stdout.close() stderr.close() - if len(xmlinput) > 0: - xmlfile = StringIO.StringIO(xmlinput) - reader = XMLReader.XMLReader() - self.claims = reader.readXML(xmlfile) + if self.xml: + if len(output) > 0: + xmlfile = StringIO.StringIO(output) + reader = XMLReader.XMLReader() + self.claims = reader.readXML(xmlfile) + else: + # no output... + self.claims = [] + result = self.claims else: - # no output... - self.claims = [] + self.output = output + result = self.output self.run = True - return self.claims + return result def getClaim(self,claimid): if self.claims: @@ -119,10 +130,13 @@ class Scyther(object): if self.errorcount > 0: return "%i errors:\n%s" % (self.errorcount, "\n".join(self.errors)) else: - s = "Claim results:\n" - for cl in self.claims: - s += str(cl) + "\n" - return s + if self.xml: + s = "Claim results:\n" + for cl in self.claims: + s += str(cl) + "\n" + return s + else: + return self.output else: return "Scyther has not been run yet." diff --git a/gui/Term.py b/gui/Scyther/Term.py similarity index 100% rename from gui/Term.py rename to gui/Scyther/Term.py diff --git a/gui/Trace.py b/gui/Scyther/Trace.py similarity index 100% rename from gui/Trace.py rename to gui/Scyther/Trace.py diff --git a/gui/XMLReader.py b/gui/Scyther/XMLReader.py similarity index 100% rename from gui/XMLReader.py rename to gui/Scyther/XMLReader.py diff --git a/gui/mpa.py b/gui/mpa.py index 83a4416..7faf026 100755 --- a/gui/mpa.py +++ b/gui/mpa.py @@ -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): """ diff --git a/gui/scyther-gui.py b/gui/scyther-gui.py index 2b6b1a5..2df64b3 100755 --- a/gui/scyther-gui.py +++ b/gui/scyther-gui.py @@ -10,9 +10,9 @@ from optparse import OptionParser, SUPPRESS_HELP #--------------------------------------------------------------------------- """ Import scyther-gui components """ -import Preference -import Mainwindow -import Misc +import Gui.Preference as Preference +import Gui.Mainwindow as Mainwindow +import Gui.Misc as Misc #---------------------------------------------------------------------------