Class: Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/pipe-run.rb,
lib/em-pipe-run.rb

Overview

Pipe class of the pipe-run. Currently with one static method only.

Defined Under Namespace

Classes: Receiver

Class Method Summary collapse

Class Method Details

.run(command, &block) {|String| ... } ⇒ String

Runs the command and returns its standard output.

If block is given, treat call as non-blocking. In that case, loads em-pipe-run and expects EventMachine loaded.

Parameters:

  • command (String)

    command for run

  • block (Proc)

    block for giving back the results

Yields:

  • (String)

    command output

Returns:

  • (String)

    command output



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pipe-run.rb', line 22

def self.run(command, &block)
    if not block.nil?
        begin
            return self.run_nonblock(command, &block)
        rescue NoMethodError
            require "em-pipe-run"
            retry
        end
    end
    
    ###
    
    pipe = File.popen(command, "r")
    
    result = pipe.read
    pipe.close()
    
    return result
end

.run2(command, &block) ⇒ Array

Runs the command and returns both standard output and error output.

If block is given, treat call as non-blocking. In that case, loads em-pipe-run and expects EventMachine loaded.

Parameters:

  • command (String)

    command for run

  • block (Proc)

    block for giving back the results

Returns:

  • (Array)

    command output and error output

Since:

  • 0.3.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pipe-run.rb', line 55

def self.run2(command, &block)
    if not block.nil?
        begin
            return self.run_nonblock2(command, &block)
        rescue NoMethodError
            require "em-pipe-run"
            retry
        end
    end
    
    ###
    
    begin
        stdin, stdout, stderr = Open3.popen3(command)
    rescue NameError
        require "open3"
        retry
    end
    
    stdin.close()
    outvalue = stdout.read
    stdout.close()
    errvalue = stderr.read
    stderr.close()
    
    return [outvalue, errvalue]
end

.run_nonblock(command, &block) {|String| ... } ⇒ Object

Runs the command and yields its standard output.

Parameters:

  • command (String)

    command for run

  • block (Proc)

    callback for giving back the results

Yields:

  • (String)

    command standard output

Since:

  • 0.2.0



76
77
78
79
# File 'lib/em-pipe-run.rb', line 76

def self.run_nonblock(command, &block)
    pipe = File.popen(command, "r")
    EM::attach(pipe, Receiver, block)
end

.run_nonblock2(command, &block) {|String| ... } ⇒ Object

Runs the command and yields both its standard output and error output.

Parameters:

  • command (String)

    command for run

  • block (Proc)

    callback for giving back the results

Yields:

  • (String)

    command standard output

  • (String)

    command erroroutput

Since:

  • 0.3.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/em-pipe-run.rb', line 92

def self.run_nonblock2(command, &block)
    begin
        stdin, stdout, stderr = Open3.popen3(command)
    rescue NameError
        require "open3"
        retry
    end
    
    outval = nil
    errval = nil
    
    yielder = Proc::new do
        if (not outval.nil?) and (not errval.nil?)
            yield outval, errval
        end 
    end
            
    outcall = Proc::new do |_outval|
         outval = _outval
         yielder.call()
    end
    
    errcall = Proc::new do |_errval|
         errval = _errval
         yielder.call()
    end
    
    EM::attach(stdout, Receiver, outcall)
    EM::attach(stderr, Receiver, errcall)
end