Added new 'safeCommandOutput' command to Scyther/Misc and force use of Python 2.4 or later.

This commit is contained in:
Cas Cremers 2010-05-15 23:26:44 +02:00
parent feb400c610
commit 7d03f22b24

View File

@ -27,11 +27,13 @@
import sys import sys
import os.path import os.path
try: try:
from subprocess import Popen from subprocess import Popen,PIPE
AvailablePopen = True
except: except:
import os panic("""
AvailablePopen = False Cannot import 'subprocess.Popen' module.
You need at least Python 2.4 to use this program.
""")
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
@ -69,21 +71,51 @@ def mypath(file):
basedir = os.path.dirname(__file__) basedir = os.path.dirname(__file__)
return os.path.join(basedir,file) return os.path.join(basedir,file)
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.
"""
p = Popen(cmd, shell=getShell(), stdout=PIPE, stderr=PIPE)
(sout,serr) = p.communicate()
return (p.returncode,sout,serr)
def safeCommand(cmd): def safeCommand(cmd):
""" Execute a command with some arguments. Safe cross-platform """ Execute a command with some arguments. Safe cross-platform
version, I hope. """ version, I hope. """
global AvailablePopen p = Popen(cmd, shell=getShell())
if AvailablePopen:
if sys.platform.startswith("win"):
shell=False
else:
shell=True
p = Popen(cmd, shell=shell)
sts = p.wait() sts = p.wait()
else:
sts = os.system(cmd)
return sts 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.
"""
import Tkinter
print text
root = Tkinter.Tk()
w = Tkinter.Label(root, text=text)
w.pack()
root.mainloop()
sys.exit(-1)