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 # This tests the fix for bug #1331-1334
# read trivial gedcom input, write gedcom output # read trivial gedcom input, write gedcom output
def test2_exec_CLI(self): def test2_exec_CLI(self):
pyexec = sys.executable
ifile = min1r ifile = min1r
ofile = out_ged ofile = out_ged
gcmd = "Gramps.py -i %s -e %s" % (ifile, ofile) gcmd = [sys.executable, "Gramps.py", "-i", ifile, "-e", ofile]
process = subprocess.Popen("%s %s" % (pyexec, gcmd), process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) stderr=subprocess.PIPE)
result_str, err_str = process.communicate() result_str, err_str = process.communicate()
self.assertEqual(process.returncode, 0, self.assertEqual(process.returncode, 0,
"executed CLI command %r" % gcmd) "executed CLI command %r" % gcmd)
@ -110,14 +109,13 @@ class Test(unittest.TestCase):
f.write("garbage") f.write("garbage")
# ~same as test 2 # ~same as test 2
pyexec = sys.executable
ifile = min1r ifile = min1r
ofile = out_ged ofile = out_ged
gcmd = "Gramps.py -i %s -e %s" % (ifile, ofile) gcmd = [sys.executable, "Gramps.py", "-i", ifile, "-e", ofile]
process = subprocess.Popen("%s %s" % (pyexec, gcmd), process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True) stderr=subprocess.PIPE)
result_str, err_str = process.communicate() result_str, err_str = process.communicate()
self.assertEqual(process.returncode, 0, self.assertEqual(process.returncode, 0,
"executed CLI command %r" % gcmd) "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. Return the short commit hash of the latest commit.
""" """
stdout = "" stdout = ""
command = "git log -1 --format=%h" command = ['git', 'log', '-1', '--format=%h', path]
try: try:
proc = subprocess.Popen( proc = subprocess.Popen(command,
"{} \"{}\"".format(command, path), stdout=subprocess.PIPE,
shell=True, stderr=subprocess.PIPE)
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = proc.communicate() (stdout, stderr) = proc.communicate()
except OSError: except OSError:
return "" # subprocess failed return "" # subprocess failed

View File

@ -71,13 +71,13 @@ class VCardCheck(unittest.TestCase):
if debug: if debug:
print(ET.tostring(input_doc)) print(ET.tostring(input_doc))
pyexec = sys.executable gcmd = [sys.executable, 'Gramps.py',
gcmd = 'Gramps.py -i - -f gramps -e - -f vcf' '-i', '-', '-f', 'gramps',
process = subprocess.Popen('%s %s' % (pyexec, gcmd), '-e', '-', '-f', 'vcf']
process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True,
env=os.environ) env=os.environ)
input_str = (self.header.encode('utf-8') + input_str = (self.header.encode('utf-8') +
ET.tostring(input_doc, encoding='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): def do_case(self, input_str, expect_doc, debug=False):
if debug: if debug:
print(input_str) print(input_str)
pyexec = sys.executable
gcmd = ('Gramps.py -d .Date -d .ImportVCard ' gcmd = [sys.executable, 'Gramps.py',
'--config=preferences.eprefix:DEFAULT ' '-d', '.Date', '-d', '.ImportVCard',
'-i - -f vcf -e - -f gramps') '--config=preferences.eprefix:DEFAULT',
process = subprocess.Popen('%s %s' % (pyexec, gcmd), '-i', '-', '-f', 'vcf',
'-e', '-', '-f', 'gramps']
process = subprocess.Popen(gcmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
shell=True,
env=os.environ) env=os.environ)
result_str, err_str = process.communicate(input_str.encode("utf-8")) result_str, err_str = process.communicate(input_str.encode("utf-8"))
if debug: if debug: