backport check_output for 2.6

This commit is contained in:
Laurent Bachelier 2015-07-30 10:50:31 +02:00
commit 69a78db3b3
5 changed files with 34 additions and 6 deletions

View file

@ -18,7 +18,7 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
__all__ = ['unicode', 'long', 'basestring']
__all__ = ['unicode', 'long', 'basestring', 'check_output']
try:
@ -35,3 +35,25 @@ try:
basestring = basestring
except NameError:
basestring = str
try:
from subprocess import check_output
except ImportError:
import subprocess
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output