forked from xonixx/zlo-search
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.py
84 lines (66 loc) · 1.97 KB
/
stats.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
import os
import os.path
src_dirs = [
r'D:\TEST\JAVA\ZloSearcher\trunk\zlo_search\src',
r'D:\TEST\JAVA\ZloSearcher\trunk\zlo_web\src',
r'D:\TEST\JAVA\ZloSearcher\trunk\zlo_web\WEB-INF',
r'D:\TEST\JAVA\ZloSearcher\trunk\zlo_web\resources',
r'D:\TEST\JAVA\ZloSearcher\trunk\zlo_search\DB Scripts']
exts = [
'java', 'jsp', 'xml', 'properties', 'sql', 'css', 'js'
]
exclude_dirs = [
'.svn', 'exploded'
]
TRACE = False
def process_dir(_dir):
if os.path.basename(_dir) in exclude_dirs:
return
if TRACE: print 'Scanning', _dir
for _file in os.listdir(_dir):
f = _dir + '/' + _file
if os.path.isdir(f):
process_dir(f)
elif os.path.isfile(f) and any([f.endswith('.' + ext) for ext in exts]):
process_file(f)
def process_file(f_name):
f = open(f_name)
file_content = f.read()
f.close()
if TRACE: print '\tFile:', f_name, get_loc(file_content), get_f_size(f_name)
ext = get_ext(f_name)
stat[ext]["loc"] += get_loc(file_content)
stat[ext]["size"] += get_f_size(f_name)
stat[ext]["count"] += 1
def get_ext(f_name):
if '.' in f_name:
return f_name.rsplit('.', 1)[1]
else:
return ''
def get_loc(content):
return len(content.split('\n'))
def get_f_size(f_name):
return os.path.getsize(f_name)
def init():
global stat
stat = {}
for ext in exts:
stat[ext] = {"count":0, "loc":0, "size":0}
def format_size(size):
return ''.join(['{', str(size / 1024), 'kb ', str(size % 1024), 'b}'])
def report():
total_loc = 0
total_size = 0
total_cnt = 0
print "Ext | count | loc | size"
for ext in stat:
print " | ".join(str(s) for s in [ext, stat[ext]["count"], stat[ext]["loc"] , format_size(stat[ext]["size"])])
total_loc += stat[ext]["loc"]
total_size += stat[ext]["size"]
total_cnt += stat[ext]["count"]
print " | ".join(str(s) for s in ["Total", total_cnt, total_loc, format_size(total_size)])
if __name__ == '__main__':
init()
for d in src_dirs:
process_dir(d)
report()