Module: POpen4

Defined in:
lib/buzzcore/shell_extras.rb

Defined Under Namespace

Classes: ExecuteError

Class Method Summary collapse

Class Method Details

.pump_thread(aIn, aOut) ⇒ Object



26
27
28
29
30
# File 'lib/buzzcore/shell_extras.rb', line 26

def self.pump_thread(aIn,aOut)
  Thread.new do
    loop { aOut.puts aIn.gets }
  end
end

.shell(aCommand, aWorkingDir = nil, aTimeout = nil, aStdOut = nil, aStdErr = nil) {|result| ... } ⇒ Object

Giving aStdOut,aStdErr causes the command output to be connected to the given stream, and that stream to not be given in the result hash

Yields:

  • (result)

Raises:



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
72
73
# File 'lib/buzzcore/shell_extras.rb', line 47

def self.shell(aCommand,aWorkingDir=nil,aTimeout=nil,aStdOut=nil,aStdErr=nil)
  raise ExecuteError.new('aWorkingDir doesnt exist') unless !aWorkingDir || File.exists?(aWorkingDir)
  orig_wd = Dir.getwd
  result = {:command => aCommand, :dir => (aWorkingDir || orig_wd)}
   status = nil
  begin
    Dir.chdir(aWorkingDir) if aWorkingDir
    Timeout.timeout(aTimeout,ExecuteError) do # nil aTimeout will not time out
      status = POpen4::popen4(aCommand) do |stdout, stderr, stdin, pid|
        thrOut = aStdOut ? Thread.new { aStdOut.puts stdout.read } : nil
        thrErr = aStdErr ? Thread.new { aStdErr.puts stderr.read } : nil
        thrOut.join if thrOut
        thrErr.join if thrErr

        result[:stdout] = stdout.read unless aStdOut
        result[:stderr] = stderr.read unless aStdErr
        result[:pid] = pid
      end
    end
  ensure
    Dir.chdir(orig_wd)
  end
  result[:exitcode] = (status && status.exitstatus) || 1
  yield(result) if block_given?
  raise ExecuteError.new(result) if result[:exitcode] != 0
  return result
end

.shell_out(aCommand, aWorkingDir = nil, aTimeout = nil, &block) ⇒ Object



75
76
77
# File 'lib/buzzcore/shell_extras.rb', line 75

def self.shell_out(aCommand,aWorkingDir=nil,aTimeout=nil,&block)
  block_given? ? POpen4::shell(aCommand,aWorkingDir,aTimeout,STDOUT,STDERR,&block) : POpen4::shell(aCommand,aWorkingDir,aTimeout,STDOUT,STDERR)
end