lib/rbbt/util/cmd.rb in rbbt-util-5.19.12 vs lib/rbbt/util/cmd.rb in rbbt-util-5.19.13

- old
+ new

@@ -1,82 +1,82 @@ require 'rbbt/util/log' require 'stringio' module CMD - module SmartIO - attr_accessor :pid, :cmd, :post, :in, :out, :err, :log - 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 + #module SmartIO + # attr_accessor :pid, :cmd, :post, :in, :out, :err, :log + # 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 + # io.class.send(:alias_method, :original_close, :close) + # io.class.send(:alias_method, :original_read, :read) + # io + # end - def wait_and_status - if @pid - begin - Process.waitpid(@pid) - rescue - end + # def wait_and_status + # if @pid + # begin + # Process.waitpid(@pid) + # rescue + # end - Log.debug{"Process #{ cmd } succeded" if $? and $?.success? and log} + # Log.debug{"Process #{ cmd } succeded" if $? and $?.success? and log} - if $? and not $?.success? - Log.debug{"Raising exception" if log} - exception = ProcessFailed.new "Command [#{@pid}] '#{@cmd}' failed with error status #{$?.exitstatus}" - begin - original_close - ensure - raise exception - end - end - end - end + # if $? and not $?.success? + # Log.debug{"Raising exception" if log} + # exception = ProcessFailed.new "Command [#{@pid}] '#{@cmd}' failed with error status #{$?.exitstatus}" + # begin + # original_close + # ensure + # raise exception + # end + # end + # end + # end - def close - self.original_read unless self.closed? or self.eof? + # def close + # self.original_read unless self.closed? or self.eof? - wait_and_status + # wait_and_status - @post.call if @post + # @post.call if @post - original_close unless self.closed? - end + # original_close unless self.closed? + # end - 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 + # 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 + # @post.call if @post - original_close unless self.closed? - end + # original_close unless self.closed? + # end - def read(*args) - data = original_read(*args) unless self.closed? #or self.eof? + # def read(*args) + # data = original_read(*args) unless self.closed? #or self.eof? - self.close if not self.closed? and self.eof? + # self.close if not self.closed? and self.eof? - data #|| "" - end - end + # data #|| "" + # end + #end def self.process_cmd_options(options = {}) string = "" options.each do |option, value| @@ -103,10 +103,11 @@ stderr = options.delete(:stderr) pipe = options.delete(:pipe) post = options.delete(:post) log = options.delete(:log) no_fail = options.delete(:no_fail) + no_wait = options.delete(:no_wait) dont_close_in = options.delete(:dont_close_in) log = true if log.nil? if stderr == true @@ -130,19 +131,13 @@ sout.first.close serr.first.close io = in_content while IO === io - if SmartIO === io - io.original_close unless io.closed? - io.out.close unless io.out.nil? or io.out.closed? - io.err.close unless io.err.nil? or io.err.closed? - io = io.in - else - io.close unless io.closed? - io = nil - end + io.join if io.respond_to?(:join) and not io.joined? + io.close if io.respond_to?(:close) and not io.closed? + io = nil end STDIN.reopen sin.first sin.first.close @@ -204,10 +199,10 @@ end serr.close end #SmartIO.tie sout, pid, cmd, post, in_content, sin, serr - ConcurrentStream.setup sout, :pids => [pid], :autojoin => true, :no_fail => no_fail + ConcurrentStream.setup sout, :pids => [pid], :autojoin => true, :no_fail => no_fail unless no_wait sout else err = "" Thread.new do