| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
""" A wrapper function to aggregate these calls in one file. |
| 18 |
Bandit B602: subprocess_popen_with_shell_equals_true |
| 19 |
https://docs.openstack.org/developer/bandit/plugins/subprocess_popen_with_shell_equals_true.html |
| 20 |
B605: start_process_with_a_shell |
| 21 |
https://docs.openstack.org/developer/bandit/plugins/start_process_with_a_shell.html |
| 22 |
""" |
| 23 |
from shlex import split |
| 24 |
from subprocess import PIPE, STDOUT, DEVNULL, Popen # nosec |
| 25 |
|
| 26 |
# pylint: disable=too-many-arguments |
| 27 |
# pylint: disable=too-many-locals |
| 28 |
|
| 29 |
|
| 30 |
def procopen(cmd, bufsize=0, executable=None, stdin=None, stdout=None, |
| 31 |
stderr=None, preexec_fn=None, close_fds=False, usesh=False, |
| 32 |
cwd=None, env=None, universal_newlines=False, startupinfo=None, |
| 33 |
creationflags=0, splitcmd=False, stdoutpipe=False, |
| 34 |
stdoutout=False, stderrpipe=False, stderrout=False, |
| 35 |
stdindevnull=DEVNULL): |
| 36 |
|
| 37 |
shell = usesh |
| 38 |
|
| 39 |
if stdoutpipe is True: |
| 40 |
stdout = PIPE |
| 41 |
elif stdoutout is True: |
| 42 |
stdout = STDOUT |
| 43 |
if stderrpipe is True: |
| 44 |
stderr = PIPE |
| 45 |
elif stderrout is True: |
| 46 |
stderr = STDOUT |
| 47 |
if stdindevnull is True: |
| 48 |
stdin = DEVNULL |
| 49 |
|
| 50 |
if splitcmd is True: |
| 51 |
command = split(cmd) |
| 52 |
else: |
| 53 |
command = cmd |
| 54 |
|
| 55 |
process = Popen(command, bufsize, executable, stdin, stdout, # nosec |
| 56 |
stderr, preexec_fn, close_fds, shell, cwd, env, |
| 57 |
universal_newlines, startupinfo, creationflags) |
| 58 |
|
| 59 |
return process |