-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.py
390 lines (297 loc) · 12.8 KB
/
size.py
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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import os, string, sys, shutil
from subprocess import call
import csv
import collections
remove_xml = True
#Function to check file counts per directory
#
#find ./ -type f | grep -E ".*\.[a-zA-Z0-9]*$" | sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/' | sort | uniq -c | sort -n
#android_branches = [ 'android-1.5r4', 'android-1.6_r2', 'android-2.1_r2.1s', 'android-2.2.3_r2.1', 'android-2.3.7_r1', 'android-4.0.4_r2.1', 'android-4.1.2_r1', 'android-4.2.2_r1' ]
#android_name = [ 'cupcake', 'donut', 'eclair', 'froyo', 'gingerbread', 'icecream-sandwidth', 'jellybean 4.1', 'jellybean 4.2' ]
android_branches = [ 'android-1.6_r2', 'android-2.1_r2.1s', 'android-2.2.3_r2.1', 'android-2.3.7_r1', 'android-4.0.4_r2.1', 'android-4.1.2_r1', 'android-4.2.2_r1' ]
android_name = [ 'donut', 'eclair', 'froyo', 'gingerbread', 'icecream-sandwidth', 'jellybean 4.1', 'jellybean 4.2' ]
#high level dirs
high_level_dirs = ['development', 'external', 'abi', 'bionic', 'sdk', 'gdk', 'cts', 'libnativehelper', 'system', 'libcore', 'docs', 'prebuilts', 'dalvik', 'pdk', 'packages', 'bootable', 'build', 'ndk', 'frameworks', 'vendor', 'prebuilt', 'device', 'hardware']
device_android = ['common', 'generic', 'google', 'sample' ]
device_vendor = ['asus', 'lge', 'samsung', 'ti', 'moto', 'htc' ]
hardware_android = ['libhardware', 'libhardware_legacy', 'ril' ]
hardware_vendor = ['broadcom', 'invensense', 'msm7k', 'qcom', 'samsung_slsi', 'ti' ]
#CLOC
cloc_columns = [ 'files', 'language', 'blank', 'comment', 'code' ]
cloc_columns_minus_lang = list(cloc_columns)
cloc_columns_minus_lang.remove('language')
cloc_columns_minus_lang_files = list(cloc_columns_minus_lang)
cloc_columns_minus_lang_files.remove('files')
#My types
classification = [ 'native', 'build_and_tools', 'framework', 'apps', 'dev' ]
file_types = [ 'java', 'native', 'build_scripts', 'xml' ]
#classification for the top level directories
external = [ 'external', 'prebuilt', 'prebuilts' ]
native = [ 'abi', 'bionic', 'bootable', 'hardware', 'system' ]
build_and_tools = [ 'build', 'pdk', 'device', 'vendor' ]
framework = [ 'development', 'dalvik', 'frameworks', 'libcore', 'libnativehelper' ]
apps = [ 'packages' ]
dev = [ 'cts', 'ndk', 'gdk', 'docs', 'sdk' ]
def CheckoutRepo( path, branch ):
shutil.rmtree(path, True )
os.makedirs(path)
os.chdir(path)
cmd = "repo init -u https://android.googlesource.com/platform/manifest -b " + branch
os.system( cmd )
cmd = "repo sync"
os.system( cmd )
def StripGitRepos( path ):
os.chdir(path)
#get the big guy first...
shutil.rmtree( ".repo" )
#now lets prune all the individual git repos
for root, dirs, files in os.walk(path):
for name in dirs:
if '.git' in name:
p = os.path.join(root, name)
print "Deleteing: ", p
shutil.rmtree( p )
def HighLevelDirs( path ):
out = []
for item in os.listdir(path):
if os.path.isdir(os.path.join(path, item)):
out.append( item )
return out
def ParseClocResults( filename ):
#create a dict for the 4 columns
d = {}
for f in cloc_columns:
d[f] = []
#read the data from the CSV file
dictReader = csv.DictReader(open(filename, 'rb'), fieldnames = cloc_columns, delimiter = ',', quotechar = '"')
dictReader.next()
#for each row of the CSV data returned, add to the column dict
for row in dictReader:
for key in row:
try:
d[key].append(row[key])
except KeyError:
continue
return d
#returns a Dict with key for the high level directory, the value being a dict of the file types and breakdown (lines, comments etc...)
def CountLinesOfCode( path, branch, high_level_dirs ):
out = {}
for d in high_level_dirs:
if d == "out":
continue
dir = path + "/" + d
tmpfile = dir + ".txt"
cmd = "cloc " + branch + "/" + d + " --quiet --csv --progress-rate=0 --force-lang=\"make\",mk --report-file=" + tmpfile
#does the directory exist first?
if os.path.exists( dir ):
if not os.path.exists( tmpfile ):
#print cmd
os.system( cmd )
o = {}
if os.path.exists( tmpfile ):
o = ParseClocResults( tmpfile )
out[d] = o
#else:
#print "Directory doesn't exist: ", dir
return out
#output dict is a list of src files
def ParseStats( dir_dict, output_dict, stat_type ):
if len(dir_dict) == 0:
return
language = dir_dict['language']
java_l = [ 'Java' ] #'Javascript'
native_l = [ 'C++', 'C/C++ Header', 'C', 'Assembly' ]
build_scripts_l = [ 'Python', 'make', 'Bourne Shell', 'Bourne Again Shell' ]
xml_l = [ 'XML' ]
d = dir_dict[ stat_type ]
for c, l in enumerate(language):
if l in java_l:
output_dict['java'] = output_dict['java'] + int(d[c])
elif l in native_l:
output_dict['native'] = output_dict['native'] + int(d[c])
elif l in build_scripts_l:
output_dict['build_scripts'] = output_dict['build_scripts'] + int(d[c])
elif l in xml_l:
if remove_xml == False:
output_dict['xml'] = output_dict['xml'] + int(d[c])
#Generate statistics per branch
#Outputs coarse data for 5 catagories, code/comments/num files
def ClassifyStats( branch_stats ):
out = {}
for branch in branch_stats:
branch_summary = {}
branch_dir_stats = branch_stats[branch]
for type in cloc_columns_minus_lang:
native_d = {'java': 0, 'native': 0, 'build_scripts': 0, 'xml': 0}
build_and_tools_d = {'java': 0, 'native': 0, 'build_scripts': 0, 'xml': 0}
framework_d = {'java': 0, 'native': 0, 'build_scripts': 0, 'xml': 0}
apps_d = {'java': 0, 'native': 0, 'build_scripts': 0, 'xml': 0}
dev_d = {'java': 0, 'native': 0, 'build_scripts': 0, 'xml': 0}
external_d = {'java': 0, 'native': 0, 'build_scripts': 0, 'xml': 0}
for dir in branch_dir_stats:
if dir in native:
ParseStats( branch_dir_stats[dir], native_d, type )
elif dir in build_and_tools:
ParseStats( branch_dir_stats[dir], build_and_tools_d, type )
elif dir in framework:
ParseStats( branch_dir_stats[dir], framework_d, type )
elif dir in apps:
ParseStats( branch_dir_stats[dir], apps_d, type )
elif dir in dev:
ParseStats( branch_dir_stats[dir], dev_d, type )
elif dir in external:
ParseStats( branch_dir_stats[dir], external_d, type )
stats = {}
stats['native'] = native_d
stats['build_and_tools'] = build_and_tools_d
stats['framework'] = framework_d
stats['apps'] = apps_d
stats['dev'] = dev_d
stats['external'] = external_d
#print branch, type, stats
branch_summary[type] = stats
out[branch] = branch_summary
return out
##################
## Calculate summary stats for all classifications of files
#key [ Directory in the Android tree ] ]
#value[ key [ column from cloc - files, code etc... ] ]
# value[ list() of counts, keyed from 'language' column ]
#################
def CalcSummaryByType( stats, type ):
branch_summary = {}
count = 0
for d in stats:
branch_summary[d] = 0
cloc_col = stats[d] #actual stats for this branch
cloc_list = cloc_col[type]
for c in cloc_list:
count = count + int(c)
return count
def PrintSummaryOfDirs( dirs ):
for d in dirs:
count = 0
cloc_col = dirs[d]
for c in cloc_columns_minus_lang_files:
for l in cloc_col[c]:
count = count + int(l)
print d + "," + str(count)
def PrintSummaryOfFilesBlankCommentCode( branch_stats ):
print ",Files,Code,Blank,Comments"
for b in branch_stats:
files_stats = CalcSummaryByType( branch_stats[b], "files" )
blank_stats = CalcSummaryByType( branch_stats[b], "blank" )
comment_stats = CalcSummaryByType( branch_stats[b], "comment" )
code_stats = CalcSummaryByType( branch_stats[b], "code" )
print b + "," + str(files_stats) + "," + str(blank_stats) + "," + str(comment_stats) + "," + str(code_stats)
#classified_stats is a dict of dicts, as follows
#key [ Android branch ]
#value[ key [ Stats type - files, blank, comment, code ] ]
# value[ key [ Classification - native, build_and_tools, framework, apps, dev ]
# value[ key [ file types - xml, build_scripts, java, native ] ]
# value[ count ]
def PrintClassifiedStats( stats ):
print "/nClassified statistics\n"
print ",xml,build_scripts,java,native"
for branch in stats:
stats_type = stats[branch]
stats_code = stats_type['code']
print branch,
count = 0
xml = 0
build_scripts = 0
java = 0
native = 0
for classification in stats_code:
file_types = stats_code[classification]
xml = xml + int(file_types['xml'])
build_scripts = build_scripts + int(file_types['build_scripts'])
java = java + int(file_types['java'])
native = native + int(file_types['native'])
print ",", xml, ",", build_scripts, ",", java, ",", native
print "/nClassified statistics\n"
#print ",build_and_tools,apps,dev,framework,external,native"
b = stats['android-1.6_r2']
c = b['code']
for d in stats_code: print ",", d,
print ""
for branch in stats:
stats_type = stats[branch]
stats_code = stats_type['code']
print branch,
for classification in stats_code:
file_types = stats_code[classification]
count = 0
for f in file_types:
count = count+ int(file_types[f])
print ",", count,
print ""
print "/nClassified statistics - comments\n"
#print ",build_and_tools,apps,dev,framework,external,native"
b = stats['android-1.6_r2']
c = b['comment']
for d in stats_code: print ",", d,
print ""
for branch in stats:
stats_type = stats[branch]
stats_code = stats_type['comment']
print branch,
for classification in stats_code:
file_types = stats_code[classification]
count = 0
for f in file_types:
count = count+ int(file_types[f])
print ",", count,
print ""
##################
## Main
#################
def main():
#assume that this is run from a directory where the android repos will be checked out
initial_path = os.getcwd()
#checkout the branches only if they don't exist already
for branch in android_branches:
branch_path = initial_path + "/" + branch
if not os.path.exists( branch_path ):
print "Branch does not exist - creating"
CheckoutRepo( branch_path, branch )
StripGitRepos( branch_path ) #this is just to save space
#get all the top level dirs from the last branch checked out
#last_branch_path = initial_path + "/" + android_branches[-1]
#high_level_dirs = HighLevelDirs( last_branch_path )
#high_level_dirs.append('vendor')
#high_level_dirs.append('prebuilt')
#if 'out' in high_level_dirs:
# high_level_dirs.remove('out')
all_device_dirs = high_level_dirs
#get all the lines of code from each branch
branch_stats = collections.OrderedDict()
for branch in android_branches:
branch_path = initial_path + "/" + branch
os.chdir( initial_path )
branch_stats[ branch ] = CountLinesOfCode( branch_path, branch, high_level_dirs )
#branch_stats is a dict of dicts, as follows
#key [ Android branch ]
#value[ key [ Directory in the Android tree ] ]
# value[ key [ column from cloc - files, code etc... ] ]
# value[ list() of counts, keyed from 'language' column ]
#print a little table summary of the 4 sections
print "Summary of high level projects for all directories\n"
PrintSummaryOfFilesBlankCommentCode( branch_stats )
#directory summary, currently per branch
print("android-4.2.2_r1:\n")
PrintSummaryOfDirs( branch_stats[ 'android-4.2.2_r1' ] )
print("android-4.1.2_r1:\n")
PrintSummaryOfDirs( branch_stats[ 'android-4.1.2_r1' ] )
#classify the statistics into high level groups, ignoring
classified_stats = ClassifyStats( branch_stats )
#classified_stats is a dict of dicts, as follows
#key [ Android branch ]
#value[ key [ Stats type - files, blank, comment, code ] ]
# value[ key [ Classification - native, build_and_tools, framework, apps, dev ]
# value[ key [ file types - xml, build_scripts, java, native ] ]
# value[ count ]
PrintClassifiedStats( classified_stats )
#usual python main thing
if __name__ == "__main__":
sys.exit(main())