Class: U3dCore::SafePty

Inherits:
Object
  • Object
show all
Defined in:
lib/u3d_core/command_runner.rb

Overview

Executes commands using PTY and takes care of error handling and more

Class Method Summary collapse

Class Method Details

.spawn(command, &_block) ⇒ Object

Wraps the PTY.spawn() call, wait until the process completes. Also catch exceptions that might be raised See also www.omniref.com/ruby/gems/shell_test/0.5.0/files/lib/shell_test/shell_methods/utils.rb



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/u3d_core/command_runner.rb', line 58

def self.spawn(command, &_block)
  require 'pty'
  PTY.spawn(command) do |r, w, p|
    trap('INT') do
      Process.kill("INT", p)
    end
    yield r, w, p
  # if the process has closed, ruby might raise an exception if we try
  # to do I/O on a closed stream. This behavior is platform specific
  rescue Errno::EIO
  ensure
    begin
      Process.wait p
    # The process might have exited.
    # This behavior is also ruby version dependent.
    rescue Errno::ECHILD, PTY::ChildExited
      # do nothing
    end
  end
  $CHILD_STATUS.exitstatus
end