File: update_execution_example.py

package info (click to toggle)
groonga 11.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 121,576 kB
  • sloc: ansic: 801,209; javascript: 62,121; ruby: 46,206; cpp: 33,790; xml: 24,951; yacc: 13,333; sh: 7,776; python: 3,266; makefile: 2,315; perl: 133
file content (277 lines) | stat: -rwxr-xr-x 8,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# -*- coding: utf-8; -*- 

import subprocess
from select import select
from sys import argv,stdout
import os
import os.path
import shutil
import re
import json

DB_DIRECTORY = "/tmp/groonga-databases"
DEFAULT_DB_NAME = "tutorial.db"

shutil.rmtree(DB_DIRECTORY, ignore_errors=True)
os.makedirs(DB_DIRECTORY)

groonga_process = None
current_log = None
output_log = False
def close_groonga():
  global groonga_process
  global current_log
  global output_log
  if groonga_process:
    groonga_process.stdin.close()
    groonga_process.stdout.close()
    groonga_process = None
    print('###<<< database: close')
  if current_log:
    current_log.close()
    current_log = None
  output_log = False

current_db_path = None
current_log_path = None
def reconnect(name):
  global groonga_process
  global current_log
  global current_db_path
  global current_log_path
  close_groonga()
  current_db_path = os.path.join(DB_DIRECTORY, name)
  current_log_path = os.path.join(DB_DIRECTORY, name + ".log")
  if os.path.exists(current_db_path):
    groonga_process = subprocess.Popen(["groonga",
                                        "--log-path", current_log_path,
                                        current_db_path],
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE)
  else:
    groonga_process = subprocess.Popen(["groonga",
                                        "--log-path", current_log_path,
                                        "-n",
                                        current_db_path],
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE)
  groonga_process.stdin.write("status\n")
  groonga_process.stdin.flush()
  groonga_process.stdout.readline()
  current_log = open(current_log_path)
  current_log.read()
  print('###>>> database: open <%s>' % current_db_path)

def expand_command_line(command_line):
  return command_line.replace('${DB_PATH}', current_db_path)

fout = None

def normalize_file_name(file_name):
  return re.sub('^.*?/lib/', 'lib/', file_name)

def normalize_output(output):
  status = output[0]
  if status:
    normalized_start_time = 1337566253.89858
    normalized_elapsed_time = 0.000355720520019531
    status[1] = normalized_start_time
    status[2] = normalized_elapsed_time
    return_code = status[0]
    if return_code != 0:
      backtraces = status[4]
      if backtraces:
        for backtrace in backtraces:
          file_name = backtrace[1]
          backtrace[1] = normalize_file_name(file_name)
  return output

def remove_continuous_marks(command):
  return re.sub('\\\\\n', '', command)

def prepend_prefix(prefix, command):
  beginning_of_line = re.compile('^', re.M)
  return beginning_of_line.sub(prefix, command)

