Class: FontProcessor::ExternalStdOutCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/fontprocessor/external_execution.rb

Class Method Summary collapse

Class Method Details

.run(command, opts = {}) ⇒ Object

Public: Executes the given command in a blocking manner and expects no output will be returned.

Note: Standard output and standard error are combined. Output on either constitutes output.

command - The command to execute. opts - A boolean that determines whether you expect

output to be returned in normal operation of the
program.

opts - The number of seconds to wait before terminating

the program with a TERM signal. Defaults to 10
seconds.

Returns the output from the command.

Raises ExternalProgramError if output was returned and none was expected or if no ouput was returned and some was expected.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fontprocessor/external_execution.rb', line 27

def self.run(command, opts={})
  expect_output = opts[:expect_output] || false
  timeout = opts[:timeout] || 10
  output = ""

  Open4::popen4(command+ " 2>&1") do |pid, stdin, stdout, stderr|
    result,_,_ = select([stdout], nil, nil, timeout)

    output = nil
    output = result[0].read() if result

    # Make sure the subprocess is dead
    Process.kill "TERM", pid
  end

  if expect_output == :return
    return output
  elsif expect_output
    return output
  elsif expect_output and output and output.strip == ''
    raise ExternalProgramError, "Expected output but none received for \"#{command}\""
  elsif not expect_output and output and output.strip != ''
    raise ExternalProgramError, "Unexpected output for \"#{command}\": #{output}"
  end
end