code: def run_command(cmd, out, ignore_errors=False):
      
      tempdir = tempfile.mkdtemp()
      output_file = os.path.join(tempdir, "stderr")
      original_cmd = " ".join(cmd)
      p = subprocess.Popen(cmd, stdout=out, stderr=subprocess.PIPE)
      t = subprocess.Popen(["tee", output_file], stdin=p.stderr, stdout=out)
      t.wait()
      p.communicate()
      p.stderr.close()
      if p.returncode != 0 and not ignore_errors:
          with open(output_file, "r") as output:
              error = output.read()
          message = 'Command: "{}": Error: "{}"'.format(
              original_cmd, error.replace("\n", "")
          )
          shutil.rmtree(tempdir, ignore_errors=True)
          raise CommandError(message)
  
      shutil.rmtree(tempdir, ignore_errors=True)
      return p.returncode
docs: 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