4 from subprocess
import *
5 from select
import select
6 from sys
import argv,stdout
13 DB_DIRECTORY =
"/tmp/groonga-databases"
14 DEFAULT_DB_NAME =
"tutorial.db"
16 shutil.rmtree(DB_DIRECTORY, ignore_errors=
True)
17 os.makedirs(DB_DIRECTORY)
19 groonga_process =
None
21 global groonga_process
23 groonga_process.stdin.close()
24 groonga_process.stdout.close()
25 groonga_process =
None
26 print '###<<< database: close'
29 global groonga_process
31 db_path = os.path.join(DB_DIRECTORY, name)
32 if os.path.exists(db_path):
33 groonga_process = Popen([
"groonga", db_path], stdin=PIPE, stdout=PIPE)
35 groonga_process = Popen([
"groonga",
"-n", db_path], stdin=PIPE, stdout=PIPE)
36 print '###>>> database: open <%s>' % db_path
43 normalized_start_time = 1337566253.89858
44 normalized_elapsed_time = 0.000355720520019531
45 status[1] = normalized_start_time
46 status[2] = normalized_elapsed_time
50 stdout.write(command +
"\n")
52 groonga_process.stdin.write(command +
"\n")
53 groonga_process.stdin.flush()
54 is_command = re.match(
"[a-z/]", command)
55 is_load_command = re.match(
"load ", command)
56 is_console =
not re.match(
"/", command)
61 prefix =
" % curl http://localhost:10041"
62 formatted_command_line = prefix + command +
"\n"
63 fout.write(formatted_command_line)
64 is_load_data_end = re.match(
"^\]", command)
67 if not is_command
and not is_load_data_end:
72 timeout = first_timeout
74 out = select([groonga_process.stdout], [], [], timeout)
75 timeout = rest_timeout
77 char = groonga_process.stdout.read(1)
79 stdout.write(output_buffer)
81 fout.write(output_buffer)
85 parsed_output = json.loads(output_buffer)
87 if len(output_buffer) < 80:
88 formatted_output = json.dumps(normalized_output,
91 formatted_output = json.dumps(normalized_output,
94 formatted_output +=
"\n"
95 formatted_output = formatted_output.encode(
"utf-8")
96 stdout.write(formatted_output)
103 first_lines_re = re.compile(
"^", re.M)
104 fout.write(first_lines_re.sub(prefix, formatted_output.strip()))
113 if fname
in processed_files:
114 print "skipped processed file: %s" % fname
117 print "!!!! INCLUDE DEPTH OVER !!!!"
119 processed_files.append(fname)
125 rootdir = fname[0:b+1]
127 fi = open(fname,
'r')
128 dat = fi.read().split("\n")
134 if cmd.startswith(
'.. groonga-command'):
135 print '### command start'
139 if cmd.startswith(
'.. database:'):
140 database_name = cmd[cmd.index(
":")+1:].strip()
142 elif cmd.startswith(
'.. include:: '):
143 a = rootdir + cmd[13:]
144 dir_name = os.path.dirname(a)
145 if not os.path.exists(dir_name):
146 os.makedirs(dir_name)
148 print '### write start : ' + a
149 fout.write(
"Execution example::\n\n")
150 elif cmd.startswith(
'.. % '):
151 command_line = cmd[5:]
153 fout.write(
" " + command_line +
"\n")
155 os.system(command_line)
156 elif cmd.startswith(
'.. .. '):
157 command_line = cmd[6:]
159 fout.write(
" " + command_line +
"\n")
161 elif cmd.startswith(
'..'):
162 if cmd.replace(
' ',
'').replace(
"\t",
'') ==
'..':
164 if dat[0] ==
'' or (dat[0][0] !=
' ' and dat[0][0] !=
' '):
171 print '### command end'
175 elif cmd.startswith(
'.. groonga-include : '):
176 a = rootdir + cmd[21:]
177 print '###>>> include : ' + a
179 print '###<<< include end'
181 entry_point =
"source/"
183 entry_point = argv[1]
184 if os.path.isfile(entry_point):
187 for root, dirs, files
in os.walk(entry_point):
189 if fname.lower().endswith(
'.txt'):
190 b = os.path.join(root, fname)