|
12 | 12 | from weakref import ref, WeakKeyDictionary
|
13 | 13 | import cPickle
|
14 | 14 | import os.path
|
15 |
| -import random |
16 | 15 | from functools import reduce
|
17 | 16 |
|
| 17 | +import subprocess |
| 18 | + |
18 | 19 | import matplotlib
|
19 | 20 |
|
20 | 21 | major, minor1, minor2, s, tmp = sys.version_info
|
@@ -1897,6 +1898,48 @@ def _putmask(a, mask, values):
|
1897 | 1898 | return np.copyto(a, values, where=mask)
|
1898 | 1899 |
|
1899 | 1900 |
|
| 1901 | +def _check_output(*popenargs, **kwargs): |
| 1902 | + r"""Run command with arguments and return its output as a byte |
| 1903 | + string. |
| 1904 | +
|
| 1905 | + If the exit code was non-zero it raises a CalledProcessError. The |
| 1906 | + CalledProcessError object will have the return code in the |
| 1907 | + returncode |
| 1908 | + attribute and output in the output attribute. |
| 1909 | +
|
| 1910 | + The arguments are the same as for the Popen constructor. Example: |
| 1911 | +
|
| 1912 | + >>> check_output(["ls", "-l", "/dev/null"]) |
| 1913 | + 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
| 1914 | +
|
| 1915 | + The stdout argument is not allowed as it is used internally. |
| 1916 | + To capture standard error in the result, use stderr=STDOUT. |
| 1917 | +
|
| 1918 | + >>> check_output(["/bin/sh", "-c", |
| 1919 | + ... "ls -l non_existent_file ; exit 0"], |
| 1920 | + ... stderr=STDOUT) |
| 1921 | + 'ls: non_existent_file: No such file or directory\n' |
| 1922 | + """ |
| 1923 | + if 'stdout' in kwargs: |
| 1924 | + raise ValueError('stdout argument not allowed, it will be overridden.') |
| 1925 | + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) |
| 1926 | + output, unused_err = process.communicate() |
| 1927 | + retcode = process.poll() |
| 1928 | + if retcode: |
| 1929 | + cmd = kwargs.get("args") |
| 1930 | + if cmd is None: |
| 1931 | + cmd = popenargs[0] |
| 1932 | + raise subprocess.CalledProcessError(retcode, cmd, output=output) |
| 1933 | + return output |
| 1934 | + |
| 1935 | + |
| 1936 | +# python2.7's subprocess provides a check_output method |
| 1937 | +if hasattr(subprocess, 'check_output'): |
| 1938 | + check_output = subprocess.check_output |
| 1939 | +else: |
| 1940 | + check_output = _check_output |
| 1941 | + |
| 1942 | + |
1900 | 1943 | if __name__=='__main__':
|
1901 | 1944 | assert( allequal([1,1,1]) )
|
1902 | 1945 | assert(not allequal([1,1,0]) )
|
|
0 commit comments