|
11 | 11 | import os |
12 | 12 | import subprocess |
13 | 13 |
|
| 14 | +try: |
| 15 | + from subprocess import CalledProcessError |
| 16 | +except ImportError: |
| 17 | + # from python2.7:subprocess.py |
| 18 | + # Exception classes used by this module. |
| 19 | + class CalledProcessError(Exception): |
| 20 | + """This exception is raised when a process run by check_call() returns |
| 21 | + a non-zero exit status. The exit status will be stored in the |
| 22 | + returncode attribute.""" |
| 23 | + def __init__(self, returncode, cmd): |
| 24 | + self.returncode = returncode |
| 25 | + self.cmd = cmd |
| 26 | + def __str__(self): |
| 27 | + return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) |
| 28 | + |
14 | 29 |
|
15 | 30 | # Whether or not to show debug messages |
16 | 31 | DEBUG = False |
@@ -128,6 +143,38 @@ def run_command (args, cwd = None, shell = False, add_env = None, |
128 | 143 | return (exit_code, output, errors) |
129 | 144 |
|
130 | 145 |
|
| 146 | +# from python2.7:subprocess.py |
| 147 | +def call(*popenargs, **kwargs): |
| 148 | + """Run command with arguments. Wait for command to complete, then |
| 149 | + return the returncode attribute. |
| 150 | +
|
| 151 | + The arguments are the same as for the Popen constructor. Example: |
| 152 | +
|
| 153 | + retcode = call(["ls", "-l"]) |
| 154 | + """ |
| 155 | + return subprocess.Popen(*popenargs, **kwargs).wait() |
| 156 | + |
| 157 | + |
| 158 | +# from python2.7:subprocess.py |
| 159 | +def check_call(*popenargs, **kwargs): |
| 160 | + """Run command with arguments. Wait for command to complete. If |
| 161 | + the exit code was zero then return, otherwise raise |
| 162 | + CalledProcessError. The CalledProcessError object will have the |
| 163 | + return code in the returncode attribute. |
| 164 | +
|
| 165 | + The arguments are the same as for the Popen constructor. Example: |
| 166 | +
|
| 167 | + check_call(["ls", "-l"]) |
| 168 | + """ |
| 169 | + retcode = call(*popenargs, **kwargs) |
| 170 | + if retcode: |
| 171 | + cmd = kwargs.get("args") |
| 172 | + if cmd is None: |
| 173 | + cmd = popenargs[0] |
| 174 | + raise CalledProcessError(retcode, cmd) |
| 175 | + return 0 |
| 176 | + |
| 177 | + |
131 | 178 | def file_reader_method (missing_ok = False): |
132 | 179 | """Decorator for simplifying reading of files. |
133 | 180 |
|
|
0 commit comments