def execmd(command, fout):
  stdout.write(command + "\n")
  stdout.flush()
  groonga_process.stdin.write(remove_continuous_marks(command) + "\n")
  groonga_process.stdin.flush()
  is_command = re.match("[a-z/]", command)
  is_load_command = re.match("load ", command)
  is_console = not re.match("/", command)
  if fout:
    if is_console:
      prefix = "  "
    else:
      prefix = "  % curl http://localhost:10041"
    formatted_command_line = prepend_prefix(prefix, command) + "\n"
    fout.write(formatted_command_line)
  is_load_data_end = re.match("^\]", command)
  if is_load_command:
    if not re.search(" --values ", command):
      return
  if not is_command and not is_load_data_end:
    return
  output_buffer = ""
  first_timeout = 1
  timeout = first_timeout
  while True:
    out = select([groonga_process.stdout], [], [], timeout)
    timeout = rest_timeout
    if len(out[0]):
      char = groonga_process.stdout.read(1)
      if char is None or char == '':
        stdout.write(output_buffer)
        if fout:
          fout.write(output_buffer)
        break
      else:
        output_buffer += char
        if char == '\n':
          if command == 'dump':
            formatted_output = output_buffer
            stdout.write(output_buffer)
          else:
            parsed_output = json.loads(output_buffer)
            normalized_output = normalize_output(parsed_output)
            if len(output_buffer) < 80:
              formatted_output = json.dumps(normalized_output,
                                            ensure_ascii=False)
            else:
              formatted_output = json.dumps(normalized_output,
                                            indent=2,
                                            ensure_ascii=False)
              formatted_output += "\n"
              formatted_output = formatted_output.encode("utf-8")
            stdout.write(formatted_output)
            stdout.write("\n")
          if fout:
            if is_console:
              prefix = "  # "
            else:
              prefix = "  "
            first_lines_re = re.compile("^", re.M)
            fout.write(first_lines_re.sub(prefix, formatted_output.strip()))
            fout.write("\n")
          if current_log:
            prefix = "  # log: "
            for line in current_log.readlines():
                formatted_line = prefix + line.strip()
                stdout.write(formatted_line)
                stdout.write("\n")
                if output_log and fout:
                    fout.write(formatted_line)
                    fout.write("\n")
          output_buffer = ""
    else:
      stdout.flush()
      break

processed_files = []
def readfile(fname, outflag):
  if fname in processed_files:
    print("skipped processed file: %s" % fname)
    return
  if outflag > 32:
    print("!!!! INCLUDE DEPTH OVER !!!!")
    raise
  processed_files.append(fname)

  b = fname.rfind('/')
  if b < 0:
    rootdir = './'
  else:
    rootdir = fname[0:b+1]

  fi = open(fname, 'r')
  dat = fi.read().split("\n")
  fi.close()

  line = 0;
  while len(dat):
    cmd = dat.pop(0)
    if cmd.startswith('.. groonga-command'):
      print('### command start')
      fout = None
      while len(dat):
        cmd = dat.pop(0)
        if cmd.startswith('.. database:'):
          database_name = cmd[cmd.index(":")+1:].strip()
          reconnect(database_name)
        elif cmd.startswith('.. log:'):
          global output_log
          log_value = cmd[cmd.index(":")+1:].strip()
          output_log = log_value == "true"
        elif cmd.startswith('.. include:: '):
          a = rootdir + cmd[13:]
          dir_name = os.path.dirname(a)
          if not os.path.exists(dir_name):
            os.makedirs(dir_name)
          fout = open(a, 'w')
          print('### write start : ' + a)
          fout.write("Execution example::\n\n")
        elif cmd.startswith('.. % '):
          command_line = cmd[5:]
          if fout:
            fout.write("  % " + command_line + "\n")
          print(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:
            fout.write("  " + command_line + "\n")
          print(command_line)
        elif cmd.startswith('..'):
          if cmd.replace(' ', '').replace("\t", '') == '..':
            while len(dat):
              if dat[0] == '' or (dat[0][0] != ' ' and dat[0][0] != '	'):
                break
              execmd(dat.pop(0), fout)
          else:
            cmd = cmd[3:]
            while cmd.endswith('\\'):
              cmd += '\n' + re.sub('^\.\. ', '', dat.pop(0))
            execmd(cmd, fout)
        else:
          print('### command end')
          if fout:
            fout.close()
          break
    elif cmd.startswith('.. groonga-include : '):
      a = rootdir + cmd[21:]
      print('###>>> include : ' + a)
      readfile(a, outflag + 1)
      print('###<<< include end')

entry_point = "source/"
rest_timeout = 0.1
if len(argv) == 2:
  entry_point = argv[1]
elif len(argv) == 3:
  entry_point = argv[1]
  rest_timeout = float(argv[2])
if os.path.isfile(entry_point):
  readfile(entry_point, 0)
else:
  for root, dirs, files in os.walk(entry_point):
    for fname in files:
      if fname.lower().endswith('.rst'):
        b = os.path.join(root, fname)
        print("===" + b)
        readfile(b, 0)

if fout:
  fout.close()
close_groonga()