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