Class: Rcodetools::XMPFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/rcodetools/xmpfilter.rb

Defined Under Namespace

Classes: Interpreter, RuntimeData

Constant Summary collapse

VERSION =
"0.8.0"
MARKER =
"!XMP#{Time.new.to_i}_#{Process.pid}_#{rand(1000000)}!"
XMP_RE =
Regexp.new("^" + Regexp.escape(MARKER) + '\[([0-9]+)\] (=>|~>|==>) (.*)')
VAR =
"_xmp_#{Time.new.to_i}_#{Process.pid}_#{rand(1000000)}"
WARNING_RE =
/.*:([0-9]+): warning: (.*)/
INITIALIZE_OPTS =
{:interpreter => "ruby", :options => [], :libs => [],
:include_paths => [], :warnings => true, 
:use_parentheses => true}
INTERPRETER_RUBY =
Interpreter.new(["-w"],
:execute_ruby, true, true, nil)
INTERPRETER_RBTEST =
Interpreter.new(["-S", "rbtest"],
:execute_script, false, false, nil)
INTERPRETER_FORK =
Interpreter.new(["-S", "rct-fork-client"],
:execute_tmpfile, false, true,
lambda { Fork::chdir_fork_directory })
SINGLE_LINE_RE =
/^(?!(?:\s+|(?:\s*#.+)?)# ?=>)(.*) # ?=>.*/
MULTI_LINE_RE =
/^(.*)\n(( *)# ?=>.*(?:\n|\z))(?: *#    .*\n)*/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ XMPFilter

Returns a new instance of XMPFilter.



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
# File 'lib/rcodetools/xmpfilter.rb', line 49

def initialize(opts = {})
  options = INITIALIZE_OPTS.merge opts
  @interpreter_info = INTERPRETER_RUBY
  @interpreter = options[:interpreter]
  @options = options[:options]
  @libs = options[:libs]
  @evals = options[:evals] || []
  @include_paths = options[:include_paths]
  @output_stdout = options[:output_stdout]
  @dump = options[:dump]
  @warnings = options[:warnings]
  @parentheses = options[:use_parentheses]
  @ignore_NoMethodError = options[:ignore_NoMethodError]
  test_script = options[:test_script]
  test_method = options[:test_method]
  filename = options[:filename]
  @execute_ruby_tmpfile = options[:execute_ruby_tmpfile]
  @postfix = ""
  @stdin_path = nil
  @width = options[:width]
  
  initialize_rct_fork if options[:detect_rct_fork]
  initialize_rbtest if options[:use_rbtest]
  initialize_for_test_script test_script, test_method, filename if test_script and !options[:use_rbtest]
end

Class Method Details

.detect_rbtest(code, opts) ⇒ Object



40
41
42
# File 'lib/rcodetools/xmpfilter.rb', line 40

def self.detect_rbtest(code, opts)
  opts[:use_rbtest] ||= (opts[:detect_rbtest] and code =~ /^=begin test./) ? true : false
end

.run(code, opts) ⇒ Object

The processor (overridable)



45
46
47
# File 'lib/rcodetools/xmpfilter.rb', line 45

def self.run(code, opts)
  new(opts).annotate(code)
end

Instance Method Details

#add_markers(code, min_codeline_size = 50) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/rcodetools/xmpfilter.rb', line 113

def add_markers(code, min_codeline_size = 50)
  maxlen = code.map{|x| x.size}.max
  maxlen = [min_codeline_size, maxlen + 2].max
  ret = ""
  code.each do |l|
    l = l.chomp.gsub(/ # (=>|!>).*/, "").gsub(/\s*$/, "")
    ret << (l + " " * (maxlen - l.size) + " # =>\n")
  end
  ret
end

#annotate(code) ⇒ Object



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
# File 'lib/rcodetools/xmpfilter.rb', line 126

def annotate(code)
  idx = 0
  newcode = code.gsub(SINGLE_LINE_RE){ prepare_line($1, idx += 1) }
  newcode.gsub!(MULTI_LINE_RE){ prepare_line($1, idx += 1, true)}
  File.open(@dump, "w"){|f| f.puts newcode} if @dump
  stdout, stderr = execute(newcode)
  output = stderr.readlines
  runtime_data = extract_data(output)
  idx = 0
  annotated = code.gsub(SINGLE_LINE_RE) { |l|
    expr = $1
    if /^\s*#/ =~ l
      l 
    else
      annotated_line(l, expr, runtime_data, idx += 1)
    end
  }
  annotated.gsub!(/ # !>.*/, '')
  annotated.gsub!(/# (>>|~>)[^\n]*\n/m, "");
  annotated.gsub!(MULTI_LINE_RE) { |l|
    annotated_multi_line(l, $1, $3, runtime_data, idx += 1)
  }
  ret = final_decoration(annotated, output)
  if @output_stdout and (s = stdout.read) != ""
    ret << s.inject(""){|s,line| s + "# >> #{line}".chomp + "\n" }
  end
  ret
end

#annotated_line(line, expression, runtime_data, idx) ⇒ Object



155
156
157
# File 'lib/rcodetools/xmpfilter.rb', line 155

def annotated_line(line, expression, runtime_data, idx)
  "#{expression} # => " + (runtime_data.results[idx].map{|x| x[1]} || []).join(", ")
end

#annotated_multi_line(line, expression, indent, runtime_data, idx) ⇒ Object



159
160
161
162
163
# File 'lib/rcodetools/xmpfilter.rb', line 159

def annotated_multi_line(line, expression, indent, runtime_data, idx)
  pretty = (runtime_data.results[idx].map{|x| x[1]} || []).join(", ")
  first, *rest = pretty.to_a
  rest.inject("#{expression}\n#{indent}# => #{first}") {|s, l| s << "#{indent}#    " << l }
end

#common_path(a, b) ⇒ Object



109
110
111
# File 'lib/rcodetools/xmpfilter.rb', line 109

def common_path(a, b)
  (a.split(File::Separator) & b.split(File::Separator)).join(File::Separator)
end

#debugprint(*args) ⇒ Object



333
334
335
# File 'lib/rcodetools/xmpfilter.rb', line 333

def debugprint(*args)
  $stderr.puts(*args) if $DEBUG
end

#execute(code) ⇒ Object



273
274
275
# File 'lib/rcodetools/xmpfilter.rb', line 273

def execute(code)
  __send__ @interpreter_info.execute_method, code
end

#execute_popen(code) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/rcodetools/xmpfilter.rb', line 242

def execute_popen(code)
  require 'open3'
  stdin, stdout, stderr = Open3::popen3(*interpreter_command)
  stdin.puts code
  @evals.each{|x| stdin.puts x } unless @evals.empty?
  stdin.close
  [stdout, stderr]
end

#execute_ruby(code) ⇒ Object



205
206
207
208
# File 'lib/rcodetools/xmpfilter.rb', line 205

def execute_ruby(code)
  meth = (windows? or @execute_ruby_tmpfile) ? :execute_tmpfile : :execute_popen
  __send__ meth, code
end

#execute_script(code) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/rcodetools/xmpfilter.rb', line 251

def execute_script(code)
  path = File.expand_path("xmpfilter.tmpfile_#{Process.pid}.rb", Dir.tmpdir)
  File.open(path, "w"){|f| f.puts code}
  at_exit { File.unlink path if File.exist? path}
  stdout_path, stderr_path = (1..2).map do |i|
    fname = "xmpfilter.tmpfile_#{Process.pid}-#{i}.rb"
    File.expand_path(fname, Dir.tmpdir)
  end
  args = *(interpreter_command << %["#{path}"] << "2>" << 
    %["#{stderr_path}"] << ">" << %["#{stdout_path}"])
  system(args.join(" "))
  
  [stdout_path, stderr_path].map do |fullname|
    f = File.open(fullname, "r")
    at_exit {
      f.close unless f.closed?
      File.unlink fullname if File.exist? fullname
    }
    f
  end
end

#execute_tmpfile(code) ⇒ Object



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
# File 'lib/rcodetools/xmpfilter.rb', line 210

def execute_tmpfile(code)
  ios = %w[_ stdin stdout stderr]
  stdin, stdout, stderr = (1..3).map do |i|
    fname = if $DEBUG
              "xmpfilter.tmpfile_#{ios[i]}.rb"
            else
              "xmpfilter.tmpfile_#{Process.pid}-#{i}.rb"
            end
    f = File.open(fname, "w+")
    at_exit { f.close unless f.closed?; File.unlink fname unless $DEBUG}
    f
  end
  stdin.puts code
  stdin.close
  @stdin_path = File.expand_path stdin.path
  exe_line = <<-EOF.map{|l| l.strip}.join(";")
    $stdout.reopen('#{File.expand_path(stdout.path)}', 'w')
    $stderr.reopen('#{File.expand_path(stderr.path)}', 'w')
    $0.replace '#{File.expand_path(stdin.path)}'
    ARGV.replace(#{@options.inspect})
    load #{File.expand_path(stdin.path).inspect}
    #{@evals.join(";")}
  EOF
  debugprint "execute command = #{(interpreter_command << "-e" << exe_line).join ' '}"

  oldpwd = Dir.pwd
  @interpreter_info.chdir_proc and @interpreter_info.chdir_proc.call
  system(*(interpreter_command << "-e" << exe_line))
  Dir.chdir oldpwd
  [stdout, stderr]
end

#extract_data(output) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rcodetools/xmpfilter.rb', line 286

def extract_data(output)
  results = Hash.new{|h,k| h[k] = []}
  exceptions = Hash.new{|h,k| h[k] = []}
  bindings = Hash.new{|h,k| h[k] = []}
  output.grep(XMP_RE).each do |line|
    result_id, op, result = XMP_RE.match(line).captures
    case op
    when "=>"
      klass, value = /(\S+)\s+(.*)/.match(result).captures
      results[result_id.to_i] << [klass, value.gsub(/PPPROTECT/, "\n")]
    when "~>"
      exceptions[result_id.to_i] << result
    when "==>"
      bindings[result_id.to_i] << result unless result.index(VAR) 
    end
  end
  RuntimeData.new(results, exceptions, bindings)
end

#final_decoration(code, output) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/rcodetools/xmpfilter.rb', line 305

def final_decoration(code, output)
  warnings = {}
  output.join.grep(WARNING_RE).map do |x|
    md = WARNING_RE.match(x)
    warnings[md[1].to_i] = md[2]
  end
  idx = 0
  ret = code.map do |line|
    w = warnings[idx+=1]
    if @warnings
      w ? (line.chomp + " # !> #{w}") : line
    else
      line
    end
  end
  output = output.reject{|x| /^-:[0-9]+: warning/.match(x)}
  if exception = /^-e?:[0-9]+:.*|^(?!!XMP)[^\n]+:[0-9]+:in .*/m.match(output.join)
    err = exception[0]
    err.gsub!(Regexp.union(@stdin_path), '-') if @stdin_path
    ret << err.map{|line| "# ~> " + line }
  end
  ret
end

#get_test_method_from_lineno(filename, lineno) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/rcodetools/xmpfilter.rb', line 99

def get_test_method_from_lineno(filename, lineno)
  lines = File.readlines(filename)
  (lineno-1).downto(0) do |i|
    if lines[i] =~ /^ *def *(test_[A-Za-z0-9?!_]+)$/
      return $1
    end
  end
  nil
end

#initialize_for_test_script(test_script, test_method, filename) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rcodetools/xmpfilter.rb', line 85

def initialize_for_test_script(test_script, test_method, filename)
  test_script.replace File.expand_path(test_script)
  filename.replace File.expand_path(filename)
  unless test_script == filename
    basedir = common_path(test_script, filename)
    relative_filename = filename[basedir.length+1 .. -1].sub(%r!^lib/!, '')
    @evals << %Q!$LOADED_FEATURES << #{relative_filename.dump}!
    @evals << safe_require_code('test/unit')
    @evals << %Q!load #{test_script.dump}!
  end
  test_method = get_test_method_from_lineno(test_script, test_method.to_i) if test_method =~ /^\d/
  @evals << %Q!Test::Unit::AutoRunner.run(false, nil, ["-n", #{test_method.dump}])! if test_method
end

#initialize_rbtestObject



81
82
83
# File 'lib/rcodetools/xmpfilter.rb', line 81

def initialize_rbtest
  @interpreter_info = INTERPRETER_RBTEST
end

#initialize_rct_forkObject



75
76
77
78
79
# File 'lib/rcodetools/xmpfilter.rb', line 75

def initialize_rct_fork
  if Fork::run?
    @interpreter_info = INTERPRETER_FORK
  end
end

#interpreter_commandObject



277
278
279
280
281
282
283
284
# File 'lib/rcodetools/xmpfilter.rb', line 277

def interpreter_command
  r = [ @interpreter ] + @interpreter_info.options
  r << "-d" if $DEBUG and @interpreter_info.accept_debug
  r << "-I#{@include_paths.join(":")}" if @interpreter_info.accept_include_paths and !@include_paths.empty?
  @libs.each{|x| r << "-r#{x}" } unless @libs.empty?
  (r << "-").concat @options unless @options.empty?
  r
end

#oneline_ize(code) ⇒ Object



329
330
331
# File 'lib/rcodetools/xmpfilter.rb', line 329

def oneline_ize(code)
  "((" + code.gsub(/\r?\n|\r/, ';') + "));#{@postfix}\n"
end

#prepare_line_annotation(expr, idx, multi_line = false) ⇒ Object Also known as: prepare_line



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
# File 'lib/rcodetools/xmpfilter.rb', line 165

def prepare_line_annotation(expr, idx, multi_line=false)
  v = "#{VAR}"
  blocal = "__#{VAR}"
  blocal2 = "___#{VAR}"
  lastmatch = "____#{VAR}"
  if multi_line
    pp = safe_require_code "pp"
    result = "((begin; #{lastmatch} = $~; PP.pp(#{v}, '', #{@width-5}).gsub(/\\r?\\n/, 'PPPROTECT'); ensure; $~ = #{lastmatch} end))"
  else
    pp = ''
    result = "#{v}.inspect"
  end
  oneline_ize(<<-EOF).chomp
#{pp}
#{v} = (#{expr})
$stderr.puts("#{MARKER}[#{idx}] => " + #{v}.class.to_s + " " + #{result}) || begin
$stderr.puts local_variables
local_variables.each{|#{blocal}|
  #{blocal2} = eval(#{blocal})
  if #{v} == #{blocal2} && #{blocal} != %#{expr}.strip
    $stderr.puts("#{MARKER}[#{idx}] ==> " + #{blocal})
  elsif [#{blocal2}] == #{v}
    $stderr.puts("#{MARKER}[#{idx}] ==> [" + #{blocal} + "]")
  end
}
nil
rescue Exception
nil
end || #{v}
  EOF

end

#windows?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rcodetools/xmpfilter.rb', line 27

def windows?
  /win|mingw/ =~ RUBY_PLATFORM && /darwin/ !~ RUBY_PLATFORM
end