1 #! /usr/bin/env python3.0
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)
27 except os
.error
as err
:
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")
56 except IOError as err
:
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
= str(data
, "latin-1").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
= sorted(self
.stats
)
84 columns
.update(self
.stats
[ext
])
85 cols
= sorted(columns
)
87 colwidth
["ext"] = max([len(ext
) for ext
in exts
])
89 self
.stats
["TOTAL"] = {}
92 cw
= max(minwidth
, len(col
))
94 value
= self
.stats
[ext
].get(col
)
101 cw
= max(cw
, len(str(total
)))
103 self
.stats
["TOTAL"][col
] = total
106 self
.stats
[ext
]["ext"] = ext
107 cols
.insert(0, "ext")
110 print("%*s" % (colwidth
[col
], col
), end
=' ')
115 value
= self
.stats
[ext
].get(col
, "")
116 print("%*s" % (colwidth
[col
], value
), end
=' ')
118 printheader() # Another header at the bottom
128 if __name__
== "__main__":