| 129 | |
| 130 | |
| 131 | def run_cmd(cmd, **kwargs): |
| 132 | if isinstance(cmd, str): |
| 133 | cmd = cmd.split(' ') |
| 134 | |
| 135 | try: |
| 136 | kwargs.update(stderr=subprocess.STDOUT) |
| 137 | output = subprocess.check_output(cmd, **kwargs) |
| 138 | except subprocess.CalledProcessError as e: |
| 139 | # this avoids hiding the stdout / stderr of failed processes |
| 140 | sio = io.StringIO() |
| 141 | command_line_string = '' |
| 142 | env = kwargs.get('env', {}) |
| 143 | for key in env.keys() - os.environ.keys(): |
| 144 | value = env[key] |
| 145 | command_line_string += f'{key}={value} ' |
| 146 | command_line_string += ' '.join(cmd) |
| 147 | print(f'Command failed: {command_line_string}', file=sio) |
| 148 | print('With output:', file=sio) |
| 149 | print('--------------', file=sio) |
| 150 | print(frombytes(e.output), file=sio) |
| 151 | print('--------------', file=sio) |
| 152 | raise RuntimeError(sio.getvalue()) |
| 153 | |
| 154 | return frombytes(output) |
| 155 | |
| 156 | |
| 157 | # Adapted from CPython |