code: def run(build_ninja, flags="", pipe=False, env=default_env):
      with tempfile.TemporaryDirectory() as d:
          os.chdir(d)
          with open("build.ninja", "w") as f:
              f.write(build_ninja)
              f.flush()
          ninja_cmd = "{} {}".format(NINJA_PATH, flags)
          try:
              if pipe:
                  output = subprocess.check_output(
                      [ninja_cmd], shell=True, env=env
                  )
              elif platform.system() == "Darwin":
                  output = subprocess.check_output(
                      [
                          "script",
                          "-q",
                          "/dev/null",
                          "bash",
                          "-c",
                          ninja_cmd,
                      ],
                      env=env,
                  )
              else:
                  output = subprocess.check_output(
                      ["script", "-qfec", ninja_cmd, "/dev/null"],
                      env=env,
                  )
          except subprocess.CalledProcessError as err:
              sys.stdout.buffer.write(err.output)
              raise err
      final_output = ""
      for line in output.decode("utf-8").splitlines(True):
          if len(line) > 0 and line[-1] == "\r":
              continue
          final_output += line.replace("\r", "")
      return final_output
  
file_path: ninja/misc/output_test.py
imports: import os
  import platform
  import subprocess
  import sys
  import tempfile
  import unittest
label_desc: Use image node_id 1 for calling a global function with example usage: run(build_ninja, flags, pipe, env) and returns: final_output