40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
82
83
84
85
86
87
88
89
90
|
# File 'lib/util/shell.rb', line 40
def spawn(*arr)
run_env = {}
run_options = {}
if (env)
run_env.merge!(env)
end
if (arr.first.kind_of?(Hash))
run_env.merge!(arr.delete_at(0))
end
if (arr.last.kind_of?(Hash))
run_options.merge!(arr.delete_at(-1))
end
if (chdir && !run_options[:chdir])
run_options[:chdir] = chdir
end
rout = wout = rerr = werr = nil
if (!run_options[:out])
rout, wout = IO.pipe
run_options[:out]=wout
end
if (!run_options[:err])
rerr, werr = IO.pipe
run_options[:err]=werr
end
cmd = arr.first
root_log.log("Shell command '#{cmd}'") do
pid = system_spawn(run_env, cmd, run_options)
pid, status = Process.waitpid2(pid)
exitstatus = status.exitstatus
@previous = ShellCommandExecution.new.
cmd(cmd).
exitstatus(exitstatus).
pid(pid).
env(run_env).
options(run_options)
if rout
wout.close
@previous.out(rout.readlines.join("\n"))
rout.close
end
if rerr
werr.close
@previous.err(rerr.readlines.join("\n"))
rerr.close
end
if (exitstatus != 0 && !ignore_error)
raise "Shell command '#{cmd}' failed with exit code #{exitstatus}"
end
@previous
end
end
|