- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathbyext.py
executable file
·130 lines (118 loc) · 3.85 KB
/
byext.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
#! /usr/bin/env python
"""Show file statistics by extension."""
from __future__ importprint_function
importos
importsys
classStats:
def__init__(self):
self.stats= {}
defstatargs(self, args):
forarginargs:
ifos.path.isdir(arg):
self.statdir(arg)
elifos.path.isfile(arg):
self.statfile(arg)
else:
sys.stderr.write("Can't find %s\n"%arg)
self.addstats("<???>", "unknown", 1)
defstatdir(self, dir):
self.addstats("<dir>", "dirs", 1)
try:
names=sorted(os.listdir(dir))
exceptos.erroraserr:
sys.stderr.write("Can't list %s: %s\n"% (dir, err))
self.addstats("<dir>", "unlistable", 1)
return
fornameinnames:
ifname.startswith(".#"):
continue# Skip CVS temp files
ifname.endswith("~"):
continue# Skip Emacs backup files
full=os.path.join(dir, name)
ifos.path.islink(full):
self.addstats("<lnk>", "links", 1)
elifos.path.isdir(full):
self.statdir(full)
else:
self.statfile(full)
defstatfile(self, filename):
head, ext=os.path.splitext(filename)
head, base=os.path.split(filename)
ifext==base:
ext=""# E.g. .cvsignore is deemed not to have an extension
ext=os.path.normcase(ext)
ifnotext:
ext="<none>"
self.addstats(ext, "files", 1)
try:
f=open(filename, "rb")
exceptIOErroraserr:
sys.stderr.write("Can't open %s: %s\n"% (filename, err))
self.addstats(ext, "unopenable", 1)
return
data=f.read()
f.close()
self.addstats(ext, "bytes", len(data))
ifb'\0'indata:
self.addstats(ext, "binary", 1)
return
ifnotdata:
self.addstats(ext, "empty", 1)
#self.addstats(ext, "chars", len(data))
lines=data.splitlines()
self.addstats(ext, "lines", len(lines))
dellines
words=data.split()
self.addstats(ext, "words", len(words))
defaddstats(self, ext, key, n):
d=self.stats.setdefault(ext, {})
d[key] =d.get(key, 0) +n
defreport(self):
exts=sorted(self.stats.keys())
# Get the column keys
columns= {}
forextinexts:
columns.update(self.stats[ext])
cols=sorted(columns.keys())
colwidth= {}
colwidth["ext"] =max([len(ext) forextinexts])
minwidth=6
self.stats["TOTAL"] = {}
forcolincols:
total=0
cw=max(minwidth, len(col))
forextinexts:
value=self.stats[ext].get(col)
ifvalueisNone:
w=0
else:
w=len("%d"%value)
total+=value
cw=max(cw, w)
cw=max(cw, len(str(total)))
colwidth[col] =cw
self.stats["TOTAL"][col] =total
exts.append("TOTAL")
forextinexts:
self.stats[ext]["ext"] =ext
cols.insert(0, "ext")
defprintheader():
forcolincols:
print("%*s"% (colwidth[col], col), end=" ")
print()
printheader()
forextinexts:
forcolincols:
value=self.stats[ext].get(col, "")
print("%*s"% (colwidth[col], value), end=" ")
print()
printheader() # Another header at the bottom
defmain():
args=sys.argv[1:]
ifnotargs:
args= [os.curdir]
s=Stats()
s.statargs(args)
s.report()
if__name__=="__main__":
main()