class_docstr: Class to run scripts and return output
  
  Finds local scripts and local modules if running in the development
  directory, otherwise finds system scripts and modules.
docstr: Run command sequence `cmd` returning exit code, stdout, stderr
  
  Parameters
  ----------
  cmd : str or sequence
      string with command name or sequence of strings defining command
  check_code : {True, False}, optional
      If True, raise error for non-zero return code
  
  Returns
  -------
  returncode : int
      return code from execution of `cmd`
  stdout : bytes (python 3) or str (python 2)
      stdout from `cmd`
  stderr : bytes (python 3) or str (python 2)
      stderr from `cmd`
docstr_tok: ["Run","command","sequence","`","cmd","`","returning","exit","code",",","stdout",",","stderr","Parameters","--","--","--","--","--","cmd",":","str","or","sequence","string","with","command","name","or","sequence","of","strings","defining","command","check_code",":","{","True",",","False","}",",","optional","If","True",",","raise","error","for","non-zero","return","code","Returns","--","--","--","-","returncode",":","int","return","code","from","execution","of","`","cmd","`","stdout",":","bytes","(","python","3",")","or","str","(","python","2",")","stdout","from","`","cmd","`","stderr",":","bytes","(","python","3",")","or","str","(","python","2",")","stderr","from","`","cmd","`"]
code: def run_command(self, cmd, check_code=True):
      """Run command sequence `cmd` returning exit code, stdout, stderr
  
      Parameters
      ----------
      cmd : str or sequence
          string with command name or sequence of strings defining command
      check_code : {True, False}, optional
          If True, raise error for non-zero return code
  
      Returns
      -------
      returncode : int
          return code from execution of `cmd`
      stdout : bytes (python 3) or str (python 2)
          stdout from `cmd`
      stderr : bytes (python 3) or str (python 2)
          stderr from `cmd`
      """
      if isinstance(cmd, string_types):
          cmd = [cmd]
      else:
          cmd = list(cmd)
      if self.local_script_dir is not None:
          # Windows can't run script files without extensions natively so we need
          # to run local scripts (no extensions) via the Python interpreter.  On
          # Unix, we might have the wrong incantation for the Python interpreter
          # in the hash bang first line in the source file.  So, either way, run
          # the script through the Python interpreter
          cmd = [
              sys.executable,
              pjoin(self.local_script_dir, cmd[0]),
          ] + cmd[1:]
      if os.name == "nt":
          # Quote any arguments with spaces. The quotes delimit the arguments
          # on Windows, and the arguments might be files paths with spaces.
          # On Unix the list elements are each separate arguments.
          cmd = [f'"{c}"' if " " in c else c for c in cmd]
      if self.debug_print:
          print(f"Running command '{cmd}'")
      env = os.environ
      if self.local_module_dir is not None:
          # module likely comes from the current working directory. We might need
          # that directory on the path if we're running the scripts from a
          # temporary directory
          env = env.copy()
          pypath = env.get("PYTHONPATH", None)
          if pypath is None:
              env["PYTHONPATH"] = self.local_module_dir
          else:
              env["PYTHONPATH"] = (
                  self.local_module_dir + pathsep + pypath
              )
      proc = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env)
      stdout, stderr = proc.communicate()
      if proc.poll() is None:
          proc.terminate()
      if check_code and proc.returncode != 0:
          raise RuntimeError(
              f"""Command "{cmd}" failed with
                  stdout
                  ------
                  {stdout}
                  stderr
                  ------
                  {stderr}
                  """
          )
      opp = self.output_processor
      return proc.returncode, opp(stdout), opp(stderr)
  
file_path: nipy/nipy/tests/scriptrunner.py
imports: import os
  import sys
  from os.path import dirname, isdir, isfile, pathsep, realpath
  from os.path import join
  from subprocess import PIPE, Popen
label_desc: Use image node_id 2 for calling the ScriptRunner obj's underlying member method code with example usage: obj.run_command(cmd, check_code) and returns: proc,  opp,  opp