Debian Codegraph @

doc/update-execution-example.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # Copyright (C) 2023-2026 Sutou Kouhei <[email protected]>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 require "fileutils"
20 require "find"
21 require "json"
22
23 class Updator
24 def initialize(base_dir)
25 @base_dir = base_dir
26 @verbose = (ENV["VERBOSE"] == "true")
27 end
28
29 def update(source)
30 FileUtils.rm_rf(@base_dir)
31 FileUtils.mkdir_p(@base_dir)
32
33 @processed_files = {}
34 @current_db = nil
35 @output_log = false
36 Find.find(source) do |path|
37 update_file(path)
38 end
39 end
40
41 private
42 def puts(*args)
43 return unless @verbose
44 $stderr.puts(*args)
45 end
46
47 def expand_command_line(command_line)
48 command_line.gsub(/\${DB_PATH}/) do
49 current_db_path
50 end
51 end
52
53 def current_db_path
54 File.join(@base_dir, @current_db)
55 end
56
57 def current_log_path
58 "#{current_db_path}.log"
59 end
60
61 def run_groonga
62 command_line = [
63 "groonga",
64 "--log-path", current_log_path,
65 ]
66 command_line << "-n" unless File.exist?(current_db_path)
67 command_line << current_db_path
68 FileUtils.touch(current_log_path)
69 IO.pipe do |in_read, in_write|
70 IO.pipe do |out_read, out_write|
71 pid = spawn(*command_line, in: in_read, out: out_write)
72 in_read.close
73 out_write.close
74 begin
75 File.open(current_log_path) do |log|
76 # Ensure initializing
77 in_write.puts("status")
78 in_write.flush
79 out_read.gets
80
81 log.seek(0, IO::SEEK_END)
82 @current_log = log
83 yield(in_write, out_read)
84 end
85 ensure
86 @current_log = nil
87 in_write.close unless in_write.closed?
88 out_read.close unless out_read.closed?
89 Process.waitpid(pid)
90 end
91 end
92 end
93 end
94
95 def read_output(output)
96 data = ""
97 timeout = 10
98 while IO.select([output], nil, nil, timeout)
99 break if output.eof?
100 data << output.readpartial(4096)
101 timeout = 0
102 end
103 data
104 end
105
106 def parse_result(command, result)
107 begin
108 JSON.parse(result)
109 rescue JSON::ParserError
110 puts("Failed to parse:")
111 puts("path: #{@current_path}")
112 puts("command:")
113 puts(command)
114 puts("result:")
115 puts(result)
116 raise
117 end
118 end
119
120 def normalize_error_message(message)
121 message.gsub(current_db_path, "${DB_PATH}")
122 end
123
124 def normalize_file_name(file_name)
125 file_name = file_name.gsub(/\A\/.*\/\.\.\/plugins\//, "lib/groonga/plugins/")
126 file_name = file_name.gsub(/\A.*?\/lib\//, "lib/")
127 file_name
128 end
129
130 def normalize_result(command, result)
131 header = result[0]
132 if header
133 normalized_start_time = 1337566253.89858
134 normalized_elapsed_time = 0.000355720520019531
135 header[1] = normalized_start_time
136 header[2] = normalized_elapsed_time
137 header[3] = normalize_error_message(header[3]) if header[3]
138 return_code = header[0]
139 if return_code.zero?
140 if command.start_with?("status") or command.start_with?("/d/status")
141 status = result[1]
142 status["alloc_count"] = 29 if status.key?("alloc_count")
143 status["starttime"] = 1696558618 if status.key?("starttime")
144 status["start_time"] = 1696558618 if status.key?("start_time")
145 status["version"] = "2.9.1" if status.key?("version")
146 apache_arrow = status["apache_arrow"]
147 if apache_arrow
148 if apache_arrow.key?("version_major")
149 apache_arrow["version_major"] = 2
150 end
151 if apache_arrow.key?("version_minor")
152 apache_arrow["version_minor"] = 9
153 end
154 if apache_arrow.key?("version_patch")
155 apache_arrow["version_patch"] = 1
156 end
157 if apache_arrow.key?("version")
158 apache_arrow["version"] = "2.9.1"
159 end
160 end
161 status["memory_map_size"] = 2929 if status.key?("memory_map_size")
162 status["os"] = "Linux" if status.key?("os")
163 status["cpu"] = "x86_64" if status.key?("cpu")
164 end
165 else
166 backtraces = header[4]
167 if backtraces
168 backtraces.each do |backtrace|
169 file_name = backtrace[1]
170 backtrace[1] = normalize_file_name(file_name)
171 # line
172 backtrace[2] = 2929 if backtrace[2]
173 end
174 end
175 end
176 end
177 result
178 end
179
180 def normalize_formatted_result(formatted_result)
181 formatted_result.gsub(current_db_path, "${DB_PATH}")
182 end
183
184 def normalize_log(log)
185 log.gsub(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+/,
186 "2023-10-05 17:26:13.890356")
187 end
188
189 def detect_markup(path)
190 markup = File.extname(path)[1..-1].to_sym
191 markup = :rst if markup == :log
192 markup
193 end
194
195 def execute_command(input, output, command, current_output_path, output_log)
196 input.puts(command)
197 input.flush
198 is_command = /\A[a-z\/]/.match?(command)
199 is_load_command = command.start_with?("load ")
200 is_path_style = command.start_with?("/")
201 puts(command)
202 result = read_output(output)
203 if command.start_with?("dump")
204 formatted_result = result
205 else
206 parsed_result = parse_result(command, result)
207 normalized_result = normalize_result(command, parsed_result)
208 formatted_result = JSON.generate(normalized_result)
209 if formatted_result.bytesize > 79
210 formatted_result = JSON.pretty_generate(normalized_result)
211 end
212 formatted_result = normalize_formatted_result(formatted_result)
213 end
214 puts(formatted_result)
215 if current_output_path
216 markup = detect_markup(current_output_path)
217 File.open(current_output_path, "a") do |o|
218 command_prefix = +""
219 command_prefix << " " if markup == :rst
220 command_prefix << "$ curl http://localhost:10041" if is_path_style
221 o.puts(command.gsub(/^/, command_prefix))
222 output_prefix = +""
223 output_prefix << " " if markup == :rst
224 output_prefix << "# " unless is_path_style
225 o.puts(formatted_result.gsub(/^/, output_prefix))
226
227 if output_log
228 log = normalize_log(read_output(@current_log).strip)
229 unless log.empty?
230 log_prefix = "# log: "
231 puts(log.gsub(/^/, log_prefix))
232 o.puts(log.gsub(/^/, " #{log_prefix}"))
233 end
234 end
235 end
236 end
237 end
238
239 def update_file(path)
240 if @processed_files.key?(path)
241 puts("Skipped processed file: #{path}")
242 return
243 end
244 case File.extname(path)
245 when ".rst"
246 @processed_files[path] = true
247 @current_path = path
248 update_rst(path)
249 when ".md"
250 @processed_files[path] = true
251 @current_path = path
252 update_md(path)
253 end
254 end
255
256 def update_rst(path)
257 groonga_command_block = ""
258 in_groonga_command = false
259 File.read(path).each_line do |line|
260 if in_groonga_command
261 if line.start_with?("..")
262 groonga_command_block << line.gsub(/\A\.\. ?/, "")
263 else
264 in_groonga_command = false
265 process_groonga_command(groonga_command_block)
266 groonga_command_block.clear
267 end
268 else
269 case line
270 when /\A\.\. groonga-command/
271 in_groonga_command = true
272 when /\A\.\. groonga-include\s*:/
273 include_path = line.split(":", 2)[1].strip
274 update_file(File.join(File.dirname(@current_path), include_path))
275 end
276 end
277 end
278 unless groonga_command_block.empty?
279 process_groonga_command(groonga_command_block)
280 end
281 end
282
283 def update_md(path)
284 groonga_command_block = ""
285 in_groonga_command = false
286 in_include = false
287 File.read(path).each_line do |line|
288 if in_groonga_command
289 if in_include
290 if line.chomp == "```"
291 in_groonga_command = false
292 in_include = false
293 process_groonga_command(groonga_command_block)
294 groonga_command_block.clear
295 else
296 groonga_command_block << line
297 end
298 else
299 case line.chomp
300 when /\A```{include} /
301 in_include = true
302 groonga_command_block << line.gsub(/\A```{include} /, "include::")
303 when /\A<!-- database: /
304 @current_db = line.split(":", 2)[1].strip
305 when ""
306 # Ignore
307 else
308 in_groonga_command = false
309 end
310 end
311 else
312 case line.chomp
313 when "<!-- groonga-command -->"
314 in_groonga_command = true
315 in_include = false
316 when /\A<!-- groonga-include\s*:/
317 include_path = line.split(":", 2)[1].strip
318 update_file(File.join(File.dirname(@current_path), include_path))
319 end
320 end
321 end
322 unless groonga_command_block.empty?
323 process_groonga_command(groonga_command_block)
324 end
325 end
326
327 def process_groonga_command(groonga_command)
328 current_output_path = nil
329 markup = nil
330 actions = []
331 command = ""
332 in_load_values = false
333 groonga_command.each_line do |line|
334 case line
335 when /\Adatabase:/
336 @current_db = line.split(":", 2)[1].strip
337 when /\Alog:/
338 actions << {
339 type: :output_log,
340 value: (line.split(":", 2)[1].strip == "true"),
341 }
342 when /\Ainclude::/
343 path = line.split("::", 2)[1].strip
344 base_dir = File.dirname(@current_path)
345 current_output_path = File.join(base_dir, path)
346 markup = detect_markup(current_output_path)
347 puts("### Current output path: #{current_output_path}")
348 FileUtils.mkdir_p(File.dirname(current_output_path))
349 File.open(current_output_path, "w") do |output|
350 if markup == :rst
351 output.puts("Execution example::")
352 output.puts
353 else
354 output.puts("Execution example:")
355 output.puts
356 end
357 end
358 when /\A[%$] /
359 actions << {
360 type: :command_line,
361 value: line.split(/[%$]/, 2)[1].strip,
362 }
363 when /\A\.\. /
364 actions << {
365 type: :comment,
366 value: line.split("..", 2)[1].strip,
367 }
368 when /\A#/
369 actions << {
370 type: :comment,
371 value: line,
372 }
373 else
374 next if command.empty? and line == "\n"
375 command << line
376 next if line.end_with?("\\\n")
377 if in_load_values
378 in_load_values = (line != "]\n")
379 else
380 in_load_values = (command.start_with?("load") and
381 not command.include?(" --values "))
382 end
383 unless in_load_values
384 actions << {
385 type: :command,
386 value: command,
387 }
388 command = ""
389 end
390 end
391 end
392 return if actions.empty?
393
394 in_fenced_code_block = false
395 run_groonga do |input, output|
396 output_log = false
397 actions.each do |action|
398 case action[:type]
399 when :output_log
400 output_log = action[:value]
401 puts("### Output log: #{output_log}")
402 when :command_line
403 command_line = action[:value]
404 expanded_command_line = expand_command_line(command_line)
405 puts(expanded_command_line)
406 command_line_output = `#{expanded_command_line}`
407 if current_output_path
408 File.open(current_output_path, "a") do |output|
409 if markup == :rst
410 output.puts(" $ #{command_line}")
411 output.puts(command_line_output.gsub(/^/, " "))
412 else
413 if in_fenced_code_block
414 output.puts("```")
415 output.puts
416 end
417 output.puts("```console")
418 output.puts("$ #{command_line}")
419 output.puts(command_line_output)
420 output.puts("```")
421 if in_fenced_code_block
422 output.puts
423 output.puts("```shell")
424 end
425 end
426 end
427 end
428 puts(command_line_output)
429 when :comment
430 comment = action[:value]
431 if current_output_path
432 File.open(current_output_path, "a") do |output|
433 if markup == :rst
434 output.puts(comment.gsub(/^/, " "))
435 else
436 output.puts(comment)
437 end
438 end
439 end
440 puts(comment)
441 when :command
442 command = action[:value]
443 if markup == :md and not in_fenced_code_block
444 File.open(current_output_path, "a") do |output|
445 output.puts("```shell")
446 end
447 in_fenced_code_block = true
448 end
449 execute_command(input, output, command, current_output_path, output_log)
450 end
451 end
452 end
453 if markup == :md and in_fenced_code_block
454 File.open(current_output_path, "a") do |output|
455 output.puts("```")
456 end
457 end
458 end
459 end
460
461 if File.directory?("/dev/shm")
462 base_dir = "/dev/shm/groonga-doc"
463 else
464 base_dir = "/tmp/groonga-doc"
465 end
466 updator = Updator.new(base_dir)
467 source = ARGV[0] || ENV["SOURCES"] || File.join(__dir__, "source")
468 updator.update(source)