Module: CMD

Defined in:
lib/rbbt/util/cmd.rb

Defined Under Namespace

Modules: SmartIO Classes: CMDError

Class Method Summary collapse

Class Method Details

.cmd(cmd, options = {}, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rbbt/util/cmd.rb', line 101

def self.cmd(cmd, options = {}, &block)
  options = Misc.add_defaults options, :stderr => Log::DEBUG
  in_content = options.delete(:in)
  stderr     = options.delete(:stderr)
  pipe       = options.delete(:pipe)
  post       = options.delete(:post)
  log        = options.delete(:log)

  log = true if log.nil?

  if stderr == true
    stderr = Log::HIGH
  end

  # Process cmd_options
  cmd_options = process_cmd_options options
  if cmd =~ /'\{opt\}'/
    cmd.sub!('\'{opt}\'', cmd_options) 
  else
    cmd << " " << cmd_options
  end

  in_content = StringIO.new in_content if String === in_content

  sout, serr, sin = IO.pipe, IO.pipe, IO.pipe

  pid = fork {
    begin
      sin.last.close
      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
      end

      STDIN.reopen sin.first
      sin.first.close

      STDERR.reopen serr.last
      serr.last.close

      STDOUT.reopen sout.last
      sout.last.close

      STDOUT.sync = STDERR.sync = true
      
      exec(cmd)

      exit(-1)
    rescue Exception
      Log.debug("CMDError: #{$!.message}") if log
      ddd $!.backtrace if log
      raise CMDError, $!.message
    end
  }

  sin.first.close
  sout.last.close
  serr.last.close

  sin = sin.last
  sout = sout.first
  serr = serr.first
  

  Log.debug "CMD: [#{pid}] #{cmd}" if log

  if in_content.respond_to?(:read)
    Thread.new do
      begin
        loop do
          break if in_content.closed?
          block = in_content.read 1024
          break if block.nil? or block.empty?
          sin.write block
        end

        sin.close unless sin.closed?
        in_content.close unless in_content.closed?
      rescue
        Process.kill "INT", pid
        raise $!
      end
    end
  else
    sin.close
  end

  if pipe
    Thread.new do
      while line = serr.gets
        Log.log line, stderr if Integer === stderr and log
      end
      serr.close
      Thread.exit
    end

    SmartIO.tie sout, pid, cmd, post, in_content, sin, serr

    sout
  else
    err = ""
    Thread.new do
      while not serr.eof?
        err << serr.gets if Integer === stderr
      end
      serr.close
      Thread.exit
    end

    out = StringIO.new sout.read
    sout.close unless sout.closed?
    SmartIO.tie out, pid, cmd, post, in_content, sin, serr

    Process.waitpid pid

    if not $?.success?
      exception      = CMDError.new "Command [#{pid}] #{cmd} failed with error status #{$?.exitstatus}.\n#{err}"
      raise exception
    else
      Log.log err, stderr if Integer === stderr and log
    end

    out
  end
end

.process_cmd_options(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rbbt/util/cmd.rb', line 81

def self.process_cmd_options(options = {})
  string = ""
  options.each do |option, value|
    case 
    when value.nil? || FalseClass === value 
      next
    when TrueClass === value
      string << "#{option} "
    else
      if option.to_s.chars.to_a.last == "="
        string << "#{option}#{value} "
      else
        string << "#{option} #{value} "
      end
    end
  end

  string.strip
end