Modernising some of the shell command execute code.

This commit is contained in:
Cas Cremers 2020-10-27 22:11:51 +01:00
parent 1e46eb3751
commit 8dc7b80bd3

View File

@ -93,21 +93,19 @@ def getShell():
""" """
Determine if we want a shell for Popen Determine if we want a shell for Popen
""" """
if sys.platform.startswith("win"): return False
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, storePopen=None): def safeCommandOutput(cmd, storePopen=None):
""" Execute a command and return (sts,sout,serr). """ Execute a command and return (sts,sout,serr).
Meant for short outputs, as output is stored in memory and Meant for short outputs, as output is stored in memory and
not written to a file. not written to a file.
""" """
p = Popen(cmd, shell=getShell(), stdout=PIPE, stderr=PIPE)
if isinstance(cmd, str):
import shlex
cmd = shlex.split(cmd)
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
if storePopen != None: if storePopen != None:
storePopen(p) storePopen(p)
(sout,serr) = p.communicate() (sout,serr) = p.communicate()
@ -119,7 +117,11 @@ def safeCommand(cmd, storePopen=None):
version, I hope. """ version, I hope. """
try: try:
p = Popen(cmd, shell=getShell()) if isinstance(cmd, str):
import shlex
cmd = shlex.split(cmd)
p = Popen(cmd)
if storePopen != None: if storePopen != None:
storePopen(p) storePopen(p)
sts = p.wait() sts = p.wait()