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