Skip to content

Commit 22df76e

Browse files
authored
python: Use argument lists instead of shell=True for subprocess (#7703)
1 parent 57968d6 commit 22df76e

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

python/grass/imaging/images2avi.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"""
4040

4141
import os
42+
import shlex
4243
import time
4344
import subprocess
4445
import shutil
@@ -115,14 +116,15 @@ def writeAvi(
115116
formatter = "%03d"
116117

117118
# Compile command to create avi
118-
command = "ffmpeg -r %i %s " % (int(fps), inputOptions)
119-
command += "-i im%s.png " % (formatter,)
120-
command += "-g 1 -vcodec %s %s " % (encoding, outputOptions)
121-
command += "output.avi"
119+
command = ["ffmpeg", "-r", "%i" % int(fps)]
120+
command += shlex.split(inputOptions)
121+
command += ["-i", "im%s.png" % formatter, "-g", "1", "-vcodec", encoding]
122+
command += shlex.split(outputOptions)
123+
command.append("output.avi")
122124

123125
# Run ffmpeg
124126
S = subprocess.Popen(
125-
command, shell=True, cwd=tempDir, stdout=subprocess.PIPE, stderr=subprocess.PIPE
127+
command, cwd=tempDir, stdout=subprocess.PIPE, stderr=subprocess.PIPE
126128
)
127129

128130
# Show what ffmpeg has to say
@@ -180,7 +182,7 @@ def readAvi(filename, asNumpy=True):
180182
shutil.copy(filename, os.path.join(tempDir, "input.avi"))
181183

182184
# Run ffmpeg
183-
command = "ffmpeg -i input.avi im%d.jpg"
185+
command = ["ffmpeg", "-i", "input.avi", "im%d.jpg"]
184186
with subprocess.Popen(
185187
command,
186188
cwd=tempDir,

python/grass/pygrass/modules/grid/grid.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import contextlib
22
import os
3-
import sys
43
import multiprocessing as mltp
54
import subprocess as sub
65
import shutil as sht
76
from math import ceil
87
from pathlib import Path
98

109
from grass.script.setup import write_gisrc
11-
from grass.script import append_node_pid, available_cpus, legalize_vector_name
10+
from grass.script import Popen, append_node_pid, available_cpus, legalize_vector_name
1211

1312
from grass.pygrass.gis import Mapset, Location
1413
from grass.pygrass.gis.region import Region
@@ -215,16 +214,20 @@ def set_region(region, gisrc_src, gisrc_dst, env):
215214
:type env:
216215
:returns: None
217216
"""
218-
reg_str = (
219-
"g.region n=%(north)r s=%(south)r "
220-
"e=%(east)r w=%(west)r "
221-
"nsres=%(nsres)r ewres=%(ewres)r"
222-
)
223-
reg_cmd = reg_str % dict(region.items())
217+
region_dict = dict(region.items())
218+
reg_cmd = [
219+
"g.region",
220+
"n=%r" % region_dict["north"],
221+
"s=%r" % region_dict["south"],
222+
"e=%r" % region_dict["east"],
223+
"w=%r" % region_dict["west"],
224+
"nsres=%r" % region_dict["nsres"],
225+
"ewres=%r" % region_dict["ewres"],
226+
]
224227
env["GISRC"] = gisrc_src
225-
sub.Popen(reg_cmd, shell=True, env=env)
228+
Popen(reg_cmd, env=env)
226229
env["GISRC"] = gisrc_dst
227-
sub.Popen(reg_cmd, shell=True, env=env)
230+
Popen(reg_cmd, env=env)
228231

229232

230233
def copy_rasters(rasters, gisrc_src, gisrc_dst, processes, region=None):
@@ -377,23 +380,22 @@ def cmd_exe(args):
377380
src, dst = get_mapset(gisrc_src, gisrc_dst)
378381
env = os.environ.copy()
379382
env["GISRC"] = gisrc_dst
380-
shell = sys.platform == "win32"
381383
if mapnames:
382384
inputs = dict(cmd["inputs"])
383385
# reset the inputs to
384386
for key in mapnames:
385387
inputs[key] = mapnames[key]
386388
cmd["inputs"] = inputs.items()
387389
# set the region to the tile
388-
sub.Popen(["g.region", "raster=%s" % key], shell=shell, env=env).wait()
390+
Popen(["g.region", "raster=%s" % key], env=env).wait()
389391
else:
390392
# set the computational region
391393
lcmd = ["g.region", *["%s=%s" % (k, v) for k, v in bbox.items()]]
392-
sub.Popen(lcmd, shell=shell, env=env).wait()
394+
Popen(lcmd, env=env).wait()
393395
if groups:
394396
copy_groups(groups, gisrc_src, gisrc_dst, processes=1)
395397
# run the grass command
396-
sub.Popen(get_cmd(cmd), shell=shell, env=env).wait()
398+
Popen(get_cmd(cmd), env=env).wait()
397399
# remove temp GISRC
398400
Path(gisrc_dst).unlink()
399401

0 commit comments

Comments
 (0)