class Ki::HashLogShell

Attributes

previous[R]

Public Instance Methods

spawn(*arr) click to toggle source
# File lib/util/shell.rb, line 40
def spawn(*arr)
  run_env = {}
  run_options = {}
  if (env)
    run_env.merge!(env)
  end
  if (arr.first.kind_of?(Hash))
    run_env.merge!(arr.delete_at(0))
  end
  if (arr.last.kind_of?(Hash))
    run_options.merge!(arr.delete_at(-1))
  end
  if (chdir && !run_options[:chdir])
    run_options[:chdir] = chdir
  end
  rout = wout = rerr = werr = nil
  if (!run_options[:out])
    rout, wout = IO.pipe
    run_options[:out]=wout
  end
  if (!run_options[:err])
    rerr, werr = IO.pipe
    run_options[:err]=werr
  end
  cmd = arr.first
  root_log.log("Shell command '#{cmd}'") do
    pid = system_spawn(run_env, cmd, run_options)
    pid, status = Process.waitpid2(pid)
    exitstatus = status.exitstatus
    @previous = ShellCommandExecution.new.
        cmd(cmd).
        exitstatus(exitstatus).
        pid(pid).
        env(run_env).
        options(run_options)
    if rout
      wout.close
      @previous.out(rout.readlines.join("\n"))
      rout.close
    end
    if rerr
      werr.close
      @previous.err(rerr.readlines.join("\n"))
      rerr.close
    end
    if (exitstatus != 0 && !ignore_error)
      raise "Shell command '#{cmd}' failed with exit code #{exitstatus}"
    end
    @previous
  end
end
system_spawn(run_env, cmd, run_options) click to toggle source
# File lib/util/shell.rb, line 92
def system_spawn(run_env, cmd, run_options)
  Process.spawn(run_env, cmd, run_options)
end