Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/share/ruby
File: open3.rb
# frozen_string_literal: true
[0] Fix | Delete
[1] Fix | Delete
#
[2] Fix | Delete
# = open3.rb: Popen, but with stderr, too
[3] Fix | Delete
#
[4] Fix | Delete
# Author:: Yukihiro Matsumoto
[5] Fix | Delete
# Documentation:: Konrad Meyer
[6] Fix | Delete
#
[7] Fix | Delete
# Open3 gives you access to stdin, stdout, and stderr when running other
[8] Fix | Delete
# programs.
[9] Fix | Delete
#
[10] Fix | Delete
[11] Fix | Delete
#
[12] Fix | Delete
# Open3 grants you access to stdin, stdout, stderr and a thread to wait for the
[13] Fix | Delete
# child process when running another program.
[14] Fix | Delete
# You can specify various attributes, redirections, current directory, etc., of
[15] Fix | Delete
# the program in the same way as for Process.spawn.
[16] Fix | Delete
#
[17] Fix | Delete
# - Open3.popen3 : pipes for stdin, stdout, stderr
[18] Fix | Delete
# - Open3.popen2 : pipes for stdin, stdout
[19] Fix | Delete
# - Open3.popen2e : pipes for stdin, merged stdout and stderr
[20] Fix | Delete
# - Open3.capture3 : give a string for stdin; get strings for stdout, stderr
[21] Fix | Delete
# - Open3.capture2 : give a string for stdin; get a string for stdout
[22] Fix | Delete
# - Open3.capture2e : give a string for stdin; get a string for merged stdout and stderr
[23] Fix | Delete
# - Open3.pipeline_rw : pipes for first stdin and last stdout of a pipeline
[24] Fix | Delete
# - Open3.pipeline_r : pipe for last stdout of a pipeline
[25] Fix | Delete
# - Open3.pipeline_w : pipe for first stdin of a pipeline
[26] Fix | Delete
# - Open3.pipeline_start : run a pipeline without waiting
[27] Fix | Delete
# - Open3.pipeline : run a pipeline and wait for its completion
[28] Fix | Delete
#
[29] Fix | Delete
[30] Fix | Delete
module Open3
[31] Fix | Delete
[32] Fix | Delete
# Open stdin, stdout, and stderr streams and start external executable.
[33] Fix | Delete
# In addition, a thread to wait for the started process is created.
[34] Fix | Delete
# The thread has a pid method and a thread variable :pid which is the pid of
[35] Fix | Delete
# the started process.
[36] Fix | Delete
#
[37] Fix | Delete
# Block form:
[38] Fix | Delete
#
[39] Fix | Delete
# Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr|
[40] Fix | Delete
# pid = wait_thr.pid # pid of the started process.
[41] Fix | Delete
# ...
[42] Fix | Delete
# exit_status = wait_thr.value # Process::Status object returned.
[43] Fix | Delete
# }
[44] Fix | Delete
#
[45] Fix | Delete
# Non-block form:
[46] Fix | Delete
#
[47] Fix | Delete
# stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts])
[48] Fix | Delete
# pid = wait_thr[:pid] # pid of the started process
[49] Fix | Delete
# ...
[50] Fix | Delete
# stdin.close # stdin, stdout and stderr should be closed explicitly in this form.
[51] Fix | Delete
# stdout.close
[52] Fix | Delete
# stderr.close
[53] Fix | Delete
# exit_status = wait_thr.value # Process::Status object returned.
[54] Fix | Delete
#
[55] Fix | Delete
# The parameters env, cmd, and opts are passed to Process.spawn.
[56] Fix | Delete
# A commandline string and a list of argument strings can be accepted as follows:
[57] Fix | Delete
#
[58] Fix | Delete
# Open3.popen3("echo abc") {|i, o, e, t| ... }
[59] Fix | Delete
# Open3.popen3("echo", "abc") {|i, o, e, t| ... }
[60] Fix | Delete
# Open3.popen3(["echo", "argv0"], "abc") {|i, o, e, t| ... }
[61] Fix | Delete
#
[62] Fix | Delete
# If the last parameter, opts, is a Hash, it is recognized as an option for Process.spawn.
[63] Fix | Delete
#
[64] Fix | Delete
# Open3.popen3("pwd", :chdir=>"/") {|i,o,e,t|
[65] Fix | Delete
# p o.read.chomp #=> "/"
[66] Fix | Delete
# }
[67] Fix | Delete
#
[68] Fix | Delete
# wait_thr.value waits for the termination of the process.
[69] Fix | Delete
# The block form also waits for the process when it returns.
[70] Fix | Delete
#
[71] Fix | Delete
# Closing stdin, stdout and stderr does not wait for the process to complete.
[72] Fix | Delete
#
[73] Fix | Delete
# You should be careful to avoid deadlocks.
[74] Fix | Delete
# Since pipes are fixed length buffers,
[75] Fix | Delete
# Open3.popen3("prog") {|i, o, e, t| o.read } deadlocks if
[76] Fix | Delete
# the program generates too much output on stderr.
[77] Fix | Delete
# You should read stdout and stderr simultaneously (using threads or IO.select).
[78] Fix | Delete
# However, if you don't need stderr output, you can use Open3.popen2.
[79] Fix | Delete
# If merged stdout and stderr output is not a problem, you can use Open3.popen2e.
[80] Fix | Delete
# If you really need stdout and stderr output as separate strings, you can consider Open3.capture3.
[81] Fix | Delete
#
[82] Fix | Delete
def popen3(*cmd, **opts, &block)
[83] Fix | Delete
in_r, in_w = IO.pipe
[84] Fix | Delete
opts[:in] = in_r
[85] Fix | Delete
in_w.sync = true
[86] Fix | Delete
[87] Fix | Delete
out_r, out_w = IO.pipe
[88] Fix | Delete
opts[:out] = out_w
[89] Fix | Delete
[90] Fix | Delete
err_r, err_w = IO.pipe
[91] Fix | Delete
opts[:err] = err_w
[92] Fix | Delete
[93] Fix | Delete
popen_run(cmd, opts, [in_r, out_w, err_w], [in_w, out_r, err_r], &block)
[94] Fix | Delete
end
[95] Fix | Delete
module_function :popen3
[96] Fix | Delete
[97] Fix | Delete
# Open3.popen2 is similar to Open3.popen3 except that it doesn't create a pipe for
[98] Fix | Delete
# the standard error stream.
[99] Fix | Delete
#
[100] Fix | Delete
# Block form:
[101] Fix | Delete
#
[102] Fix | Delete
# Open3.popen2([env,] cmd... [, opts]) {|stdin, stdout, wait_thr|
[103] Fix | Delete
# pid = wait_thr.pid # pid of the started process.
[104] Fix | Delete
# ...
[105] Fix | Delete
# exit_status = wait_thr.value # Process::Status object returned.
[106] Fix | Delete
# }
[107] Fix | Delete
#
[108] Fix | Delete
# Non-block form:
[109] Fix | Delete
#
[110] Fix | Delete
# stdin, stdout, wait_thr = Open3.popen2([env,] cmd... [, opts])
[111] Fix | Delete
# ...
[112] Fix | Delete
# stdin.close # stdin and stdout should be closed explicitly in this form.
[113] Fix | Delete
# stdout.close
[114] Fix | Delete
#
[115] Fix | Delete
# See Process.spawn for the optional hash arguments _env_ and _opts_.
[116] Fix | Delete
#
[117] Fix | Delete
# Example:
[118] Fix | Delete
#
[119] Fix | Delete
# Open3.popen2("wc -c") {|i,o,t|
[120] Fix | Delete
# i.print "answer to life the universe and everything"
[121] Fix | Delete
# i.close
[122] Fix | Delete
# p o.gets #=> "42\n"
[123] Fix | Delete
# }
[124] Fix | Delete
#
[125] Fix | Delete
# Open3.popen2("bc -q") {|i,o,t|
[126] Fix | Delete
# i.puts "obase=13"
[127] Fix | Delete
# i.puts "6 * 9"
[128] Fix | Delete
# p o.gets #=> "42\n"
[129] Fix | Delete
# }
[130] Fix | Delete
#
[131] Fix | Delete
# Open3.popen2("dc") {|i,o,t|
[132] Fix | Delete
# i.print "42P"
[133] Fix | Delete
# i.close
[134] Fix | Delete
# p o.read #=> "*"
[135] Fix | Delete
# }
[136] Fix | Delete
#
[137] Fix | Delete
def popen2(*cmd, **opts, &block)
[138] Fix | Delete
in_r, in_w = IO.pipe
[139] Fix | Delete
opts[:in] = in_r
[140] Fix | Delete
in_w.sync = true
[141] Fix | Delete
[142] Fix | Delete
out_r, out_w = IO.pipe
[143] Fix | Delete
opts[:out] = out_w
[144] Fix | Delete
[145] Fix | Delete
popen_run(cmd, opts, [in_r, out_w], [in_w, out_r], &block)
[146] Fix | Delete
end
[147] Fix | Delete
module_function :popen2
[148] Fix | Delete
[149] Fix | Delete
# Open3.popen2e is similar to Open3.popen3 except that it merges
[150] Fix | Delete
# the standard output stream and the standard error stream.
[151] Fix | Delete
#
[152] Fix | Delete
# Block form:
[153] Fix | Delete
#
[154] Fix | Delete
# Open3.popen2e([env,] cmd... [, opts]) {|stdin, stdout_and_stderr, wait_thr|
[155] Fix | Delete
# pid = wait_thr.pid # pid of the started process.
[156] Fix | Delete
# ...
[157] Fix | Delete
# exit_status = wait_thr.value # Process::Status object returned.
[158] Fix | Delete
# }
[159] Fix | Delete
#
[160] Fix | Delete
# Non-block form:
[161] Fix | Delete
#
[162] Fix | Delete
# stdin, stdout_and_stderr, wait_thr = Open3.popen2e([env,] cmd... [, opts])
[163] Fix | Delete
# ...
[164] Fix | Delete
# stdin.close # stdin and stdout_and_stderr should be closed explicitly in this form.
[165] Fix | Delete
# stdout_and_stderr.close
[166] Fix | Delete
#
[167] Fix | Delete
# See Process.spawn for the optional hash arguments _env_ and _opts_.
[168] Fix | Delete
#
[169] Fix | Delete
# Example:
[170] Fix | Delete
# # check gcc warnings
[171] Fix | Delete
# source = "foo.c"
[172] Fix | Delete
# Open3.popen2e("gcc", "-Wall", source) {|i,oe,t|
[173] Fix | Delete
# oe.each {|line|
[174] Fix | Delete
# if /warning/ =~ line
[175] Fix | Delete
# ...
[176] Fix | Delete
# end
[177] Fix | Delete
# }
[178] Fix | Delete
# }
[179] Fix | Delete
#
[180] Fix | Delete
def popen2e(*cmd, **opts, &block)
[181] Fix | Delete
in_r, in_w = IO.pipe
[182] Fix | Delete
opts[:in] = in_r
[183] Fix | Delete
in_w.sync = true
[184] Fix | Delete
[185] Fix | Delete
out_r, out_w = IO.pipe
[186] Fix | Delete
opts[[:out, :err]] = out_w
[187] Fix | Delete
[188] Fix | Delete
popen_run(cmd, opts, [in_r, out_w], [in_w, out_r], &block)
[189] Fix | Delete
end
[190] Fix | Delete
module_function :popen2e
[191] Fix | Delete
[192] Fix | Delete
def popen_run(cmd, opts, child_io, parent_io) # :nodoc:
[193] Fix | Delete
if last = Hash.try_convert(cmd.last)
[194] Fix | Delete
opts = opts.merge(last)
[195] Fix | Delete
cmd.pop
[196] Fix | Delete
end
[197] Fix | Delete
pid = spawn(*cmd, opts)
[198] Fix | Delete
wait_thr = Process.detach(pid)
[199] Fix | Delete
child_io.each(&:close)
[200] Fix | Delete
result = [*parent_io, wait_thr]
[201] Fix | Delete
if defined? yield
[202] Fix | Delete
begin
[203] Fix | Delete
return yield(*result)
[204] Fix | Delete
ensure
[205] Fix | Delete
parent_io.each(&:close)
[206] Fix | Delete
wait_thr.join
[207] Fix | Delete
end
[208] Fix | Delete
end
[209] Fix | Delete
result
[210] Fix | Delete
end
[211] Fix | Delete
module_function :popen_run
[212] Fix | Delete
class << self
[213] Fix | Delete
private :popen_run
[214] Fix | Delete
end
[215] Fix | Delete
[216] Fix | Delete
# Open3.capture3 captures the standard output and the standard error of a command.
[217] Fix | Delete
#
[218] Fix | Delete
# stdout_str, stderr_str, status = Open3.capture3([env,] cmd... [, opts])
[219] Fix | Delete
#
[220] Fix | Delete
# The arguments env, cmd and opts are passed to Open3.popen3 except
[221] Fix | Delete
# <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>. See Process.spawn.
[222] Fix | Delete
#
[223] Fix | Delete
# If <code>opts[:stdin_data]</code> is specified, it is sent to the command's standard input.
[224] Fix | Delete
#
[225] Fix | Delete
# If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.
[226] Fix | Delete
#
[227] Fix | Delete
# Examples:
[228] Fix | Delete
#
[229] Fix | Delete
# # dot is a command of graphviz.
[230] Fix | Delete
# graph = <<'End'
[231] Fix | Delete
# digraph g {
[232] Fix | Delete
# a -> b
[233] Fix | Delete
# }
[234] Fix | Delete
# End
[235] Fix | Delete
# drawn_graph, dot_log = Open3.capture3("dot -v", :stdin_data=>graph)
[236] Fix | Delete
#
[237] Fix | Delete
# o, e, s = Open3.capture3("echo abc; sort >&2", :stdin_data=>"foo\nbar\nbaz\n")
[238] Fix | Delete
# p o #=> "abc\n"
[239] Fix | Delete
# p e #=> "bar\nbaz\nfoo\n"
[240] Fix | Delete
# p s #=> #<Process::Status: pid 32682 exit 0>
[241] Fix | Delete
#
[242] Fix | Delete
# # generate a thumbnail image using the convert command of ImageMagick.
[243] Fix | Delete
# # However, if the image is really stored in a file,
[244] Fix | Delete
# # system("convert", "-thumbnail", "80", "png:#{filename}", "png:-") is better
[245] Fix | Delete
# # because of reduced memory consumption.
[246] Fix | Delete
# # But if the image is stored in a DB or generated by the gnuplot Open3.capture2 example,
[247] Fix | Delete
# # Open3.capture3 should be considered.
[248] Fix | Delete
# #
[249] Fix | Delete
# image = File.read("/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png", :binmode=>true)
[250] Fix | Delete
# thumbnail, err, s = Open3.capture3("convert -thumbnail 80 png:- png:-", :stdin_data=>image, :binmode=>true)
[251] Fix | Delete
# if s.success?
[252] Fix | Delete
# STDOUT.binmode; print thumbnail
[253] Fix | Delete
# end
[254] Fix | Delete
#
[255] Fix | Delete
def capture3(*cmd, stdin_data: '', binmode: false, **opts)
[256] Fix | Delete
popen3(*cmd, opts) {|i, o, e, t|
[257] Fix | Delete
if binmode
[258] Fix | Delete
i.binmode
[259] Fix | Delete
o.binmode
[260] Fix | Delete
e.binmode
[261] Fix | Delete
end
[262] Fix | Delete
out_reader = Thread.new { o.read }
[263] Fix | Delete
err_reader = Thread.new { e.read }
[264] Fix | Delete
begin
[265] Fix | Delete
if stdin_data.respond_to? :readpartial
[266] Fix | Delete
IO.copy_stream(stdin_data, i)
[267] Fix | Delete
else
[268] Fix | Delete
i.write stdin_data
[269] Fix | Delete
end
[270] Fix | Delete
rescue Errno::EPIPE
[271] Fix | Delete
end
[272] Fix | Delete
i.close
[273] Fix | Delete
[out_reader.value, err_reader.value, t.value]
[274] Fix | Delete
}
[275] Fix | Delete
end
[276] Fix | Delete
module_function :capture3
[277] Fix | Delete
[278] Fix | Delete
# Open3.capture2 captures the standard output of a command.
[279] Fix | Delete
#
[280] Fix | Delete
# stdout_str, status = Open3.capture2([env,] cmd... [, opts])
[281] Fix | Delete
#
[282] Fix | Delete
# The arguments env, cmd and opts are passed to Open3.popen3 except
[283] Fix | Delete
# <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>. See Process.spawn.
[284] Fix | Delete
#
[285] Fix | Delete
# If <code>opts[:stdin_data]</code> is specified, it is sent to the command's standard input.
[286] Fix | Delete
#
[287] Fix | Delete
# If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.
[288] Fix | Delete
#
[289] Fix | Delete
# Example:
[290] Fix | Delete
#
[291] Fix | Delete
# # factor is a command for integer factorization.
[292] Fix | Delete
# o, s = Open3.capture2("factor", :stdin_data=>"42")
[293] Fix | Delete
# p o #=> "42: 2 3 7\n"
[294] Fix | Delete
#
[295] Fix | Delete
# # generate x**2 graph in png using gnuplot.
[296] Fix | Delete
# gnuplot_commands = <<"End"
[297] Fix | Delete
# set terminal png
[298] Fix | Delete
# plot x**2, "-" with lines
[299] Fix | Delete
# 1 14
[300] Fix | Delete
# 2 1
[301] Fix | Delete
# 3 8
[302] Fix | Delete
# 4 5
[303] Fix | Delete
# e
[304] Fix | Delete
# End
[305] Fix | Delete
# image, s = Open3.capture2("gnuplot", :stdin_data=>gnuplot_commands, :binmode=>true)
[306] Fix | Delete
#
[307] Fix | Delete
def capture2(*cmd, stdin_data: nil, binmode: false, **opts)
[308] Fix | Delete
popen2(*cmd, opts) {|i, o, t|
[309] Fix | Delete
if binmode
[310] Fix | Delete
i.binmode
[311] Fix | Delete
o.binmode
[312] Fix | Delete
end
[313] Fix | Delete
out_reader = Thread.new { o.read }
[314] Fix | Delete
if stdin_data
[315] Fix | Delete
begin
[316] Fix | Delete
if stdin_data.respond_to? :readpartial
[317] Fix | Delete
IO.copy_stream(stdin_data, i)
[318] Fix | Delete
else
[319] Fix | Delete
i.write stdin_data
[320] Fix | Delete
end
[321] Fix | Delete
rescue Errno::EPIPE
[322] Fix | Delete
end
[323] Fix | Delete
end
[324] Fix | Delete
i.close
[325] Fix | Delete
[out_reader.value, t.value]
[326] Fix | Delete
}
[327] Fix | Delete
end
[328] Fix | Delete
module_function :capture2
[329] Fix | Delete
[330] Fix | Delete
# Open3.capture2e captures the standard output and the standard error of a command.
[331] Fix | Delete
#
[332] Fix | Delete
# stdout_and_stderr_str, status = Open3.capture2e([env,] cmd... [, opts])
[333] Fix | Delete
#
[334] Fix | Delete
# The arguments env, cmd and opts are passed to Open3.popen3 except
[335] Fix | Delete
# <code>opts[:stdin_data]</code> and <code>opts[:binmode]</code>. See Process.spawn.
[336] Fix | Delete
#
[337] Fix | Delete
# If <code>opts[:stdin_data]</code> is specified, it is sent to the command's standard input.
[338] Fix | Delete
#
[339] Fix | Delete
# If <code>opts[:binmode]</code> is true, internal pipes are set to binary mode.
[340] Fix | Delete
#
[341] Fix | Delete
# Example:
[342] Fix | Delete
#
[343] Fix | Delete
# # capture make log
[344] Fix | Delete
# make_log, s = Open3.capture2e("make")
[345] Fix | Delete
#
[346] Fix | Delete
def capture2e(*cmd, stdin_data: nil, binmode: false, **opts)
[347] Fix | Delete
popen2e(*cmd, opts) {|i, oe, t|
[348] Fix | Delete
if binmode
[349] Fix | Delete
i.binmode
[350] Fix | Delete
oe.binmode
[351] Fix | Delete
end
[352] Fix | Delete
outerr_reader = Thread.new { oe.read }
[353] Fix | Delete
if stdin_data
[354] Fix | Delete
begin
[355] Fix | Delete
if stdin_data.respond_to? :readpartial
[356] Fix | Delete
IO.copy_stream(stdin_data, i)
[357] Fix | Delete
else
[358] Fix | Delete
i.write stdin_data
[359] Fix | Delete
end
[360] Fix | Delete
rescue Errno::EPIPE
[361] Fix | Delete
end
[362] Fix | Delete
end
[363] Fix | Delete
i.close
[364] Fix | Delete
[outerr_reader.value, t.value]
[365] Fix | Delete
}
[366] Fix | Delete
end
[367] Fix | Delete
module_function :capture2e
[368] Fix | Delete
[369] Fix | Delete
# Open3.pipeline_rw starts a list of commands as a pipeline with pipes
[370] Fix | Delete
# which connect to stdin of the first command and stdout of the last command.
[371] Fix | Delete
#
[372] Fix | Delete
# Open3.pipeline_rw(cmd1, cmd2, ... [, opts]) {|first_stdin, last_stdout, wait_threads|
[373] Fix | Delete
# ...
[374] Fix | Delete
# }
[375] Fix | Delete
#
[376] Fix | Delete
# first_stdin, last_stdout, wait_threads = Open3.pipeline_rw(cmd1, cmd2, ... [, opts])
[377] Fix | Delete
# ...
[378] Fix | Delete
# first_stdin.close
[379] Fix | Delete
# last_stdout.close
[380] Fix | Delete
#
[381] Fix | Delete
# Each cmd is a string or an array.
[382] Fix | Delete
# If it is an array, the elements are passed to Process.spawn.
[383] Fix | Delete
#
[384] Fix | Delete
# cmd:
[385] Fix | Delete
# commandline command line string which is passed to a shell
[386] Fix | Delete
# [env, commandline, opts] command line string which is passed to a shell
[387] Fix | Delete
# [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell)
[388] Fix | Delete
# [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)
[389] Fix | Delete
#
[390] Fix | Delete
# Note that env and opts are optional, as for Process.spawn.
[391] Fix | Delete
#
[392] Fix | Delete
# The options to pass to Process.spawn are constructed by merging
[393] Fix | Delete
# +opts+, the last hash element of the array, and
[394] Fix | Delete
# specifications for the pipes between each of the commands.
[395] Fix | Delete
#
[396] Fix | Delete
# Example:
[397] Fix | Delete
#
[398] Fix | Delete
# Open3.pipeline_rw("tr -dc A-Za-z", "wc -c") {|i, o, ts|
[399] Fix | Delete
# i.puts "All persons more than a mile high to leave the court."
[400] Fix | Delete
# i.close
[401] Fix | Delete
# p o.gets #=> "42\n"
[402] Fix | Delete
# }
[403] Fix | Delete
#
[404] Fix | Delete
# Open3.pipeline_rw("sort", "cat -n") {|stdin, stdout, wait_thrs|
[405] Fix | Delete
# stdin.puts "foo"
[406] Fix | Delete
# stdin.puts "bar"
[407] Fix | Delete
# stdin.puts "baz"
[408] Fix | Delete
# stdin.close # send EOF to sort.
[409] Fix | Delete
# p stdout.read #=> " 1\tbar\n 2\tbaz\n 3\tfoo\n"
[410] Fix | Delete
# }
[411] Fix | Delete
def pipeline_rw(*cmds, **opts, &block)
[412] Fix | Delete
in_r, in_w = IO.pipe
[413] Fix | Delete
opts[:in] = in_r
[414] Fix | Delete
in_w.sync = true
[415] Fix | Delete
[416] Fix | Delete
out_r, out_w = IO.pipe
[417] Fix | Delete
opts[:out] = out_w
[418] Fix | Delete
[419] Fix | Delete
pipeline_run(cmds, opts, [in_r, out_w], [in_w, out_r], &block)
[420] Fix | Delete
end
[421] Fix | Delete
module_function :pipeline_rw
[422] Fix | Delete
[423] Fix | Delete
# Open3.pipeline_r starts a list of commands as a pipeline with a pipe
[424] Fix | Delete
# which connects to stdout of the last command.
[425] Fix | Delete
#
[426] Fix | Delete
# Open3.pipeline_r(cmd1, cmd2, ... [, opts]) {|last_stdout, wait_threads|
[427] Fix | Delete
# ...
[428] Fix | Delete
# }
[429] Fix | Delete
#
[430] Fix | Delete
# last_stdout, wait_threads = Open3.pipeline_r(cmd1, cmd2, ... [, opts])
[431] Fix | Delete
# ...
[432] Fix | Delete
# last_stdout.close
[433] Fix | Delete
#
[434] Fix | Delete
# Each cmd is a string or an array.
[435] Fix | Delete
# If it is an array, the elements are passed to Process.spawn.
[436] Fix | Delete
#
[437] Fix | Delete
# cmd:
[438] Fix | Delete
# commandline command line string which is passed to a shell
[439] Fix | Delete
# [env, commandline, opts] command line string which is passed to a shell
[440] Fix | Delete
# [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell)
[441] Fix | Delete
# [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)
[442] Fix | Delete
#
[443] Fix | Delete
# Note that env and opts are optional, as for Process.spawn.
[444] Fix | Delete
#
[445] Fix | Delete
# Example:
[446] Fix | Delete
#
[447] Fix | Delete
# Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz",
[448] Fix | Delete
# [{"LANG"=>"C"}, "grep", "GET /favicon.ico"],
[449] Fix | Delete
# "logresolve") {|o, ts|
[450] Fix | Delete
# o.each_line {|line|
[451] Fix | Delete
# ...
[452] Fix | Delete
# }
[453] Fix | Delete
# }
[454] Fix | Delete
#
[455] Fix | Delete
# Open3.pipeline_r("yes", "head -10") {|o, ts|
[456] Fix | Delete
# p o.read #=> "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n"
[457] Fix | Delete
# p ts[0].value #=> #<Process::Status: pid 24910 SIGPIPE (signal 13)>
[458] Fix | Delete
# p ts[1].value #=> #<Process::Status: pid 24913 exit 0>
[459] Fix | Delete
# }
[460] Fix | Delete
#
[461] Fix | Delete
def pipeline_r(*cmds, **opts, &block)
[462] Fix | Delete
out_r, out_w = IO.pipe
[463] Fix | Delete
opts[:out] = out_w
[464] Fix | Delete
[465] Fix | Delete
pipeline_run(cmds, opts, [out_w], [out_r], &block)
[466] Fix | Delete
end
[467] Fix | Delete
module_function :pipeline_r
[468] Fix | Delete
[469] Fix | Delete
# Open3.pipeline_w starts a list of commands as a pipeline with a pipe
[470] Fix | Delete
# which connects to stdin of the first command.
[471] Fix | Delete
#
[472] Fix | Delete
# Open3.pipeline_w(cmd1, cmd2, ... [, opts]) {|first_stdin, wait_threads|
[473] Fix | Delete
# ...
[474] Fix | Delete
# }
[475] Fix | Delete
#
[476] Fix | Delete
# first_stdin, wait_threads = Open3.pipeline_w(cmd1, cmd2, ... [, opts])
[477] Fix | Delete
# ...
[478] Fix | Delete
# first_stdin.close
[479] Fix | Delete
#
[480] Fix | Delete
# Each cmd is a string or an array.
[481] Fix | Delete
# If it is an array, the elements are passed to Process.spawn.
[482] Fix | Delete
#
[483] Fix | Delete
# cmd:
[484] Fix | Delete
# commandline command line string which is passed to a shell
[485] Fix | Delete
# [env, commandline, opts] command line string which is passed to a shell
[486] Fix | Delete
# [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell)
[487] Fix | Delete
# [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)
[488] Fix | Delete
#
[489] Fix | Delete
# Note that env and opts are optional, as for Process.spawn.
[490] Fix | Delete
#
[491] Fix | Delete
# Example:
[492] Fix | Delete
#
[493] Fix | Delete
# Open3.pipeline_w("bzip2 -c", :out=>"/tmp/hello.bz2") {|i, ts|
[494] Fix | Delete
# i.puts "hello"
[495] Fix | Delete
# }
[496] Fix | Delete
#
[497] Fix | Delete
def pipeline_w(*cmds, **opts, &block)
[498] Fix | Delete
in_r, in_w = IO.pipe
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function