Class: Flay
- Inherits:
-
Object
- Object
- Flay
- Defined in:
- lib/flay.rb,
lib/flay_erb.rb
Constant Summary collapse
- VERSION =
'1.2.1'
Instance Attribute Summary collapse
-
#hashes ⇒ Object
readonly
Returns the value of attribute hashes.
-
#identical ⇒ Object
Returns the value of attribute identical.
-
#mass_threshold ⇒ Object
Returns the value of attribute mass_threshold.
-
#masses ⇒ Object
Returns the value of attribute masses.
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#total ⇒ Object
Returns the value of attribute total.
Class Method Summary collapse
- .default_options ⇒ Object
- .expand_dirs_to_files(*dirs) ⇒ Object
- .load_plugins ⇒ Object
- .parse_options ⇒ Object
Instance Method Summary collapse
- #analyze ⇒ Object
-
#initialize(option = nil) ⇒ Flay
constructor
A new instance of Flay.
- #n_way_diff(*data) ⇒ Object
-
#process(*files) ⇒ Object
TODO: rename from process - should act as SexpProcessor.
- #process_erb(file) ⇒ Object
- #process_fuzzy_similarities ⇒ Object
- #process_rb(file) ⇒ Object
- #process_sexp(pt) ⇒ Object
- #prune ⇒ Object
- #report(prune = nil) ⇒ Object
Constructor Details
#initialize(option = nil) ⇒ Flay
Returns a new instance of Flay.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/flay.rb', line 93 def initialize option = nil @option = option || Flay. @hashes = Hash.new { |h,k| h[k] = [] } self.identical = {} self.masses = {} self.total = 0 self.mass_threshold = @option[:mass] require 'ruby2ruby' if @option[:verbose] end |
Instance Attribute Details
#hashes ⇒ Object (readonly)
Returns the value of attribute hashes.
91 92 93 |
# File 'lib/flay.rb', line 91 def hashes @hashes end |
#identical ⇒ Object
Returns the value of attribute identical.
90 91 92 |
# File 'lib/flay.rb', line 90 def identical @identical end |
#mass_threshold ⇒ Object
Returns the value of attribute mass_threshold.
90 91 92 |
# File 'lib/flay.rb', line 90 def mass_threshold @mass_threshold end |
#masses ⇒ Object
Returns the value of attribute masses.
90 91 92 |
# File 'lib/flay.rb', line 90 def masses @masses end |
#option ⇒ Object (readonly)
Returns the value of attribute option.
91 92 93 |
# File 'lib/flay.rb', line 91 def option @option end |
#total ⇒ Object
Returns the value of attribute total.
90 91 92 |
# File 'lib/flay.rb', line 90 def total @total end |
Class Method Details
.default_options ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/flay.rb', line 16 def self. { :fuzzy => false, :verbose => false, :mass => 16, } end |
.expand_dirs_to_files(*dirs) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/flay.rb', line 61 def self. *dirs extensions = ['rb'] + Flay.load_plugins dirs.flatten.map { |p| if File.directory? p then Dir[File.join(p, '**', "*.{#{extensions.join(',')}}")] else p end }.flatten end |
.load_plugins ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/flay.rb', line 73 def self.load_plugins unless defined? @@plugins then plugins = Gem.find_files("flay_*.rb").reject { |p| p =~ /flay_task/ } plugins.each do |plugin| begin load plugin rescue LoadError => e warn "error loading #{plugin.inspect}: #{e.}. skipping..." end end @@plugins = plugins.map { |f| File.basename(f, '.rb').sub(/^flay_/, '') } end @@plugins end |
.parse_options ⇒ Object
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 |
# File 'lib/flay.rb', line 24 def self. = self. OptionParser.new do |opts| opts. = 'flay [options] files_or_dirs' opts.version = Flay::VERSION opts.separator "" opts.separator "Specific options:" opts.separator "" opts.on('-h', '--help', 'Display this help.') do puts opts exit end opts.on('-f', '--fuzzy', "Attempt to do fuzzy similarities. (SLOW)") do [:fuzzy] = true end opts.on('-m', '--mass MASS', Integer, "Sets mass threshold") do |m| [:mass] = m.to_i end opts.on('-v', '--verbose', "Verbose. Display N-Way diff for ruby.") do [:verbose] = true end extensions = ['rb'] + Flay.load_plugins opts.separator "" opts.separator "Known extensions: #{extensions.join(', ')}" end.parse! end |
Instance Method Details
#analyze ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/flay.rb', line 136 def analyze self.prune self.hashes.each do |hash,nodes| identical[hash] = nodes[1..-1].all? { |n| n == nodes.first } masses[hash] = nodes.first.mass * nodes.size masses[hash] *= (nodes.size) if identical[hash] self.total += masses[hash] end end |
#n_way_diff(*data) ⇒ Object
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 |
# File 'lib/flay.rb', line 214 def n_way_diff *data data.each_with_index do |s, i| c = (?A + i).chr s.group = c end max = data.map { |s| s.scan(/^.*/).size }.max data.map! { |s| # FIX: this is tarded, but I'm out of brain c = s.group s = s.scan(/^.*/) s.push(*([""] * (max - s.size))) # pad s.each do |o| o.group = c end s } groups = data[0].zip(*data[1..-1]) groups.map! { |lines| collapsed = lines.uniq if collapsed.size == 1 then " #{lines.first}" else # TODO: make r2r have a canonical mode (doesn't make 1-liners) lines.reject { |l| l.empty? }.map { |l| "#{l.group}: #{l}" } end } groups.flatten.join("\n") end |
#process(*files) ⇒ Object
TODO: rename from process - should act as SexpProcessor
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 |
# File 'lib/flay.rb', line 105 def process(*files) # TODO: rename from process - should act as SexpProcessor files.each do |file| warn "Processing #{file}" ext = File.extname(file).sub(/^\./, '') ext = "rb" if ext.nil? || ext.empty? msg = "process_#{ext}" unless respond_to? msg then warn " Unknown file type: #{ext}, defaulting to ruby" msg = "process_rb" end sexp = begin send msg, file rescue => e warn " #{e..strip}" warn " skipping #{file}" nil end next unless sexp process_sexp sexp end process_fuzzy_similarities if option[:fuzzy] analyze end |
#process_erb(file) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/flay_erb.rb', line 8 def process_erb file erb = File.read file src = ERB.new(erb).src RubyParser.new.process(src, file) end |
#process_fuzzy_similarities ⇒ Object
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 |
# File 'lib/flay.rb', line 160 def process_fuzzy_similarities all_hashes, detected = {}, {} self.hashes.values.each do |nodes| nodes.each do |node| next if node.mass > 4 * self.mass_threshold # TODO: try out with fuzzy_hash # all_hashes[node] = node.grep(Sexp).map { |s| [s.hash] * s.mass }.flatten all_hashes[node] = node.grep(Sexp).map { |s| [s.hash] }.flatten end end # warn "looking for copy/paste/edit code across #{all_hashes.size} nodes" all_hashes = all_hashes.to_a all_hashes.each_with_index do |(s1, h1), i| similar = [s1] all_hashes[i+1..-1].each do |(s2, h2)| next if detected[h2] intersection = h1.intersection h2 max = [h1.size, h2.size].max if intersection.size >= max * 0.60 then similarity = s1.similarity(s2) if similarity > 0.60 then similar << s2 detected[h2] = true else p [similarity, s1, s2] end end end self.hashes[similar.first.hash].push(*similar) if similar.size > 1 end end |
#process_rb(file) ⇒ Object
147 148 149 |
# File 'lib/flay.rb', line 147 def process_rb file RubyParser.new.process(File.read(file), file) end |
#process_sexp(pt) ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/flay.rb', line 151 def process_sexp pt pt.deep_each do |node| next unless node.any? { |sub| Sexp === sub } next if node.mass < self.mass_threshold self.hashes[node.fuzzy_hash] << node end end |
#prune ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/flay.rb', line 196 def prune # prune trees that aren't duped at all, or are too small self.hashes.delete_if { |_,nodes| nodes.size == 1 } # extract all subtree hashes from all nodes all_hashes = {} self.hashes.values.each do |nodes| nodes.each do |node| node.all_subhashes.each do |h| all_hashes[h] = true end end end # nuke subtrees so we show the biggest matching tree possible self.hashes.delete_if { |h,_| all_hashes[h] } end |
#report(prune = nil) ⇒ Object
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 278 279 280 281 282 283 |
# File 'lib/flay.rb', line 245 def report prune = nil puts "Total score (lower is better) = #{self.total}" puts count = 0 masses.sort_by { |h,m| [-m, hashes[h].first.file] }.each do |hash, mass| nodes = hashes[hash] next unless nodes.first.first == prune if prune puts same = identical[hash] node = nodes.first n = nodes.size match, bonus = if same then ["IDENTICAL", "*#{n}"] else ["Similar", ""] end count += 1 puts "%d) %s code found in %p (mass%s = %d)" % [count, match, node.first, bonus, mass] nodes.each_with_index do |node, i| if option[:verbose] then c = (?A + i).chr puts " #{c}: #{node.file}:#{node.line}" else puts " #{node.file}:#{node.line}" end end if option[:verbose] then puts r2r = Ruby2Ruby.new puts n_way_diff(*nodes.map { |s| r2r.process(s.deep_clone) }) end end end |