diff --git a/satori/bash.py b/satori/bash.py index 93a2fd7..fb5a87f 100644 --- a/satori/bash.py +++ b/satori/bash.py @@ -20,8 +20,8 @@ Execute commands over ssh or using the python subprocess module. import logging import shlex -import subprocess +from satori.common import popen from satori import errors from satori import smb from satori import ssh @@ -147,11 +147,11 @@ class LocalShell(ShellMixin): """ cwd = kwargs.get('cwd') with_exit_code = kwargs.get('with_exit_code') - spipe = subprocess.PIPE + spipe = popen.PIPE cmd = shlex.split(command) LOG.debug("Executing `%s` on local machine", command) - result = subprocess.Popen( + result = popen.popen( cmd, stdout=spipe, stderr=spipe, cwd=cwd) out, err = result.communicate() resultdict = { diff --git a/satori/common/popen.py b/satori/common/popen.py new file mode 100644 index 0000000..f637bad --- /dev/null +++ b/satori/common/popen.py @@ -0,0 +1,23 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" Popen wrapper to allow custom patching of subprocess.Popen.""" + +import subprocess + +PIPE = subprocess.PIPE +STDOUT = subprocess.STDOUT + + +def popen(*args, **kwargs): + """Wrap Popen to allow for higher level patching if necessary.""" + return subprocess.Popen(*args, **kwargs) diff --git a/satori/smb.py b/satori/smb.py index ac0c285..bab5b31 100644 --- a/satori/smb.py +++ b/satori/smb.py @@ -26,9 +26,9 @@ import logging import os import re import shlex -import subprocess import tempfile +from satori.common import popen from satori import errors from satori import ssh from satori import tunnel @@ -199,11 +199,11 @@ class SMBClient(object): # pylint: disable=R0902 self.username, self.password, self.host) - self._process = subprocess.Popen( + self._process = popen.popen( shlex.split(self._substituted_command), stdout=self._file_write, - stderr=subprocess.STDOUT, - stdin=subprocess.PIPE, + stderr=popen.STDOUT, + stdin=popen.PIPE, close_fds=True, universal_newlines=True, bufsize=-1) diff --git a/satori/tests/test_bash.py b/satori/tests/test_bash.py index 7bde863..b9da53c 100644 --- a/satori/tests/test_bash.py +++ b/satori/tests/test_bash.py @@ -39,7 +39,7 @@ class TestLocalShell(TestBashModule): def setUp(self): super(TestLocalShell, self).setUp() - popen_patcher = mock.patch.object(bash.subprocess, 'Popen') + popen_patcher = mock.patch.object(bash.popen, 'popen') self.mock_popen = popen_patcher.start() mock_result = mock.MagicMock() mock_result.returncode = self.testrun.returncode diff --git a/tox.ini b/tox.ini index c38d24b..e961cab 100644 --- a/tox.ini +++ b/tox.ini @@ -49,4 +49,4 @@ downloadcache = ~/cache/pip ignore = H102 show-source = True exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,tools,*satori/contrib*,*.ropeproject,*satori/tests*,setup.py -max-complexity = 12 +max-complexity = 16