ruby-changes:9081
From: akr <ko1@a...>
Date: Thu, 11 Dec 2008 02:24:57 +0900 (JST)
Subject: [ruby-changes:9081] Ruby:r20618 (trunk): * lib/open3.rb (Open3.pipeline_start): return an array of threads if a
akr 2008-12-11 02:24:42 +0900 (Thu, 11 Dec 2008) New Revision: 20618 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20618 Log: * lib/open3.rb (Open3.pipeline_start): return an array of threads if a block is not given. Modified files: trunk/ChangeLog trunk/lib/open3.rb trunk/test/test_open3.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 20617) +++ ChangeLog (revision 20618) @@ -1,3 +1,8 @@ +Thu Dec 11 02:23:51 2008 Tanaka Akira <akr@f...> + + * lib/open3.rb (Open3.pipeline_start): return an array of threads if a + block is not given. + Thu Dec 11 01:48:00 2008 Yukihiro Matsumoto <matz@r...> * ext/bigdecimal/bigdecimal.c (BigDecimal_round): should be round Index: lib/open3.rb =================================================================== --- lib/open3.rb (revision 20617) +++ lib/open3.rb (revision 20618) @@ -11,6 +11,8 @@ # # Open3 grants you access to stdin, stdout, stderr and a thread to wait the # child process when running another program. +# You can specify various attributes, redirections, current directory, etc., of +# the program as Process.spawn. # # - Open3.popen3 : pipes for stdin, stdout, stderr # - Open3.popen2 : pipes for stdin, stdout @@ -41,7 +43,7 @@ # } # # Non-block form: - # + # # stdin, stdout, stderr, wait_thr = Open3.popen3([env,] cmd... [, opts]) # pid = wait_thr[:pid] # pid of the started process. # ... @@ -50,14 +52,14 @@ # stderr.close # exit_status = wait_thr.value # Process::Status object returned. # - # The parameters +cmd...+ is passed to Kernel#spawn. + # The parameters +cmd...+ is passed to Process.spawn. # So a commandline string and list of argument strings can be accepted as follows. # # Open3.popen3("echo a") {|i, o, e, t| ... } # Open3.popen3("echo", "a") {|i, o, e, t| ... } # Open3.popen3(["echo", "argv0"], "a") {|i, o, e, t| ... } # - # If the last parameter, opts, is a Hash, it is recognized as an option for Kernel#spawn. + # If the last parameter, opts, is a Hash, it is recognized as an option for Process.spawn. # # Open3.popen3("pwd", :chdir=>"/") {|i,o,e,t| # p o.read.chomp #=> "/" @@ -101,12 +103,14 @@ # } # # Non-block form: - # + # # stdin, stdout, wait_thr = Open3.popen2([env,] cmd... [, opts]) # ... # stdin.close # stdin and stdout should be closed explicitly in this form. # stdout.close # + # See Process.spawn for the optional hash arguments _env_ and _opts_. + # # Example: # # Open3.popen2("wc -c") {|i,o,t| @@ -115,14 +119,14 @@ # p o.gets #=> "42\n" # } # - # Open3.popen2("bc -q") {|i,o,t| + # Open3.popen2("bc -q") {|i,o,t| # i.puts "obase=13" # i.puts "6 * 9" # p o.gets #=> "42\n" # } # # Open3.popen2("dc") {|i,o,t| - # i.print "42P" + # i.print "42P" # i.close # p o.read #=> "*" # } @@ -157,12 +161,14 @@ # } # # Non-block form: - # + # # stdin, stdout_and_stderr, wait_thr = Open3.popen2e([env,] cmd... [, opts]) # ... # stdin.close # stdin and stdout_and_stderr should be closed explicitly in this form. # stdout_and_stderr.close # + # See Process.spawn for the optional hash arguments _env_ and _opts_. + # # Example: # # check gcc warnings # source = "foo.c" @@ -216,7 +222,8 @@ # # stdout_str, stderr_str, status = Open3.capture3([env,] cmd... [, opts]) # - # The arguments cmd and opts are passed to Open3.popen3 except opts[:stdin_data]. + # The arguments env, cmd and opts are passed to Open3.popen3 except + # opts[:stdin_data] and opts[:stdin_data]. See Process.spawn. # # If opts[:stdin_data] is specified, it is sent to the command's standard input. # @@ -279,7 +286,8 @@ # # stdout_str, status = Open3.capture2([env,] cmd... [, opts]) # - # The arguments cmd and opts are passed to Open3.popen2 except opts[:stdin_data]. + # The arguments env, cmd and opts are passed to Open3.popen3 except + # opts[:stdin_data] and opts[:stdin_data]. See Process.spawn. # # If opts[:stdin_data] is specified, it is sent to the command's standard input. # @@ -288,7 +296,7 @@ # Example: # # # factor is a command for integer factorization. - # o, s = Open3.capture2("factor", :stdin_data=>"42") + # o, s = Open3.capture2("factor", :stdin_data=>"42") # p o #=> "42: 2 3 7\n" # # # generate x**2 graph in png using gnuplot. @@ -296,12 +304,12 @@ # set terminal png # plot x**2, "-" with lines # 1 14 - # 2 1 + # 2 1 # 3 8 # 4 5 # e # End - # image, s = Open3.capture2("gnuplot", :stdin_data=>gnuplot_commands, :binmode=>true) + # image, s = Open3.capture2("gnuplot", :stdin_data=>gnuplot_commands, :binmode=>true) # def capture2(*cmd, &block) if Hash === cmd.last @@ -330,7 +338,8 @@ # # stdout_and_stderr_str, status = Open3.capture2e([env,] cmd... [, opts]) # - # The arguments cmd and opts are passed to Open3.popen2e except opts[:stdin_data]. + # The arguments env, cmd and opts are passed to Open3.popen3 except + # opts[:stdin_data] and opts[:stdin_data]. See Process.spawn. # # If opts[:stdin_data] is specified, it is sent to the command's standard input. # @@ -377,9 +386,17 @@ # last_stdout.close # # Each cmd is a string or an array. - # If it is an array, the elements are passed to Kernel#spawn. + # If it is an array, the elements are passed to Process.spawn. # - # The option to pass Kernel#spawn is constructed by merging + # cmd: + # commandline command line string which is passed to a shell + # [env, commandline, opts] command line string which is passed to a shell + # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) + # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) + # + # Note that env and opts are optional, as Process.spawn. + # + # The option to pass Process.spawn is constructed by merging # +opts+, the last hash element of the array and # specification for the pipe between each commands. # @@ -427,6 +444,17 @@ # ... # last_stdout.close # + # Each cmd is a string or an array. + # If it is an array, the elements are passed to Process.spawn. + # + # cmd: + # commandline command line string which is passed to a shell + # [env, commandline, opts] command line string which is passed to a shell + # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) + # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) + # + # Note that env and opts are optional, as Process.spawn. + # # Example: # # Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz", @@ -468,10 +496,21 @@ # ... # first_stdin.close # + # Each cmd is a string or an array. + # If it is an array, the elements are passed to Process.spawn. + # + # cmd: + # commandline command line string which is passed to a shell + # [env, commandline, opts] command line string which is passed to a shell + # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) + # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) + # + # Note that env and opts are optional, as Process.spawn. + # # Example: # # Open3.pipeline_w("bzip2 -c", :out=>"/tmp/hello.bz2") {|w, ts| - # w.puts "hello" + # w.puts "hello" # } # def pipeline_w(*cmds, &block) @@ -500,16 +539,39 @@ # wait_threads = Open3.pipeline_start(cmd1, cmd2, ... [, opts]) # ... # + # Each cmd is a string or an array. + # If it is an array, the elements are passed to Process.spawn. + # + # cmd: + # commandline command line string which is passed to a shell + # [env, commandline, opts] command line string which is passed to a shell + # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) + # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) + # + # Note that env and opts are optional, as Process.spawn. + # # Example: # # # run xeyes in 10 seconds. - # Open3.pipeline_start("xeyes") {|ts| + # Open3.pipeline_start("xeyes") {|ts| # sleep 10 # t = ts[0] - # Process.kill("TERM", t.pid) + # Process.kill("TERM", t.pid) # p t.value #=> #<Process::Status: pid 911 SIGTERM (signal 15)> # } # + # # convert pdf to ps and send it to a printer. + # # collect error message of pdftops and lpr. + # pdf_file = "paper.pdf" + # printer = "printer-name" + # err_r, err_w = IO.pipe + # Open3.pipeline_start(["pdftops", pdf_file, "-"], + # ["lpr", "-P#{printer}"], + # :err=>err_w) {|ts| + # err_w.close + # p err_r.read # error messages of pdftops and lpr. + # } + # def pipeline_start(*cmds, &block) if Hash === cmds.last opts = cmds.pop.dup @@ -517,7 +579,12 @@ opts = {} end - pipeline_run(cmds, opts, [], [], &block) + if block + pipeline_run(cmds, opts, [], [], &block) + else + ts, = pipeline_run(cmds, opts, [], []) + ts + end end module_function :pipeline_start @@ -528,10 +595,21 @@ # # status_list = Open3.pipeline(cmd1, cmd2, ... [, opts]) # + # Each cmd is a string or an array. + # If it is an array, the elements are passed to Process.spawn. + # + # cmd: + # commandline command line string which is passed to a shell + # [env, commandline, opts] command line string which is passed to a shell + # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell) + # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell) + # + # Note that env and opts are optional, as Process.spawn. + # # Example: # # fname = "/usr/share/man/man1/ruby.1.gz" - # p Open3.pipeline(["zcat", fname], "nroff -man", "less") + # p Open3.pipeline(["zcat", fname], "nroff -man", "less") # #=> [#<Process::Status: pid 11817 exit 0>, # # #<Process::Status: pid 11820 exit 0>, # # #<Process::Status: pid 11828 exit 0>] Index: test/test_open3.rb =================================================================== --- test/test_open3.rb (revision 20617) +++ test/test_open3.rb (revision 20618) @@ -214,6 +214,15 @@ } end + def test_pipeline_start_noblock + ts = Open3.pipeline_start([RUBY, '-e', '']) + assert_kind_of(Array, ts) + assert_equal(1, ts.length) + ts.each {|t| assert_kind_of(Thread, t) } + t = ts[0] + assert(t.value.success?) + end + def test_pipeline command = [RUBY, '-e', 's=STDIN.read; print s[1..-1]; exit s[0] == ?t'] str = 'ttftff' -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/