Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops.rb
More file actions
Latest commit
254 lines (210 loc) · 7.43 KB
/
Copy pathoops.rb
File metadata and controls
254 lines (210 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# coding: utf-8
#
# author - Huascar Sanchez
#
require'rubygems'
require'find'
require'csv'
# Result is a record class that will hold
# any collected data related to loops in BIND
classResult
attr_accessor:dir,:file,:loop,:count,:content
definitialize(dir,file,loop,count,content=[])
@dir=dir
@file=file
@loop=loop
@count=count
@content=content
end
defto_s
"Result{dir=#{dir}, file=#{file}, loop=#{loop}, count=#{count}, content=#{content}}"
end
end
# Public: finds all files matching a given file extension(s).
#
# dir - path/to/your/files
# ext - file extension
#
# Returns an array of files matching the extension.
deffind(dir,ext='c|h')
files=[]
# copy file and path to array if
# filename ends in ext. e.g., ".c" or ".h"
Find.find(dir)do |f|
files << fiff.match(/\.#{ext}\Z/)
end
files
end
# Public: calculates frequency of loops
#
# files - array of files matching a given extension: e.g., .c, .h
# directory - path/to/your/files
#
# Returns an array of results.
defcount_loops(files,directory)
result=[]
files.eachdo |file|
begin
# ignore statements that start with '#'
curated_code=IO.popen("cpp #{file}").readlines.select{ |line| not line.match(/^#/)}
# collect while and for loops
all_while=curated_code.select{|line| line.match('while *\(.*\)')}
all_for=curated_code.select{|line| line.match('for *\(.*\)')}
ifall_while.length > 0
content=[]
all_while.eachdo |w|
content << w.strip
end
result << Result.new(directory,File.basename(file),'while',all_while.length,content)
end
ifall_for.length > 0
content=[]
all_for.eachdo |f|
content << f.strip
end
result << Result.new(directory,File.basename(file),'for',all_for.length,content)
end
rescueException=>e
puts"Got exception: #{e.inspect}"
end
end
result
end
# makes a new csv file
defmakefile(directory)
begin
File.new("result/data-#{directory.gsub('/','-')}.csv",'w').close
rescueException=>e
puts"Got exception: #{e.inspect}"
end
end
# Public: extracts items from results and writes them into a csv file.
#
# result - array of results
# directory - path/to/your/files
#
# Returns nothing.
defcopy_to_csv(result,directory)
makefile(directory)
CSV.open("result/data-#{directory.gsub('/','-')}.csv",'wb')do |csv|
csv << [:dir,:file,:loop,:count,:content]
result.eachdo |item|
begin
csv << [
"#{item.dir}",
"#{item.file}",
"#{item.loop}",
"#{item.count}",
item.content.join('>>>')]
rescueException=>e
puts"Got exception: #{e.inspect}"
end
end
end
end
defplay
directories=%w(
bind/lib/dns
bind/lib/bind9
bind/lib/export
bind/lib/irs
bind/lib/isc
bind/lib/isccc
bind/lib/isccfg
bind/lib/lwres
bind/lib/tests
bind/lib/win32
)
directories.eachdo |dir|
copy_to_csv(count_loops(find(dir),dir),dir)
sleep(15.seconds)# will avoid the vfork problem - not enough resources
end
end
# Public: prints any given string.
#
# s = String output
#
# Prints to STDOUT and returns.
defoutput(s)
puts(s)
end
# Public: prints a tidy overview of your Lists in descending order of
# number of Items.
#
# Returns nothing.
defoverview
output' Total Loops | Total Whiles | Total Fors '
total_whiles=0
total_fors=0
CSV.foreach('result/data.csv',:headers=>true)do |row|
increment=row['count'].to_i
total_whiles += incrementifrow['loop'] == 'while'
total_fors += incrementifrow['loop'] == 'for'
end
output" \t#{total_whiles + total_fors}\t\t\t#{total_whiles}\t\t#{total_fors}"
output"\n"
end
# Public: prints the detailed view of all collected data.
#
# Returns nothing.
defbreakdown
dns={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/dns'}
bind9={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/bind9'}
export={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/export'}
irs={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/irs'}
isc={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/isc'}
isccc={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/isccc'}
isccfg={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/isccfg'}
lwres={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/lwres'}
test={:while=>0,:for=>0,:total=>0,:dir=>'bind/lib/tests'}
CSV.foreach('result/data.csv',:headers=>true)do |row|
increment=row['count'].to_i
# calculate total loops
dns[:total] += incrementifrow['dir'] == dns[:dir]
bind9[:total] += incrementifrow['dir'] == bind9[:dir]
export[:total] += incrementifrow['dir'] == export[:dir]
irs[:total] += incrementifrow['dir'] == irs[:dir]
isc[:total] += incrementifrow['dir'] == isc[:dir]
isccc[:total] += incrementifrow['dir'] == isccc[:dir]
isccfg[:total] += incrementifrow['dir'] == isccfg[:dir]
lwres[:total] += incrementifrow['dir'] == lwres[:dir]
test[:total] += incrementifrow['dir'] == test[:dir]
# counts whiles and fors
dns[:while] += incrementifrow['dir'] == dns[:dir]androw['loop'] == 'while'
bind9[:while] += incrementifrow['dir'] == bind9[:dir]androw['loop'] == 'while'
export[:while] += incrementifrow['dir'] == export[:dir]androw['loop'] == 'while'
irs[:while] += incrementifrow['dir'] == irs[:dir]androw['loop'] == 'while'
isc[:while] += incrementifrow['dir'] == isc[:dir]androw['loop'] == 'while'
isccc[:while] += incrementifrow['dir'] == isccc[:dir]androw['loop'] == 'while'
isccfg[:while] += incrementifrow['dir'] == isccfg[:dir]androw['loop'] == 'while'
lwres[:while] += incrementifrow['dir'] == lwres[:dir]androw['loop'] == 'while'
test[:while] += incrementifrow['dir'] == test[:dir]androw['loop'] == 'while'
end
# counts the for loops
dns[:for]=dns[:total] - dns[:while]
bind9[:for]=bind9[:total] - bind9[:while]
export[:for]=export[:total] - export[:while]
irs[:for]=irs[:total] - irs[:while]
isc[:for]=isc[:total] - isc[:while]
isccc[:for]=isccc[:total] - isccc[:while]
isccfg[:for]=isccfg[:total] - isccfg[:while]
lwres[:for]=lwres[:total] - lwres[:while]
test[:for]=test[:total] - test[:while]
output" Directory \t| While Loops | For Whiles | Total "
output" #{dns[:dir]}#{dns[:while]}#{dns[:for]}#{dns[:total]}"
output" #{bind9[:dir]}#{bind9[:while]}#{bind9[:for]}#{bind9[:total]}"
output" #{export[:dir]}#{export[:while]}#{export[:for]}#{export[:total]}"
output" #{irs[:dir]}#{irs[:while]}#{irs[:for]}#{irs[:total]}"
output" #{isc[:dir]}#{isc[:while]}#{isc[:for]}#{isc[:total]}"
output" #{isccc[:dir]}#{isccc[:while]}#{isccc[:for]}#{isccc[:total]}"
output" #{isccfg[:dir]}#{isccfg[:while]}#{isccfg[:for]}#{isccfg[:total]}"
output" #{lwres[:dir]}#{lwres[:while]}#{lwres[:for]}#{lwres[:total]}"
output" #{test[:dir]}#{test[:while]}#{test[:for]}#{test[:total]}"
chart({})
end
defchart(hash)
hash
# TODO (Huascar) use google charts gem to create a chart
end
overview
breakdown