code: def capture_stdout(
      cmd: List[str], input_str: Optional[str] = None, file=None
  ) -> str:
      """Executes the supplied command with the supplied string of std input, then
      streams the output to stdout, and returns it as a string along with the
      process's return code.
  
      Args:
      cmd: list of strings to send in as the command
      input_str: if supplied, this string will be passed as stdin to the supplied
                 command. if None, stdin will get closed immediately.
      file: optional file-like object (stream): the output from the executed
            process's stdout will get sent to this stream. Defaults to sys.stdout.
  
      Returns:
      Pair of
      - string of all stdout received during the command's execution
      - return code of the process
  
      """
      if file is None:
          file = sys.stdout
  
      buf = io.StringIO()
      ret_code = None
  
      with subprocess.Popen(
          cmd,
          stdin=subprocess.PIPE,
          stdout=subprocess.PIPE,
          stderr=subprocess.STDOUT,
          universal_newlines=False,
      ) as p:
          if input_str:
              p.stdin.write(input_str.encode("utf-8"))
          p.stdin.close()
  
          out = io.TextIOWrapper(p.stdout, newline="")
  
          for line in out:
              buf.write(line)
              file.write(line)
              file.flush()
  
          # flush to force the contents to display.
          file.flush()
  
          while p.poll() is None:
              # Process hasn't exited yet, let's wait some
              time.sleep(0.5)
  
          ret_code = p.returncode
          p.stdout.close()
  
      return buf.getvalue(), ret_code
  
imports: import io
  import os
  import shutil
  import socket
  import subprocess
  import sys
  import time
  import uuid
  from itertools import chain
  from typing import Dict, List, NamedTuple, Optional, Tuple
  from blessings import Terminal
label_desc: Use image node_id 8 for calling a global function with example usage: capture_stdout(cmd, input_str, file) and returns: buf,  ret_code