flog 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/History.txt +5 -0
  2. data/Manifest.txt +1 -0
  3. data/Rakefile +1 -1
  4. data/bin/flog +4 -208
  5. data/lib/flog.rb +203 -0
  6. metadata +2 -1
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.0.2 / 2007-08-01
2
+
3
+ * 1 bug fix:
4
+ * stupid rubygems bin wrapper... *sigh*
5
+
1
6
  == 1.0.1 / 2007-08-01
2
7
 
3
8
  * 1 bug fix:
data/Manifest.txt CHANGED
@@ -3,3 +3,4 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  bin/flog
6
+ lib/flog.rb
data/Rakefile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'hoe'
5
- load './bin/flog'
5
+ require './lib/flog'
6
6
 
7
7
  Hoe.new('flog', Flog::VERSION) do |p|
8
8
  p.rubyforge_name = 'seattlerb'
data/bin/flog CHANGED
@@ -1,211 +1,7 @@
1
1
  #!/usr/local/bin/ruby -w
2
2
 
3
- require 'rubygems'
4
- require 'parse_tree'
5
- require 'sexp_processor'
6
- require 'unified_ruby'
3
+ require 'flog'
7
4
 
8
- class Flog < SexpProcessor
9
- VERSION = '1.0.1'
10
-
11
- include UnifiedRuby
12
-
13
- THRESHOLD = 0.60
14
-
15
- SCORES = Hash.new(1)
16
-
17
- SCORES.merge!(:define_method => 5,
18
- :eval => 5,
19
- :module_eval => 5,
20
- :class_eval => 5,
21
- :instance_eval => 5)
22
-
23
- SCORES.merge!(:alias_method => 2,
24
- :include => 2,
25
- :extend => 2,
26
- :instance_method => 2,
27
- :instance_methods => 2,
28
- :method_added => 2,
29
- :method_defined? => 2,
30
- :method_removed => 2,
31
- :method_undefined => 2,
32
- :private_class_method => 2,
33
- :private_instance_methods => 2,
34
- :private_method_defined? => 2,
35
- :protected_instance_methods => 2,
36
- :protected_method_defined? => 2,
37
- :public_class_method => 2,
38
- :public_instance_methods => 2,
39
- :public_method_defined? => 2,
40
- :remove_method => 2,
41
- :undef_method => 2)
42
-
43
- @@no_class = :none
44
- @@no_method = :none
45
-
46
- def initialize
47
- super
48
- @pt = ParseTree.new(false)
49
- @klass_name, @method_name = @@no_class, @@no_method
50
- self.auto_shift_type = true
51
- self.require_empty = false # HACK
52
- @totals = Hash.new 0
53
- @multiplier = 1.0
54
-
55
- @calls = Hash.new { |h,k| h[k] = Hash.new 0 }
56
- end
57
-
58
- def process_files *files
59
- files.flatten.each do |file|
60
- next unless File.file? file or file == "-"
61
- ruby = file == "-" ? $stdin.read : File.read(file)
62
- sexp = @pt.parse_tree_for_string(ruby, file)
63
- process Sexp.from_array(sexp)
64
- end
65
- end
66
-
67
- def report
68
- total_score = @totals.values.inject(0) { |sum,n| sum + n }
69
- max = total_score * THRESHOLD
70
- current = 0
71
-
72
- puts "Total score = #{total_score}"
73
- puts
74
-
75
- @calls.sort_by { |k,v| -@totals[k] }.each do |klass_method, calls|
76
- total = @totals[klass_method]
77
- puts "%s: (%d)" % [klass_method, total]
78
- calls.sort_by { |k,v| -v }.each do |call, count|
79
- puts " %4d: %s" % [count, call]
80
- end
81
-
82
- current += total
83
- break if current >= max
84
- end
85
- rescue
86
- # do nothing
87
- end
88
-
89
- def add_to_score(name, score)
90
- @totals["#{@klass_name}##{@method_name}"] += score * @multiplier
91
- @calls["#{@klass_name}##{@method_name}"][name] += score * @multiplier
92
- end
93
-
94
- def bad_dog! bonus
95
- @multiplier += bonus
96
- yield
97
- @multiplier -= bonus
98
- end
99
-
100
- ############################################################
101
- # Process Methods:
102
-
103
- def process_alias(exp)
104
- process exp.shift
105
- process exp.shift
106
- add_to_score :alias, 2
107
- s()
108
- end
109
-
110
- # [:block_pass, [:lit, :blah], [:fcall, :foo]]
111
- def process_block_pass(exp)
112
- arg = exp.shift
113
- call = exp.shift
114
-
115
- case arg.first
116
- when :iter then
117
- add_to_score :to_proc_iter_wtf?, 6
118
- when :lit, :call, :iter then
119
- add_to_score :to_proc, 3
120
- when :lvar, :dvar, :ivar, :nil then
121
- # do nothing
122
- else
123
- raise({:block_pass => [call, arg]}.inspect)
124
- end
125
-
126
- call = process call
127
- s()
128
- end
129
-
130
- def process_call(exp)
131
- bad_dog! 0.2 do
132
- recv = process exp.shift
133
- end
134
- name = exp.shift
135
- bad_dog! 0.2 do
136
- args = process exp.shift
137
- end
138
-
139
- score = SCORES[name]
140
- add_to_score name, score
141
-
142
- s()
143
- end
144
-
145
- def process_class(exp)
146
- @klass_name = exp.shift
147
- bad_dog! 1.0 do
148
- supr = process exp.shift
149
- end
150
- until exp.empty?
151
- process exp.shift
152
- end
153
- @klass_name = @@no_class
154
- s()
155
- end
156
-
157
- def process_defn(exp)
158
- @method_name = exp.shift
159
- process exp.shift until exp.empty?
160
- @method_name = @@no_method
161
- s()
162
- end
163
-
164
- def process_defs(exp)
165
- process exp.shift
166
- @method_name = exp.shift
167
- process exp.shift until exp.empty?
168
- @method_name = @@no_method
169
- s()
170
- end
171
-
172
- def process_lit(exp)
173
- value = exp.shift
174
- case value
175
- when 0, -1 then
176
- # ignore those because they're used as array indicies instead of first/last
177
- when Integer then
178
- add_to_score :lit_fixnum, 0.25
179
- when Float, Symbol, Regexp, Range then
180
- # do nothing
181
- else
182
- raise value.inspect
183
- end
184
- s()
185
- end
186
-
187
- def process_module(exp)
188
- @klass_name = exp.shift
189
- until exp.empty?
190
- process exp.shift
191
- end
192
- @klass_name = @@no_class
193
- s()
194
- end
195
-
196
- def process_sclass(exp)
197
- bad_dog! 0.5 do
198
- recv = process exp.shift
199
- process exp.shift until exp.empty?
200
- end
201
-
202
- add_to_score :sclass, 5
203
- s()
204
- end
205
- end
206
-
207
- if $0 == __FILE__ then
208
- flogger = Flog.new
209
- flogger.process_files ARGV
210
- flogger.report
211
- end
5
+ flogger = Flog.new
6
+ flogger.process_files ARGV
7
+ flogger.report
data/lib/flog.rb ADDED
@@ -0,0 +1,203 @@
1
+ require 'rubygems'
2
+ require 'parse_tree'
3
+ require 'sexp_processor'
4
+ require 'unified_ruby'
5
+
6
+ class Flog < SexpProcessor
7
+ VERSION = '1.0.2'
8
+
9
+ include UnifiedRuby
10
+
11
+ THRESHOLD = 0.60
12
+
13
+ SCORES = Hash.new(1)
14
+
15
+ SCORES.merge!(:define_method => 5,
16
+ :eval => 5,
17
+ :module_eval => 5,
18
+ :class_eval => 5,
19
+ :instance_eval => 5)
20
+
21
+ SCORES.merge!(:alias_method => 2,
22
+ :include => 2,
23
+ :extend => 2,
24
+ :instance_method => 2,
25
+ :instance_methods => 2,
26
+ :method_added => 2,
27
+ :method_defined? => 2,
28
+ :method_removed => 2,
29
+ :method_undefined => 2,
30
+ :private_class_method => 2,
31
+ :private_instance_methods => 2,
32
+ :private_method_defined? => 2,
33
+ :protected_instance_methods => 2,
34
+ :protected_method_defined? => 2,
35
+ :public_class_method => 2,
36
+ :public_instance_methods => 2,
37
+ :public_method_defined? => 2,
38
+ :remove_method => 2,
39
+ :undef_method => 2)
40
+
41
+ @@no_class = :none
42
+ @@no_method = :none
43
+
44
+ def initialize
45
+ super
46
+ @pt = ParseTree.new(false)
47
+ @klass_name, @method_name = @@no_class, @@no_method
48
+ self.auto_shift_type = true
49
+ self.require_empty = false # HACK
50
+ @totals = Hash.new 0
51
+ @multiplier = 1.0
52
+
53
+ @calls = Hash.new { |h,k| h[k] = Hash.new 0 }
54
+ end
55
+
56
+ def process_files *files
57
+ files.flatten.each do |file|
58
+ next unless File.file? file or file == "-"
59
+ ruby = file == "-" ? $stdin.read : File.read(file)
60
+ sexp = @pt.parse_tree_for_string(ruby, file)
61
+ process Sexp.from_array(sexp)
62
+ end
63
+ end
64
+
65
+ def report
66
+ total_score = @totals.values.inject(0) { |sum,n| sum + n }
67
+ max = total_score * THRESHOLD
68
+ current = 0
69
+
70
+ puts "Total score = #{total_score}"
71
+ puts
72
+
73
+ @calls.sort_by { |k,v| -@totals[k] }.each do |klass_method, calls|
74
+ total = @totals[klass_method]
75
+ puts "%s: (%d)" % [klass_method, total]
76
+ calls.sort_by { |k,v| -v }.each do |call, count|
77
+ puts " %4d: %s" % [count, call]
78
+ end
79
+
80
+ current += total
81
+ break if current >= max
82
+ end
83
+ rescue
84
+ # do nothing
85
+ end
86
+
87
+ def add_to_score(name, score)
88
+ @totals["#{@klass_name}##{@method_name}"] += score * @multiplier
89
+ @calls["#{@klass_name}##{@method_name}"][name] += score * @multiplier
90
+ end
91
+
92
+ def bad_dog! bonus
93
+ @multiplier += bonus
94
+ yield
95
+ @multiplier -= bonus
96
+ end
97
+
98
+ ############################################################
99
+ # Process Methods:
100
+
101
+ def process_alias(exp)
102
+ process exp.shift
103
+ process exp.shift
104
+ add_to_score :alias, 2
105
+ s()
106
+ end
107
+
108
+ # [:block_pass, [:lit, :blah], [:fcall, :foo]]
109
+ def process_block_pass(exp)
110
+ arg = exp.shift
111
+ call = exp.shift
112
+
113
+ case arg.first
114
+ when :iter then
115
+ add_to_score :to_proc_iter_wtf?, 6
116
+ when :lit, :call, :iter then
117
+ add_to_score :to_proc, 3
118
+ when :lvar, :dvar, :ivar, :nil then
119
+ # do nothing
120
+ else
121
+ raise({:block_pass => [call, arg]}.inspect)
122
+ end
123
+
124
+ call = process call
125
+ s()
126
+ end
127
+
128
+ def process_call(exp)
129
+ bad_dog! 0.2 do
130
+ recv = process exp.shift
131
+ end
132
+ name = exp.shift
133
+ bad_dog! 0.2 do
134
+ args = process exp.shift
135
+ end
136
+
137
+ score = SCORES[name]
138
+ add_to_score name, score
139
+
140
+ s()
141
+ end
142
+
143
+ def process_class(exp)
144
+ @klass_name = exp.shift
145
+ bad_dog! 1.0 do
146
+ supr = process exp.shift
147
+ end
148
+ until exp.empty?
149
+ process exp.shift
150
+ end
151
+ @klass_name = @@no_class
152
+ s()
153
+ end
154
+
155
+ def process_defn(exp)
156
+ @method_name = exp.shift
157
+ process exp.shift until exp.empty?
158
+ @method_name = @@no_method
159
+ s()
160
+ end
161
+
162
+ def process_defs(exp)
163
+ process exp.shift
164
+ @method_name = exp.shift
165
+ process exp.shift until exp.empty?
166
+ @method_name = @@no_method
167
+ s()
168
+ end
169
+
170
+ def process_lit(exp)
171
+ value = exp.shift
172
+ case value
173
+ when 0, -1 then
174
+ # ignore those because they're used as array indicies instead of first/last
175
+ when Integer then
176
+ add_to_score :lit_fixnum, 0.25
177
+ when Float, Symbol, Regexp, Range then
178
+ # do nothing
179
+ else
180
+ raise value.inspect
181
+ end
182
+ s()
183
+ end
184
+
185
+ def process_module(exp)
186
+ @klass_name = exp.shift
187
+ until exp.empty?
188
+ process exp.shift
189
+ end
190
+ @klass_name = @@no_class
191
+ s()
192
+ end
193
+
194
+ def process_sclass(exp)
195
+ bad_dog! 0.5 do
196
+ recv = process exp.shift
197
+ process exp.shift until exp.empty?
198
+ end
199
+
200
+ add_to_score :sclass, 5
201
+ s()
202
+ end
203
+ end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: flog
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.1
6
+ version: 1.0.2
7
7
  date: 2007-08-01 00:00:00 -07:00
8
8
  summary: Flog reports the most tortured code in an easy to read pain report. The higher the score, the more pain the code is in.
9
9
  require_paths:
@@ -34,6 +34,7 @@ files:
34
34
  - README.txt
35
35
  - Rakefile
36
36
  - bin/flog
37
+ - lib/flog.rb
37
38
  test_files: []
38
39
 
39
40
  rdoc_options: