Class: Fig::ExternalProgram

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/external_program.rb

Class Method Summary collapse

Class Method Details

.capture(command_line, input = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fig/external_program.rb', line 34

def self.capture(command_line, input = nil)
  output = nil
  errors = nil

  result = popen(*command_line) do
    |stdin, stdout, stderr|

    if ! input.nil?
      stdin.puts input # Potential deadlock if input is bigger than pipe size.
    end
    stdin.close

    output = StringIO.new
    errors = StringIO.new
    input_to_output = {stdout => output, stderr => errors}
    while ! input_to_output.empty?
      ready, * = IO.select input_to_output.keys
      ready.each do
        |handle|

        begin
          input_to_output[handle] << handle.readpartial(4096)
        rescue EOFError
          input_to_output.delete handle
        end
      end
    end
  end

  if ! output.nil?
    output = output.string
  end
  if ! errors.nil?
    errors = errors.string
  end

  return output, errors, result
end

.popen(*cmd) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fig/external_program.rb', line 10

def self.popen(*cmd)
  exit_code = nil

  Open3.popen3(*cmd) { |stdin, stdout, stderr, wait_thread|
    yield stdin, stdout, stderr

    exit_code = wait_thread.value
  }

  return exit_code
end

.set_up_open3Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fig/external_program.rb', line 8

def self.set_up_open3
  require 'open3'
  def self.popen(*cmd)
    exit_code = nil

    Open3.popen3(*cmd) { |stdin, stdout, stderr, wait_thread|
      yield stdin, stdout, stderr

      exit_code = wait_thread.value
    }

    return exit_code
  end
end