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