2003-01-01 10:41:25 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
"""Show file statistics by extension."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
class Stats:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.stats = {}
|
|
|
|
|
|
|
|
def statargs(self, args):
|
|
|
|
for arg in args:
|
|
|
|
if os.path.isdir(arg):
|
|
|
|
self.statdir(arg)
|
|
|
|
elif os.path.isfile(arg):
|
|
|
|
self.statfile(arg)
|
|
|
|
else:
|
|
|
|
sys.stderr.write("Can't find %s\n" % file)
|
|
|
|
self.addstats("<???>", "unknown", 1)
|
|
|
|
|
|
|
|
def statdir(self, dir):
|
|
|
|
self.addstats("<dir>", "dirs", 1)
|
|
|
|
try:
|
|
|
|
names = os.listdir(dir)
|
|
|
|
except os.error, err:
|
|
|
|
sys.stderr.write("Can't list %s: %s\n" % (file, err))
|
|
|
|
self.addstats(ext, "unlistable", 1)
|
|
|
|
return
|
|
|
|
names.sort()
|
|
|
|
for name in names:
|
2003-04-09 16:10:46 -03:00
|
|
|
if name.startswith(".#"):
|
|
|
|
continue # Skip CVS temp files
|
|
|
|
if name.endswith("~"):
|
|
|
|
continue# Skip Emacs backup files
|
2003-01-01 10:41:25 -04:00
|
|
|
full = os.path.join(dir, name)
|
|
|
|
if os.path.islink(full):
|
|
|
|
self.addstats("<lnk>", "links", 1)
|
|
|
|
elif os.path.isdir(full):
|
|
|
|
self.statdir(full)
|
|
|
|
else:
|
|
|
|
self.statfile(full)
|
|
|
|
|
|
|
|
def statfile(self, file):
|
|
|
|
head, ext = os.path.splitext(file)
|
|
|
|
head, base = os.path.split(file)
|
|
|
|
if ext == base:
|
2003-04-09 16:10:46 -03:00
|
|
|
ext = "" # E.g. .cvsignore is deemed not to have an extension
|
|
|
|
ext = os.path.normcase(ext)
|
|
|
|
if not ext:
|
|
|
|
ext = "<none>"
|
2003-01-01 10:41:25 -04:00
|
|
|
self.addstats(ext, "files", 1)
|
|
|
|
try:
|
|
|
|
f = open(file, "rb")
|
|
|
|
except IOError, err:
|
|
|
|
sys.stderr.write("Can't open %s: %s\n" % (file, err))
|
|
|
|
self.addstats(ext, "unopenable", 1)
|
|
|
|
return
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
self.addstats(ext, "bytes", len(data))
|
|
|
|
if '\0' in data:
|
|
|
|
self.addstats(ext, "binary", 1)
|
|
|
|
return
|
|
|
|
if not data:
|
|
|
|
self.addstats(ext, "empty", 1)
|
|
|
|
#self.addstats(ext, "chars", len(data))
|
|
|
|
lines = data.splitlines()
|
|
|
|
self.addstats(ext, "lines", len(lines))
|
|
|
|
del lines
|
|
|
|
words = data.split()
|
|
|
|
self.addstats(ext, "words", len(words))
|
|
|
|
|
|
|
|
def addstats(self, ext, key, n):
|
|
|
|
d = self.stats.setdefault(ext, {})
|
|
|
|
d[key] = d.get(key, 0) + n
|
|
|
|
|
|
|
|
def report(self):
|
|
|
|
exts = self.stats.keys()
|
|
|
|
exts.sort()
|
|
|
|
# Get the column keys
|
|
|
|
columns = {}
|
|
|
|
for ext in exts:
|
|
|
|
columns.update(self.stats[ext])
|
|
|
|
cols = columns.keys()
|
|
|
|
cols.sort()
|
2003-04-09 16:10:46 -03:00
|
|
|
colwidth = {}
|
|
|
|
colwidth["ext"] = max([len(ext) for ext in exts])
|
|
|
|
minwidth = 6
|
|
|
|
self.stats["TOTAL"] = {}
|
2003-01-01 10:41:25 -04:00
|
|
|
for col in cols:
|
2003-04-09 16:10:46 -03:00
|
|
|
total = 0
|
|
|
|
cw = max(minwidth, len(col))
|
|
|
|
for ext in exts:
|
2003-01-01 10:41:25 -04:00
|
|
|
value = self.stats[ext].get(col)
|
|
|
|
if value is None:
|
2003-04-09 16:10:46 -03:00
|
|
|
w = 0
|
2003-01-01 10:41:25 -04:00
|
|
|
else:
|
2003-04-09 16:10:46 -03:00
|
|
|
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")
|
|
|
|
for ext in exts:
|
|
|
|
self.stats[ext]["ext"] = ext
|
|
|
|
cols.insert(0, "ext")
|
|
|
|
def printheader():
|
|
|
|
for col in cols:
|
|
|
|
print "%*s" % (colwidth[col], col),
|
2003-01-01 10:41:25 -04:00
|
|
|
print
|
2003-04-09 16:10:46 -03:00
|
|
|
printheader()
|
|
|
|
for ext in exts:
|
|
|
|
for col in cols:
|
|
|
|
value = self.stats[ext].get(col, "")
|
|
|
|
print "%*s" % (colwidth[col], value),
|
|
|
|
print
|
|
|
|
printheader() # Another header at the bottom
|
2003-01-01 10:41:25 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
args = sys.argv[1:]
|
|
|
|
if not args:
|
|
|
|
args = [os.curdir]
|
|
|
|
s = Stats()
|
|
|
|
s.statargs(args)
|
|
|
|
s.report()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|