[Groonga-commit] groonga/groonga at e6afb37 [master] doc: support putting output from command

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Apr 14 19:01:49 JST 2016


Kouhei Sutou	2016-04-14 19:01:49 +0900 (Thu, 14 Apr 2016)

  New Revision: e6afb376b52ccb06d4186d4a64b67f91f4a4e9b0
  https://github.com/groonga/groonga/commit/e6afb376b52ccb06d4186d4a64b67f91f4a4e9b0

  Message:
    doc: support putting output from command

  Modified files:
    doc/update_execution_example.py

  Modified: doc/update_execution_example.py (+20 -9)
===================================================================
--- doc/update_execution_example.py    2016-04-14 19:01:03 +0900 (94bc024)
+++ doc/update_execution_example.py    2016-04-14 19:01:49 +0900 (5e17c0a)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8; -*- 
 
-from subprocess import *
+import subprocess
 from select import select
 from sys import argv,stdout
 import os
@@ -32,13 +32,16 @@ def reconnect(name):
   close_groonga()
   current_db_path = os.path.join(DB_DIRECTORY, name)
   if os.path.exists(current_db_path):
-    groonga_process = Popen(["groonga", current_db_path],
-                            stdin=PIPE,
-                            stdout=PIPE)
+    groonga_process = subprocess.Popen(["groonga", current_db_path],
+                                       stdin=subprocess.PIPE,
+                                       stdout=subprocess.PIPE)
   else:
-    groonga_process = Popen(["groonga", "-n", current_db_path],
-                            stdin=PIPE,
-                            stdout=PIPE)
+    groonga_process = subprocess.Popen(["groonga", "-n", current_db_path],
+                                       stdin=subprocess.PIPE,
+                                       stdout=subprocess.PIPE)
+  groonga_process.stdin.write("status\n")
+  groonga_process.stdin.flush()
+  groonga_process.stdout.readline()
   print '###>>> database: open <%s>' % current_db_path
 
 def expand_command_line(command_line):
@@ -180,9 +183,17 @@ def readfile(fname, outflag):
         elif cmd.startswith('.. % '):
           command_line = cmd[5:]
           if fout:
-            fout.write("  " + command_line + "\n")
+            fout.write("  % " + command_line + "\n")
           print command_line
-          os.system(expand_command_line(command_line))
+          expanded_command_line = expand_command_line(command_line)
+          command_output = subprocess.check_output(expanded_command_line,
+                                                   shell=True)
+          if fout:
+            first_lines_re = re.compile("^", re.M)
+            fout.write(first_lines_re.sub("  ", command_output.strip()))
+            fout.write("\n")
+          else:
+            print command_output
         elif cmd.startswith('.. .. '):
           command_line = cmd[6:]
           if fout:
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index