Module: Tetra::ProcessRunner

Includes:
Logging
Included in:
Bash, Git, Tar, Unzip
Defined in:
lib/tetra/facades/process_runner.rb

Overview

runs programs in subprocesses

Defined Under Namespace

Classes: RecordingIO

Instance Method Summary collapse

Methods included from Logging

#log

Instance Method Details

#run(commandline, echo = false, stdin = nil) ⇒ Object

runs a noninteractive executable and returns its output as a string raises ExecutionFailed if the exit status is not 0 optionally echoes the executable’s output/error to standard output/error



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tetra/facades/process_runner.rb', line 11

def run(commandline, echo = false, stdin = nil)
  log.debug "running `#{commandline}`"

  out_recorder = echo ? RecordingIO.new(STDOUT) : RecordingIO.new
  err_recorder = echo ? RecordingIO.new(STDERR) : RecordingIO.new

  status = Open4.spawn(commandline, stdin: stdin, stdout: out_recorder,
                                    stderr: err_recorder, quiet: true).exitstatus

  log.debug "`#{commandline}` exited with status #{status}"

  if status != 0
    log.warn("`#{commandline}` failed with status #{status}")
    out = out_recorder.record
    err = err_recorder.record
    if out != "" || err != ""
      log.warn("Output follows:")
      log.warn(out) unless out == ""
      log.warn(err) unless err == ""
    end
    fail ExecutionFailed.new(commandline, status, out, err)
  end

  out_recorder.record
end

#run_interactive(command) ⇒ Object

runs an interactive executable in a subshell



38
39
40
41
42
43
# File 'lib/tetra/facades/process_runner.rb', line 38

def run_interactive(command)
  log.debug "running `#{command}`"
  success = system({}, command)
  log.debug "`#{command}` exited with success #{success}"
  fail ExecutionFailed.new(command, $CHILD_STATUS, nil, nil) unless success
end