Avoid using 'shell=True' when running a subprocess

This commit is contained in:
Nick Hall 2017-02-24 22:26:55 +00:00
parent ce830fb8bf
commit f92ee329a9
4 changed files with 21 additions and 23 deletions

View File

@ -77,14 +77,13 @@ class Test(unittest.TestCase):
# This tests the fix for bug #1331-1334
# read trivial gedcom input, write gedcom output
def test2_exec_CLI(self):
pyexec = sys.executable
ifile = min1r
ofile = out_ged
gcmd = "Gramps.py -i %s -e %s" % (ifile, ofile)
process = subprocess.Popen("%s %s" % (pyexec, gcmd),
gcmd = [sys.executable, "Gramps.py", "-i", ifile, "-e", ofile]
process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stderr=subprocess.PIPE)
result_str, err_str = process.communicate()
self.assertEqual(process.returncode, 0,
"executed CLI command %r" % gcmd)
@ -110,14 +109,13 @@ class Test(unittest.TestCase):
f.write("garbage")
# ~same as test 2
pyexec = sys.executable
ifile = min1r
ofile = out_ged
gcmd = "Gramps.py -i %s -e %s" % (ifile, ofile)
process = subprocess.Popen("%s %s" % (pyexec, gcmd),
gcmd = [sys.executable, "Gramps.py", "-i", ifile, "-e", ofile]
process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stderr=subprocess.PIPE)
result_str, err_str = process.communicate()
self.assertEqual(process.returncode, 0,
"executed CLI command %r" % gcmd)

View File

@ -30,12 +30,11 @@ def get_git_revision(path=""):
Return the short commit hash of the latest commit.
"""
stdout = ""
command = "git log -1 --format=%h"
command = ['git', 'log', '-1', '--format=%h', path]
try:
proc = subprocess.Popen(
"{} \"{}\"".format(command, path),
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
except OSError:
return "" # subprocess failed

View File

@ -71,13 +71,13 @@ class VCardCheck(unittest.TestCase):
if debug:
print(ET.tostring(input_doc))
pyexec = sys.executable
gcmd = 'Gramps.py -i - -f gramps -e - -f vcf'
process = subprocess.Popen('%s %s' % (pyexec, gcmd),
gcmd = [sys.executable, 'Gramps.py',
'-i', '-', '-f', 'gramps',
'-e', '-', '-f', 'vcf']
process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
env=os.environ)
input_str = (self.header.encode('utf-8') +
ET.tostring(input_doc, encoding='utf-8'))

View File

@ -77,15 +77,16 @@ class VCardCheck(unittest.TestCase):
def do_case(self, input_str, expect_doc, debug=False):
if debug:
print(input_str)
pyexec = sys.executable
gcmd = ('Gramps.py -d .Date -d .ImportVCard '
'--config=preferences.eprefix:DEFAULT '
'-i - -f vcf -e - -f gramps')
process = subprocess.Popen('%s %s' % (pyexec, gcmd),
gcmd = [sys.executable, 'Gramps.py',
'-d', '.Date', '-d', '.ImportVCard',
'--config=preferences.eprefix:DEFAULT',
'-i', '-', '-f', 'vcf',
'-e', '-', '-f', 'gramps']
process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
env=os.environ)
result_str, err_str = process.communicate(input_str.encode("utf-8"))
if debug: