2007-06-11 13:09:24 +01:00
|
|
|
"""
|
|
|
|
Scyther : An automatic verifier for security protocols.
|
|
|
|
Copyright (C) 2007 Cas Cremers
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
"""
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
#
|
|
|
|
# Misc.py
|
|
|
|
# Various helper functions
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
""" Import externals """
|
2007-05-19 14:32:58 +01:00
|
|
|
import sys
|
2006-08-02 13:59:57 +01:00
|
|
|
import os.path
|
2007-05-19 14:32:58 +01:00
|
|
|
try:
|
2010-05-15 22:26:44 +01:00
|
|
|
from subprocess import Popen,PIPE
|
2007-05-19 14:32:58 +01:00
|
|
|
except:
|
2010-05-15 22:26:44 +01:00
|
|
|
panic("""
|
|
|
|
Cannot import 'subprocess.Popen' module.
|
|
|
|
|
|
|
|
You need at least Python 2.4 to use this program.
|
|
|
|
""")
|
2006-08-02 13:59:57 +01:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2010-11-09 11:05:18 +00:00
|
|
|
# ensurePath: does what os.makedirs should do.
|
|
|
|
def ensurePath(pt):
|
|
|
|
"""
|
|
|
|
Make sure the path exists: if not, create the directories one by one
|
|
|
|
|
|
|
|
By example:
|
|
|
|
|
|
|
|
Call with "dog/cat/bone" ensures that afterwards, this subdirectory structure (dog/cat/bone) exists, with 'bone' a directory.
|
|
|
|
It ensures this by doing the procedure for "dog", then "dog/cat", etc...
|
|
|
|
"""
|
|
|
|
|
|
|
|
ptn = os.path.normpath(pt)
|
|
|
|
|
|
|
|
# First we see what needs to exist overall
|
|
|
|
todo = [ptn]
|
|
|
|
tail = "x"
|
|
|
|
while len(tail) > 0:
|
|
|
|
(head,tail) = os.path.split(ptn)
|
|
|
|
if len(head) > 0:
|
|
|
|
todo.append(head)
|
|
|
|
ptn = head
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Reverse list: path prefixes first
|
|
|
|
todo.reverse()
|
|
|
|
|
|
|
|
# Create bottom-up
|
|
|
|
for pt in todo:
|
|
|
|
if not os.path.isdir(pt):
|
|
|
|
# Note that os.path.exists(pt) may still hold. In this case the next command will cause an error.
|
|
|
|
os.mkdir(pt)
|
|
|
|
|
|
|
|
|
2006-08-02 13:59:57 +01:00
|
|
|
# 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)
|
|
|
|
|
2010-05-15 22:26:44 +01:00
|
|
|
def getShell():
|
|
|
|
"""
|
|
|
|
Determine if we want a shell for Popen
|
|
|
|
"""
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
shell=False
|
|
|
|
else:
|
|
|
|
# Needed to handle the string input correctly (as opposed to a sequence where the first element is the executable)
|
|
|
|
# This is not needed on Windows, where it has a different effect altogether.
|
|
|
|
# See http://docs.python.org/library/subprocess.html?highlight=subprocess#module-subprocess
|
|
|
|
shell=True
|
|
|
|
return shell
|
|
|
|
|
|
|
|
def safeCommandOutput(cmd):
|
|
|
|
""" Execute a command and return (sts,sout,serr).
|
|
|
|
Meant for short outputs, as output is stored in memory and
|
|
|
|
not written to a file.
|
|
|
|
"""
|
2010-05-15 23:33:53 +01:00
|
|
|
|
|
|
|
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
|
2010-05-15 22:26:44 +01:00
|
|
|
(sout,serr) = p.communicate()
|
2010-05-15 23:33:53 +01:00
|
|
|
|
2010-05-15 22:26:44 +01:00
|
|
|
return (p.returncode,sout,serr)
|
|
|
|
|
2007-05-19 14:32:58 +01:00
|
|
|
def safeCommand(cmd):
|
|
|
|
""" Execute a command with some arguments. Safe cross-platform
|
|
|
|
version, I hope. """
|
|
|
|
|
2010-05-15 23:33:53 +01:00
|
|
|
p = Popen(cmd, shell=True)
|
2010-05-15 22:26:44 +01:00
|
|
|
sts = p.wait()
|
2007-05-19 14:32:58 +01:00
|
|
|
|
|
|
|
return sts
|
|
|
|
|
2010-05-15 22:26:44 +01:00
|
|
|
def panic(text):
|
|
|
|
"""
|
|
|
|
Errors that occur before we even are sure about wxPython etc. are dumped
|
|
|
|
on the command line and reported using Tkinter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Tkinter
|
|
|
|
|
|
|
|
print text
|
|
|
|
|
|
|
|
root = Tkinter.Tk()
|
|
|
|
w = Tkinter.Label(root, text=text)
|
|
|
|
w.pack()
|
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
sys.exit(-1)
|
|
|
|
|