[前][次][番号順一覧][スレッド一覧]

ruby-changes:9032

From: akr <ko1@a...>
Date: Sun, 7 Dec 2008 11:49:37 +0900 (JST)
Subject: [ruby-changes:9032] Ruby:r20567 (trunk): * lib/open3.rb (Open3.poutput3): :binmode option implemented.

akr	2008-12-07 11:49:18 +0900 (Sun, 07 Dec 2008)

  New Revision: 20567

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20567

  Log:
    * lib/open3.rb (Open3.poutput3): :binmode option implemented.
      (Open3.poutput2): ditto.
      (Open3.poutput2e): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/lib/open3.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 20566)
+++ ChangeLog	(revision 20567)
@@ -1,3 +1,9 @@
+Sun Dec  7 11:48:04 2008  Tanaka Akira  <akr@f...>
+
+	* lib/open3.rb (Open3.poutput3): :binmode option implemented.
+	  (Open3.poutput2): ditto.
+	  (Open3.poutput2e): ditto.
+
 Sat Dec  6 18:33:16 2008  Nobuyoshi Nakada  <nobu@r...>
 
 	* tool/make-snapshot (package): added RM and CP.  [ruby-dev:37288]
Index: lib/open3.rb
===================================================================
--- lib/open3.rb	(revision 20566)
+++ lib/open3.rb	(revision 20567)
@@ -209,6 +209,8 @@
   #
   # If opts[:stdin_data] is specified, it is sent to the command's standard input.
   #
+  # If opts[:binmode] is true, internal pipes are set to binary mode.
+  #
   # Example:
   #
   #   # dot is a command of graphviz.
@@ -224,6 +226,19 @@
   #   p e #=> "bar\nbaz\nfoo\n"
   #   p s #=> #<Process::Status: pid 32682 exit 0>
   #
+  #   # generate a thumnail image using the convert command of ImageMagick.
+  #   # However, if the image stored really in a file,
+  #   # system("convert", "-thumbnail", "80", filename, "png:-") is better
+  #   # because memory consumption.
+  #   # But if the image is stored in a DB or generated by gnuplot Open3.poutput2 example,
+  #   # Open3.poutput3 is considerable.
+  #   #
+  #   image = File.read("/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png", :binmode=>true)
+  #   thumnail, err, s = Open3.poutput3("convert -thumbnail 80 - png:-", :stdin_data=>image, :binmode=>true)
+  #   if s.success?
+  #     STDOUT.binmode; print thumnail
+  #   end
+  #
   def poutput3(*cmd, &block)
     if Hash === cmd.last
       opts = cmd.pop.dup
@@ -232,8 +247,14 @@
     end
 
     stdin_data = opts.delete(:stdin_data) || ''
+    binmode = opts.delete(:binmode)
 
     popen3(*cmd, opts) {|i, o, e, t|
+      if binmode
+        i.binmode
+        o.binmode
+        e.binmode
+      end
       out_reader = Thread.new { o.read }
       err_reader = Thread.new { e.read }
       i.write stdin_data
@@ -251,10 +272,24 @@
   #
   # If opts[:stdin_data] is specified, it is sent to the command's standard input.
   #
-  #   # factor is a command for integer factorization
+  # If opts[:binmode] is true, internal pipes are set to binary mode.
+  #
+  #   # factor is a command for integer factorization.
   #   o, s = Open3.poutput2("factor", :stdin_data=>"42")    
   #   p o #=> "42: 2 3 7\n"
   #
+  #   # generate x**2 graph in png using gnuplot.
+  #   gnuplot_commands = <<"End"
+  #     set terminal png
+  #     plot x**2, "-" with lines
+  #     1 14
+  #     2 1 
+  #     3 8
+  #     4 5
+  #     e
+  #   End
+  #   image, s = Open3.poutput2("gnuplot", :stdin_data=>gnuplot_commands, :binmode=>true)        
+  #
   def poutput2(*cmd, &block)
     if Hash === cmd.last
       opts = cmd.pop.dup
@@ -263,8 +298,13 @@
     end
 
     stdin_data = opts.delete(:stdin_data) || ''
+    binmode = opts.delete(:binmode)
 
     popen2(*cmd, opts) {|i, o, t|
+      if binmode
+        i.binmode
+        o.binmode
+      end
       out_reader = Thread.new { o.read }
       i.write stdin_data
       i.close
@@ -281,6 +321,13 @@
   #
   # If opts[:stdin_data] is specified, it is sent to the command's standard input.
   #
+  # If opts[:binmode] is true, internal pipes are set to binary mode.
+  #
+  # Example:
+  #
+  #   # capture make log
+  #   make_log, s = Open3.poutput2e("make")
+  #
   def poutput2e(*cmd, &block)
     if Hash === cmd.last
       opts = cmd.pop.dup
@@ -289,8 +336,13 @@
     end
 
     stdin_data = opts.delete(:stdin_data) || ''
+    binmode = opts.delete(:binmode)
 
     popen2e(*cmd, opts) {|i, oe, t|
+      if binmode
+        i.binmode
+        oe.binmode
+      end
       outerr_reader = Thread.new { oe.read }
       i.write stdin_data
       i.close
@@ -364,11 +416,6 @@
   #
   # Example:
   #
-  #   fname = "/usr/share/man/man1/ls.1.gz"
-  #   Open3.pipeline_r(["zcat", fname], "nroff -man", "colcrt") {|r, ts|
-  #     IO.copy_stream(r, STDOUT)
-  #   }
-  #
   #   Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz",
   #                    [{"LANG"=>"C"}, "grep", "GET /favicon.ico"],
   #                    "logresolve") {|r, ts|
@@ -476,6 +523,15 @@
   #   #    #<Process::Status: pid 11820 exit 0>,
   #   #    #<Process::Status: pid 11828 exit 0>]
   #
+  #   fname = "/usr/share/man/man1/ls.1.gz"
+  #   Open3.pipeline(["zcat", fname], "nroff -man", "colcrt")
+  #
+  #   # convert PDF to PS and send to a printer by lpr
+  #   pdf_file = "paper.pdf"
+  #   printer = "printer-name"
+  #   Open3.pipeline(["pdftops", pdf_file, "-"],
+  #                  ["lpr", "-P#{printer}"])
+  #
   #   # count lines
   #   Open3.pipeline("sort", "uniq -c", :in=>"names.txt", :out=>"count")
   #

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]