Class: Rcodetools::XMPFilter
- Inherits:
-
Object
- Object
- Rcodetools::XMPFilter
- Defined in:
- lib/rcodetools/xmpfilter.rb
Direct Known Subclasses
XMPAddMarkers, XMPCompletionFilter, XMPDocFilter, XMPTestUnitFilter
Defined Under Namespace
Classes: Interpreter, RuntimeData
Constant Summary collapse
- VERSION =
"0.8.5"
- 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
- .detect_rbtest(code, opts) ⇒ Object
-
.run(code, opts) ⇒ Object
The processor (overridable).
Instance Method Summary collapse
- #add_markers(code, min_codeline_size = 50) ⇒ Object
- #annotate(code) ⇒ Object
- #annotated_line(line, expression, runtime_data, idx) ⇒ Object
- #annotated_multi_line(line, expression, indent, runtime_data, idx) ⇒ Object
- #common_path(a, b) ⇒ Object
- #debugprint(*args) ⇒ Object
- #execute(code) ⇒ Object
- #execute_popen(code) ⇒ Object
- #execute_ruby(code) ⇒ Object
- #execute_script(code) ⇒ Object
- #execute_tmpfile(code) ⇒ Object
- #extract_data(output) ⇒ Object
- #final_decoration(code, output) ⇒ Object
- #get_test_method_from_lineno(filename, lineno) ⇒ Object
-
#initialize(opts = {}) ⇒ XMPFilter
constructor
A new instance of XMPFilter.
- #initialize_for_test_script(test_script, test_method, filename) ⇒ Object
- #initialize_rbtest ⇒ Object
- #initialize_rct_fork ⇒ Object
- #interpreter_command ⇒ Object
- #oneline_ize(code) ⇒ Object
- #prepare_line_annotation(expr, idx, multi_line = false) ⇒ Object (also: #prepare_line)
- #windows? ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ XMPFilter
Returns a new instance of XMPFilter.
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 |
# File 'lib/rcodetools/xmpfilter.rb', line 53 def initialize(opts = {}) = INITIALIZE_OPTS.merge opts @interpreter_info = INTERPRETER_RUBY @interpreter = [:interpreter] @options = [:options] @libs = [:libs] @evals = [:evals] || [] @include_paths = [:include_paths] @output_stdout = [:output_stdout] @dump = [:dump] @warnings = [:warnings] @parentheses = [:use_parentheses] @ignore_NoMethodError = [:ignore_NoMethodError] test_script = [:test_script] test_method = [:test_method] filename = [:filename] @execute_ruby_tmpfile = [:execute_ruby_tmpfile] @postfix = "" @stdin_path = nil @width = [:width] initialize_rct_fork if [:detect_rct_fork] initialize_rbtest if [:use_rbtest] initialize_for_test_script test_script, test_method, filename if test_script and ![:use_rbtest] end |
Class Method Details
.detect_rbtest(code, opts) ⇒ Object
44 45 46 |
# File 'lib/rcodetools/xmpfilter.rb', line 44 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)
49 50 51 |
# File 'lib/rcodetools/xmpfilter.rb', line 49 def self.run(code, opts) new(opts).annotate(code) end |
Instance Method Details
#add_markers(code, min_codeline_size = 50) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rcodetools/xmpfilter.rb', line 117 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
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 |
# File 'lib/rcodetools/xmpfilter.rb', line 130 def annotate(code) idx = 0 code = code.gsub(/ # !>.*/, '') 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
160 161 162 |
# File 'lib/rcodetools/xmpfilter.rb', line 160 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
164 165 166 167 168 |
# File 'lib/rcodetools/xmpfilter.rb', line 164 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 || "\n"}") {|s, l| s << "#{indent}# " << l } end |
#common_path(a, b) ⇒ Object
113 114 115 |
# File 'lib/rcodetools/xmpfilter.rb', line 113 def common_path(a, b) (a.split(File::Separator) & b.split(File::Separator)).join(File::Separator) end |
#debugprint(*args) ⇒ Object
338 339 340 |
# File 'lib/rcodetools/xmpfilter.rb', line 338 def debugprint(*args) $stderr.puts(*args) if $DEBUG end |
#execute(code) ⇒ Object
278 279 280 |
# File 'lib/rcodetools/xmpfilter.rb', line 278 def execute(code) __send__ @interpreter_info.execute_method, code end |
#execute_popen(code) ⇒ Object
247 248 249 250 251 252 253 254 |
# File 'lib/rcodetools/xmpfilter.rb', line 247 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
210 211 212 213 |
# File 'lib/rcodetools/xmpfilter.rb', line 210 def execute_ruby(code) meth = (windows? or @execute_ruby_tmpfile) ? :execute_tmpfile : :execute_popen __send__ meth, code end |
#execute_script(code) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/rcodetools/xmpfilter.rb', line 256 def execute_script(code) path = File.("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.(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
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 |
# File 'lib/rcodetools/xmpfilter.rb', line 215 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. stdin.path exe_line = <<-EOF.map{|l| l.strip}.join(";") $stdout.reopen('#{File.(stdout.path)}', 'w') $stderr.reopen('#{File.(stderr.path)}', 'w') $0 = '#{File.(stdin.path)}' ARGV.replace(#{@options.inspect}) load #{File.(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
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/rcodetools/xmpfilter.rb', line 291 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
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/rcodetools/xmpfilter.rb', line 310 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
103 104 105 106 107 108 109 110 111 |
# File 'lib/rcodetools/xmpfilter.rb', line 103 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
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rcodetools/xmpfilter.rb', line 89 def initialize_for_test_script(test_script, test_method, filename) test_script.replace File.(test_script) filename.replace File.(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_rbtest ⇒ Object
85 86 87 |
# File 'lib/rcodetools/xmpfilter.rb', line 85 def initialize_rbtest @interpreter_info = INTERPRETER_RBTEST end |
#initialize_rct_fork ⇒ Object
79 80 81 82 83 |
# File 'lib/rcodetools/xmpfilter.rb', line 79 def initialize_rct_fork if Fork::run? @interpreter_info = INTERPRETER_FORK end end |
#interpreter_command ⇒ Object
282 283 284 285 286 287 288 289 |
# File 'lib/rcodetools/xmpfilter.rb', line 282 def interpreter_command r = [ @interpreter ] + @interpreter_info. 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
334 335 336 |
# File 'lib/rcodetools/xmpfilter.rb', line 334 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
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 |
# File 'lib/rcodetools/xmpfilter.rb', line 170 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
31 32 33 |
# File 'lib/rcodetools/xmpfilter.rb', line 31 def windows? /win|mingw/ =~ RUBY_PLATFORM && /darwin/ !~ RUBY_PLATFORM end |