| 1 |
|
| 2 |
|
| 3 |
unless respond_to?(:spawn, true) |
| 4 |
puts("Ruby 1.9 is required.") |
| 5 |
exit(false) |
| 6 |
end |
| 7 |
|
| 8 |
require 'ostruct' |
| 9 |
require 'optparse' |
| 10 |
require 'tempfile' |
| 11 |
require 'time' |
| 12 |
|
| 13 |
options = OpenStruct.new |
| 14 |
options.groonga = "groonga" |
| 15 |
options.show_result = false |
| 16 |
options.report_progress = true |
| 17 |
|
| 18 |
option_parser = OptionParser.new do |parser| |
| 19 |
parser.banner += " DATABASE COMMAND_FILE1 ..." |
| 20 |
|
| 21 |
parser.on("--groonga=PATH", |
| 22 |
"Use PATH as groonga command path") do |path| |
| 23 |
options.groonga = path |
| 24 |
end |
| 25 |
|
| 26 |
parser.on("--[no-]show-result", |
| 27 |
"Show result of command") do |boolean| |
| 28 |
options.show_result = boolean |
| 29 |
end |
| 30 |
|
| 31 |
parser.on("--[no-]report-progress", |
| 32 |
"Report progress") do |boolean| |
| 33 |
options.report_progress = boolean |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
database, *command_files = option_parser.parse!(ARGV) |
| 38 |
if database.nil? |
| 39 |
puts(option_parser) |
| 40 |
exit(false) |
| 41 |
end |
| 42 |
|
| 43 |
i = 0 |
| 44 |
command_files.each do |path| |
| 45 |
File.open(path) do |file| |
| 46 |
file.each_line do |command| |
| 47 |
if options.report_progress |
| 48 |
i += 1 |
| 49 |
puts("#{Time.now.iso8601}: #{i} commands done.") if (i % 1000).zero? |
| 50 |
end |
| 51 |
command = command.chomp |
| 52 |
base_name = File.basename($0, ".*") |
| 53 |
log = Tempfile.new("#{base_name}-log") |
| 54 |
command_file = Tempfile.new("#{base_name}-command") |
| 55 |
command_file.puts(command) |
| 56 |
command_file.close |
| 57 |
command_line = [options.groonga, |
| 58 |
"--log-path", log.path, |
| 59 |
"--file", command_file.path] |
| 60 |
command_file << "-n" unless File.exist?(database) |
| 61 |
command_line << database |
| 62 |
result = Tempfile.new("#{base_name}-result") |
| 63 |
pid = spawn(*command_line, :out => result.fileno) |
| 64 |
pid, status = Process.waitpid2(pid) |
| 65 |
unless status.success? |
| 66 |
puts("failed to run: (#{status.exitstatus}): " + |
| 67 |
"[#{command_line.join(' ')}]") |
| 68 |
puts("command:") |
| 69 |
puts(command) |
| 70 |
puts("result:") |
| 71 |
result.open |
| 72 |
puts(result.read) |
| 73 |
next |
| 74 |
|
| 75 |
end |
| 76 |
if options.show_result |
| 77 |
result.open |
| 78 |
puts(result.read) |
| 79 |
end |
| 80 |
log.open |
| 81 |
log.each_line do |log_line| |
| 82 |
case log_line |
| 83 |
when /grn_fin \((\d+)\)/ |
| 84 |
n_unfreed_allocations = $1.to_i |
| 85 |
unless n_unfreed_allocations.zero? |
| 86 |
puts("maybe memory leak: #{n_unfreed_allocations}: <#{command}>") |
| 87 |
exit(false) |
| 88 |
end |
| 89 |
end |
| 90 |
end |
| 91 |
end |
| 92 |
end |
| 93 |
end |