Class: Sprout::ProcessRunner

Inherits:
Object
  • Object
show all
Defined in:
sprout/lib/sprout/process_runner.rb

Overview

The ProcessRunner is a cross-platform wrapper for executing external executable processes.

This class is typically accesses via the concrete Sprout::System classes in order to avoid ugly branching logic in the application layer.

An example of this kind of usage might be:

Sprout.current_system.execute './some.exe', '--foo=bar --baz=buz'

To use this class directly, you need to know if you're on a unix-like system or a dos platform as these two deal with processes very differently.

Assuming you know you're on a unix-like system, you could execute the previous example with:

runner = Sprout::ProcessRunner.new
runner.execute_open4 './some.exe --foo-bar --baz=buz'
puts runner.read

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ProcessRunner) initialize

A new instance of ProcessRunner



43
44
45
46
# File 'sprout/lib/sprout/process_runner.rb', line 43

def initialize
  super
  @ruby_version = RUBY_VERSION
end

Instance Attribute Details

- (IO) e (readonly)

Error output

Returns:

  • (IO)

    Error output



41
42
43
# File 'sprout/lib/sprout/process_runner.rb', line 41

def e
  @e
end

- (Object) pid (readonly)

Returns the value of attribute pid



28
29
30
# File 'sprout/lib/sprout/process_runner.rb', line 28

def pid
  @pid
end

- (IO) r (readonly)

Read only

Returns:

  • (IO)

    Read only



33
34
35
# File 'sprout/lib/sprout/process_runner.rb', line 33

def r
  @r
end

- (Object) ruby_version (readonly)

Returns the value of attribute ruby_version



29
30
31
# File 'sprout/lib/sprout/process_runner.rb', line 29

def ruby_version
  @ruby_version
end

- (IO) w (readonly)

Write only

Returns:

  • (IO)

    Write only



37
38
39
# File 'sprout/lib/sprout/process_runner.rb', line 37

def w
  @w
end

Instance Method Details

- (Boolean) alive?

Whether the process is still running.

Returns:

  • (Boolean)

    whether the process is still running.



73
74
75
# File 'sprout/lib/sprout/process_runner.rb', line 73

def alive?
  @alive = update_status
end

- (Object) close

Close the process



85
86
87
# File 'sprout/lib/sprout/process_runner.rb', line 85

def close
  w.close_write
end

- (Object) close_write

Close the write stream - usually terminates the process.



147
148
149
# File 'sprout/lib/sprout/process_runner.rb', line 147

def close_write
  @w.close_write
end

- (Object) execute_open4(*command)

Execute the provided command using the open4.popen4 library. This is generally only used by Cygwin and *nix variants (including OS X).



52
53
54
55
56
57
58
59
# File 'sprout/lib/sprout/process_runner.rb', line 52

def execute_open4 *command
  execute_with_block *command do
    # Not sure about the join - with the 1.0 push, we're
    # sending in 2 args - the exe path, and options as a string.
    # This was new and was causing problems...
    @pid, @w, @r, @e = open4_popen4_block *command.join(' ')
  end
end

- (Object) execute_win32(*command)

Execute the provided command using the win32-open3 library. This should only be used on Windows systems (even 64 bit).



65
66
67
68
69
# File 'sprout/lib/sprout/process_runner.rb', line 65

def execute_win32(*command)
  execute_with_block *command do
    @pid, @w, @r, @e = io_popen_block *command.join(' ')
  end
end

- (Object) flush

Flush the write IO to the process.



123
124
125
# File 'sprout/lib/sprout/process_runner.rb', line 123

def flush
  @w.flush
end

- (Object) getc

Get user input on the read stream from the process.



129
130
131
# File 'sprout/lib/sprout/process_runner.rb', line 129

def getc
  @r.getc
end

- (Object) kill

Kill the process.



79
80
81
# File 'sprout/lib/sprout/process_runner.rb', line 79

def kill
  update_status 9
end

Print some characters to process without an end of line character.



135
136
137
# File 'sprout/lib/sprout/process_runner.rb', line 135

def print msg
  @w.print msg
end

- (Object) puts(msg)

Print characters to the process followed by an end of line.



141
142
143
# File 'sprout/lib/sprout/process_runner.rb', line 141

def puts(msg)
  @w.puts(msg)
end

- (Object) read

Wait for the process to end and return the entire standard output.



153
154
155
# File 'sprout/lib/sprout/process_runner.rb', line 153

def read
  return @r.read
end

- (Object) read_err

Wait for the process to end and return the entire standard error.



159
160
161
# File 'sprout/lib/sprout/process_runner.rb', line 159

def read_err
  return @e.read
end

- (String) readlines(count)

Read count lines from the process standard out.

Parameters:

  • (Integer) count

    Number of lines to read.

Returns:



117
118
119
# File 'sprout/lib/sprout/process_runner.rb', line 117

def readlines count
  @r.readlines count
end

- (String) readpartial(count)

Read count characters from the process standard out.

Parameters:

  • (Integer) count

    Number of characters to read.

Returns:



108
109
110
# File 'sprout/lib/sprout/process_runner.rb', line 108

def readpartial count
  @r.readpartial count
end

- (Object) readpartial_err(count)



163
164
165
# File 'sprout/lib/sprout/process_runner.rb', line 163

def readpartial_err count
  return @e.readpartial count
end

- (Object) update_status(sig = 0)

Send an update signal to the process.

Parameters:

  • (Integer) sig (defaults to: 0)

    The signal to send, default 0 (or no action requested)



93
94
95
96
97
98
99
100
101
# File 'sprout/lib/sprout/process_runner.rb', line 93

def update_status sig=0
  pid_int = Integer("#{ @pid }")
  begin
    Process::kill sig, pid_int
    true
  rescue Errno::ESRCH
    false
  end
end