Sha256: 07076d55fde425ffadea19407ccfd0692a2ed54aec47b12461ef4fff6bba8d20

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

require 'childprocess'
require 'tempfile'
require 'shellwords'

module Aruba
  class Process
    include Shellwords

    def initialize(cmd, exit_timeout, io_wait)
      @exit_timeout = exit_timeout
      @io_wait = io_wait

      @out = Tempfile.new("aruba-out")
      @err = Tempfile.new("aruba-err")
      @process = ChildProcess.build(*shellwords(cmd))
      @process.io.stdout = @out
      @process.io.stderr = @err
      @process.duplex = true
    end

    def run!(&block)
      @process.start
      yield self if block_given?
    end

    def stdin
      wait_for_io do
        @process.io.stdin.sync = true
        @process.io.stdin
      end
    end

    def output(keep_ansi)
      stdout(keep_ansi) + stderr(keep_ansi)
    end

    def stdout(keep_ansi)
      wait_for_io do
        @out.rewind
        filter_ansi(@out.read, keep_ansi)
      end
    end

    def stderr(keep_ansi)
      wait_for_io do
        @err.rewind
        filter_ansi(@err.read, keep_ansi)
      end
    end

    def stop(reader, keep_ansi)
      return unless @process
      unless @process.exited?
        reader.stdout stdout(keep_ansi)
        reader.stderr stderr(keep_ansi)
        @process.poll_for_exit(@exit_timeout)
      end
      @process.exit_code
    end

    def terminate(keep_ansi)
      if @process
        stdout(keep_ansi) && stderr(keep_ansi) # flush output
        @process.stop
        stdout(keep_ansi) && stderr(keep_ansi) # flush output
      end
    end

    private

    def wait_for_io(&block)
      sleep @io_wait if @process.alive?
      yield
    end

    def filter_ansi(string, keep_ansi)
      keep_ansi ? string : string.gsub(/\e\[\d+(?>(;\d+)*)m/, '')
    end

  end
end

Version data entries

6 entries across 6 versions & 4 rubygems

Version Path
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/aruba-0.4.11/lib/aruba/process.rb
librarian-puppet-0.9.3 vendor/gems/ruby/1.8/gems/aruba-0.4.11/lib/aruba/process.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/aruba-0.4.11/lib/aruba/process.rb
ftl-0.2.0 vendor/bundle/gems/aruba-0.4.11/lib/aruba/process.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/aruba-0.4.11/lib/aruba/process.rb
aruba-0.4.11 lib/aruba/process.rb