3 """Show file statistics by extension."""
13 def statargs(self
, args
):
15 if os
.path
.isdir(arg
):
17 elif os
.path
.isfile(arg
):
20 sys
.stderr
.write("Can't find %s\n" % arg
)
21 self
.addstats("<???>", "unknown", 1)
23 def statdir(self
, dir):
24 self
.addstats("<dir>", "dirs", 1)
26 names
= os
.listdir(dir)
28 sys
.stderr
.write("Can't list %s: %s\n" % (dir, err
))
29 self
.addstats("<dir>", "unlistable", 1)
33 if name
.startswith(".#"):
34 continue # Skip CVS temp files
35 if name
.endswith("~"):
36 continue# Skip Emacs backup files
37 full
= os
.path
.join(dir, name
)
38 if os
.path
.islink(full
):
39 self
.addstats("<lnk>", "links", 1)
40 elif os
.path
.isdir(full
):
45 def statfile(self
, filename
):
46 head
, ext
= os
.path
.splitext(filename
)
47 head
, base
= os
.path
.split(filename
)
49 ext
= "" # E.g. .cvsignore is deemed not to have an extension
50 ext
= os
.path
.normcase(ext
)
53 self
.addstats(ext
, "files", 1)
55 f
= open(filename
, "rb")
57 sys
.stderr
.write("Can't open %s: %s\n" % (filename
, err
))
58 self
.addstats(ext
, "unopenable", 1)
62 self
.addstats(ext
, "bytes", len(data
))
64 self
.addstats(ext
, "binary", 1)
67 self
.addstats(ext
, "empty", 1)
68 #self.addstats(ext, "chars", len(data))
69 lines
= data
.splitlines()
70 self
.addstats(ext
, "lines", len(lines
))
73 self
.addstats(ext
, "words", len(words
))
75 def addstats(self
, ext
, key
, n
):
76 d
= self
.stats
.setdefault(ext
, {})
77 d
[key
] = d
.get(key
, 0) + n
80 exts
= self
.stats
.keys()
85 columns
.update(self
.stats
[ext
])
89 colwidth
["ext"] = max([len(ext
) for ext
in exts
])
91 self
.stats
["TOTAL"] = {}
94 cw
= max(minwidth
, len(col
))
96 value
= self
.stats
[ext
].get(col
)
100 w
= len("%d" % value
)
103 cw
= max(cw
, len(str(total
)))
105 self
.stats
["TOTAL"][col
] = total
108 self
.stats
[ext
]["ext"] = ext
109 cols
.insert(0, "ext")
112 print "%*s" % (colwidth
[col
], col
),
117 value
= self
.stats
[ext
].get(col
, "")
118 print "%*s" % (colwidth
[col
], value
),
120 printheader() # Another header at the bottom
130 if __name__
== "__main__":