scyther/gui/Scyther/Misc.py

161 lines
4.1 KiB
Python
Raw Normal View History

2007-06-11 13:09:24 +01:00
"""
Scyther : An automatic verifier for security protocols.
2013-10-05 23:56:12 +01:00
Copyright (C) 2007-2013 Cas Cremers
2007-06-11 13:09:24 +01:00
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 """
import sys
2006-08-02 13:59:57 +01:00
import os.path
try:
from subprocess import Popen,PIPE
except:
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'):
2020-10-27 21:09:03 +00:00
print(question, end=' ')
answer = input().lower()
2006-08-02 13:59:57 +01:00
return answer == 'y'
def exists(func,list):
2020-10-27 21:09:03 +00:00
return len(list(filter(func,list))) > 0
2006-08-02 13:59:57 +01:00
def forall(func,list):
2020-10-27 21:09:03 +00:00
return len(list(filter(func,list))) == len(list)
2006-08-02 13:59:57 +01:00
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
# ensurePath: wraps os.makedirs
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...
"""
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.makedirs(pt)
2006-08-02 13:59:57 +01:00
# path
def mypath(file):
""" Construct a file path relative to the scyther-gui main directory
"""
# Determine base directory (taking symbolic links into account)
cmd_file = os.path.realpath(os.path.abspath(inspect.getfile( inspect.currentframe() )))
basedir = os.path.split(cmd_file)[0]
2006-08-02 13:59:57 +01:00
return os.path.join(basedir,file)
def getShell():
"""
Determine if we want a shell for Popen
"""
return False
def safeCommandOutput(cmd, storePopen=None):
""" Execute a command and return (sts,sout,serr).
Meant for short outputs, as output is stored in memory and
not written to a file.
"""
if isinstance(cmd, str):
import shlex
cmd = shlex.split(cmd)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
if storePopen != None:
storePopen(p)
(sout,serr) = p.communicate()
2010-05-15 23:33:53 +01:00
return (p.returncode,sout,serr)
def safeCommand(cmd, storePopen=None):
""" Execute a command with some arguments. Safe cross-platform
version, I hope. """
try:
if isinstance(cmd, str):
import shlex
cmd = shlex.split(cmd)
p = Popen(cmd)
if storePopen != None:
storePopen(p)
sts = p.wait()
2020-10-27 21:09:03 +00:00
except KeyboardInterrupt as EnvironmentError:
raise
except:
2020-10-27 21:09:03 +00:00
print("Wile processing [%s] we had an" % (cmd))
print("unexpected error:", sys.exc_info()[0])
print()
sts = -1
raise # For now still raise
return sts
def panic(text):
"""
Errors that occur before we even are sure about wxPython etc. are dumped
on the command line and reported using Tkinter.
"""
try:
2020-10-27 21:09:03 +00:00
import tkinter
except:
2020-10-27 21:09:03 +00:00
print(text)
sys.exit(-1)
2020-10-27 21:09:03 +00:00
print(text)
2020-10-27 21:09:03 +00:00
root = tkinter.Tk()
w = tkinter.Label(root, justify=tkinter.LEFT, padx = 10, text=text)
w.pack()
root.mainloop()
sys.exit(-1)