|
21 | 21 |
|
22 | 22 | from os.path import dirname, isdir, join |
23 | 23 | import re |
24 | | -from subprocess import CalledProcessError, check_output |
| 24 | +from subprocess import CalledProcessError, Popen, PIPE |
| 25 | + |
| 26 | +try: |
| 27 | + from subprocess import check_output |
| 28 | +except ImportError: |
| 29 | + def check_output(*popenargs, **kwargs): |
| 30 | + r"""Run command with arguments and return its output as a byte string. |
| 31 | +
|
| 32 | + If the exit code was non-zero it raises a CalledProcessError. The |
| 33 | + CalledProcessError object will have the return code in the returncode |
| 34 | + attribute and output in the output attribute. |
| 35 | +
|
| 36 | + The arguments are the same as for the Popen constructor. Example: |
| 37 | +
|
| 38 | + >>> check_output(["ls", "-l", "/dev/null"]) |
| 39 | + 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 40 | +
|
| 41 | + The stdout argument is not allowed as it is used internally. |
| 42 | + To capture standard error in the result, use stderr=STDOUT. |
| 43 | +
|
| 44 | + >>> check_output(["/bin/sh", "-c", |
| 45 | + ... "ls -l non_existent_file ; exit 0"], |
| 46 | + ... stderr=STDOUT) |
| 47 | + 'ls: non_existent_file: No such file or directory\n' |
| 48 | + """ |
| 49 | + if 'stdout' in kwargs: |
| 50 | + raise ValueError('stdout argument not allowed, it will be overridden.') |
| 51 | + process = Popen(stdout=PIPE, *popenargs, **kwargs) |
| 52 | + output, unused_err = process.communicate() |
| 53 | + retcode = process.poll() |
| 54 | + if retcode: |
| 55 | + cmd = kwargs.get("args") |
| 56 | + if cmd is None: |
| 57 | + cmd = popenargs[0] |
| 58 | + raise CalledProcessError(retcode, cmd, output=output) |
| 59 | + return output |
25 | 60 |
|
26 | 61 | version_re = re.compile('^Version: (.+)$', re.M) |
27 | 62 |
|
|
0 commit comments