Method: Open4.spawn

Defined in:
lib/crazy_ivan/vendor/open4-1.0.1/lib/open4.rb

.spawn(arg, *argv) ⇒ Object

Raises:



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/crazy_ivan/vendor/open4-1.0.1/lib/open4.rb', line 249

def spawn arg, *argv 
#--{{{
  argv.unshift(arg)
  opts = ((argv.size > 1 and Hash === argv.last) ? argv.pop : {})
  argv.flatten!
  cmd = argv.join(' ')


  getopt = getopts opts

  ignore_exit_failure = getopt[ 'ignore_exit_failure', getopt['quiet', false] ]
  ignore_exec_failure = getopt[ 'ignore_exec_failure', !getopt['raise', true] ]
  exitstatus = getopt[ %w( exitstatus exit_status status ) ]
  stdin = getopt[ %w( stdin in i 0 ) << 0 ]
  stdout = getopt[ %w( stdout out o 1 ) << 1 ]
  stderr = getopt[ %w( stderr err e 2 ) << 2 ]
  pid = getopt[ 'pid' ]
  timeout = getopt[ %w( timeout spawn_timeout ) ]
  stdin_timeout = getopt[ %w( stdin_timeout ) ]
  stdout_timeout = getopt[ %w( stdout_timeout io_timeout ) ]
  stderr_timeout = getopt[ %w( stderr_timeout ) ]
  status = getopt[ %w( status ) ]
  cwd = getopt[ %w( cwd dir ) ]

  exitstatus =
    case exitstatus
      when TrueClass, FalseClass
        ignore_exit_failure = true if exitstatus
        [0]
      else
        [*(exitstatus || 0)].map{|i| Integer i}
    end

  stdin ||= '' if stdin_timeout
  stdout ||= '' if stdout_timeout
  stderr ||= '' if stderr_timeout

  started = false

  status =
    begin
      chdir(cwd) do
        Timeout::timeout(timeout) do
          popen4(*argv) do |c, i, o, e|
            started = true

            %w( replace pid= << push update ).each do |msg|
              break(pid.send(msg, c)) if pid.respond_to? msg 
            end

            te = ThreadEnsemble.new c

            te.add_thread(i, stdin) do |i, stdin|
              relay stdin, i, stdin_timeout
              i.close rescue nil
            end

            te.add_thread(o, stdout) do |o, stdout|
              relay o, stdout, stdout_timeout
            end

            te.add_thread(e, stderr) do |o, stderr|
              relay e, stderr, stderr_timeout
            end

            te.run
          end
        end
      end
    rescue
      raise unless(not started and ignore_exec_failure)
    end

  raise SpawnError.new(cmd, status) unless
    (ignore_exit_failure or (status.nil? and ignore_exec_failure) or exitstatus.include?(status.exitstatus))

  status
#--}}}
end