Class: Sprout::ProcessRunner

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*command) ⇒ ProcessRunner

Returns a new instance of ProcessRunner.



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

def initialize(*command)
  @command = command
  begin
    @alive = true
    usr = User.new()
    if(usr.is_a?(WinUser) && !usr.is_a?(CygwinUser))
      gem 'win32-open3'
      require 'win32/open3'
      Open3.popen3(*@command) do |w, r, e, pid|
        @w = w
        @r = r
        @e = e
        @pid = pid
      end
    else
      require 'open4'
      @pid, @w, @r, @e = open4.popen4(*@command)
    end
  rescue Errno::EACCES => e
    update_executable_mode(*@command)
    @pid, @w, @r, @e = open4.popen4(*@command)
  rescue Errno::ENOENT => e
    @alive = false
    part = command[0].split(' ').shift
    raise ProcessRunnerError.new("The expected executable was not found for command [#{part}], please check your system path and/or sprout definition")
  end
end

Instance Attribute Details

#eObject (readonly)

Returns the value of attribute e.



7
8
9
# File 'lib/sprout/process_runner.rb', line 7

def e
  @e
end

#pidObject (readonly)

Returns the value of attribute pid.



7
8
9
# File 'lib/sprout/process_runner.rb', line 7

def pid
  @pid
end

#rObject (readonly)

Returns the value of attribute r.



7
8
9
# File 'lib/sprout/process_runner.rb', line 7

def r
  @r
end

#wObject (readonly)

Returns the value of attribute w.



7
8
9
# File 'lib/sprout/process_runner.rb', line 7

def w
  @w
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sprout/process_runner.rb', line 53

def alive?
  @alive = update_status
end

#closeObject



61
62
63
# File 'lib/sprout/process_runner.rb', line 61

def close
  update_status
end

#close_writeObject



99
100
101
# File 'lib/sprout/process_runner.rb', line 99

def close_write
  @w.close_write
end

#flushObject



83
84
85
# File 'lib/sprout/process_runner.rb', line 83

def flush
  @w.flush
end

#getcObject



87
88
89
# File 'lib/sprout/process_runner.rb', line 87

def getc
  @r.getc
end

#killObject



57
58
59
# File 'lib/sprout/process_runner.rb', line 57

def kill
  Process.kill(9, pid)
end


91
92
93
# File 'lib/sprout/process_runner.rb', line 91

def print(msg)
  @w.print msg
end

#puts(msg) ⇒ Object



95
96
97
# File 'lib/sprout/process_runner.rb', line 95

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

#readObject



103
104
105
# File 'lib/sprout/process_runner.rb', line 103

def read
  return @r.read
end

#read_errObject



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

def read_err
  return @e.read
end

#readlinesObject



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

def readlines
  @r.readlines
end

#readpartial(count) ⇒ Object



75
76
77
# File 'lib/sprout/process_runner.rb', line 75

def readpartial(count)
  @r.readpartial(count)
end

#update_executable_mode(*command) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sprout/process_runner.rb', line 40

def update_executable_mode(*command)
  parts = command.join(' ').split(' ')
  str = parts.shift
  while(parts.size > 0)
    if(File.exists?(str))
      FileUtils.chmod(744, str)
      return
    else
      str << " #{parts.shift}"
    end
  end
end

#update_statusObject



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

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