Module: CMD::SmartIO

Defined in:
lib/rbbt/util/cmd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def cmd
  @cmd
end

#errObject

Returns the value of attribute err.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def err
  @err
end

#inObject

Returns the value of attribute in.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def in
  @in
end

#logObject

Returns the value of attribute log.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def log
  @log
end

#outObject

Returns the value of attribute out.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def out
  @out
end

#pidObject

Returns the value of attribute pid.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def pid
  @pid
end

#postObject

Returns the value of attribute post.



9
10
11
# File 'lib/rbbt/util/cmd.rb', line 9

def post
  @post
end

Class Method Details

.tie(io, pid = nil, cmd = "", post = nil, sin = nil, out = nil, err = nil, log = true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbbt/util/cmd.rb', line 10

def self.tie(io, pid = nil, cmd = "",  post = nil, sin = nil, out = nil, err = nil, log = true)
  io.extend SmartIO
  io.pid = pid
  io.cmd = cmd
  io.in  = sin 
  io.out  = out 
  io.err  = err 
  io.post = post
  io.log = log

  io.class.send(:alias_method, :original_close, :close)
  io.class.send(:alias_method, :original_read, :read)
  io
end

Instance Method Details

#closeObject



46
47
48
49
50
51
52
53
54
# File 'lib/rbbt/util/cmd.rb', line 46

def close
  self.original_read unless self.closed? or self.eof?

  wait_and_status

  @post.call if @post

  original_close unless self.closed?
end

#force_closeObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbbt/util/cmd.rb', line 56

def force_close
  if @pid
    Log.debug{"Forcing close by killing '#{@pid}'" if log}
    begin
      Process.kill("KILL", @pid)
      Process.waitpid(@pid)
    rescue
      Log.low{"Exception in forcing close of command [#{ @pid }, #{cmd}]: #{$!.message}"}
    end
  end

  @post.call if @post

  original_close unless self.closed?
end

#read(*args) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rbbt/util/cmd.rb', line 72

def read(*args)
  data = original_read(*args) unless self.closed? or self.eof?

  self.close if self.eof? and not self.closed?

  data || ""
end

#wait_and_statusObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rbbt/util/cmd.rb', line 25

def wait_and_status
  if @pid
    begin
      Process.waitpid(@pid)
    rescue
    end

    Log.debug{"Process #{ cmd } succeded" if $? and $?.success? and log}

    if $? and not $?.success?
      Log.debug{"Raising exception" if log}
      exception = CMDError.new "Command [#{@pid}] '#{@cmd}' failed with error status #{$?.exitstatus}"
      begin
        original_close
      ensure
        raise exception
      end
    end
  end
end