def run_command(cmd, out, ignore_errors=False):
tempdir = tempfile.mkdtemp()<EOL>output_file = os.path.join(tempdir, "<STR_LIT>")<EOL>original_cmd = "<STR_LIT:U+0020>".join(cmd)<EOL>p = subprocess.Popen(cmd, stdout=out, stderr=subprocess.PIPE)<EOL>t = subprocess.Popen(["<STR_LIT>", output_file], stdin=p.stderr, stdout=out)<EOL>t.wait()<EOL>p.communicate()<EOL>p.stderr.close()<EOL>if p.returncode != <NUM_LIT:0> and not ignore_errors:<EOL><INDENT>with open(output_file, "<STR_LIT:r>") as output:<EOL><INDENT>error = output.read()<EOL><DEDENT>message = '<STR_LIT>'.format(<EOL>original_cmd, error.replace("<STR_LIT:\n>", "<STR_LIT>")<EOL>)<EOL>shutil.rmtree(tempdir, ignore_errors=True)<EOL>raise CommandError(message)<EOL><DEDENT>shutil.rmtree(tempdir, ignore_errors=True)<EOL>return p.returncode<EOL>
We want to both send subprocess output to stdout or another file
    descriptor as the subprocess runs, *and* capture the actual exception
    message on errors. CalledProcessErrors do not reliably contain the
    underlying exception in either the 'message' or 'out' attributes, so
    we tee the stderr to a temporary file and if a CalledProcessError is
    raised we read its contents to recover stderr