Merged revisions 76156 via svnmerge from
[python/dscho.git] / Tools / scripts / byext.py
blobe5b090c8e507f17cb759d3d237fe10be8ec15378
1 #! /usr/bin/env python3.0
3 """Show file statistics by extension."""
5 import os
6 import sys
8 class Stats:
10 def __init__(self):
11 self.stats = {}
13 def statargs(self, args):
14 for arg in args:
15 if os.path.isdir(arg):
16 self.statdir(arg)
17 elif os.path.isfile(arg):
18 self.statfile(arg)
19 else:
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)
25 try:
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)
30 return
31 names.sort()
32 for name in names:
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):
41 self.statdir(full)
42 else:
43 self.statfile(full)
45 def statfile(self, filename):
46 head, ext = os.path.splitext(filename)
47 head, base = os.path.split(filename)
48 if ext == base:
49 ext = "" # E.g. .cvsignore is deemed not to have an extension
50 ext = os.path.normcase(ext)
51 if not ext:
52 ext = "<none>"
53 self.addstats(ext, "files", 1)
54 try:
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)
59 return
60 data = f.read()
61 f.close()
62 self.addstats(ext, "bytes", len(data))
63 if b'\0' in data:
64 self.addstats(ext, "binary", 1)
65 return
66 if not data:
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))
71 del lines
72 words = data.split()
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
79 def report(self):
80 exts = sorted(self.stats)
81 # Get the column keys
82 columns = {}
83 for ext in exts:
84 columns.update(self.stats[ext])
85 cols = sorted(columns)
86 colwidth = {}
87 colwidth["ext"] = max([len(ext) for ext in exts])
88 minwidth = 6
89 self.stats["TOTAL"] = {}
90 for col in cols:
91 total = 0
92 cw = max(minwidth, len(col))
93 for ext in exts:
94 value = self.stats[ext].get(col)
95 if value is None:
96 w = 0
97 else:
98 w = len("%d" % value)
99 total += value
100 cw = max(cw, w)
101 cw = max(cw, len(str(total)))
102 colwidth[col] = cw
103 self.stats["TOTAL"][col] = total
104 exts.append("TOTAL")
105 for ext in exts:
106 self.stats[ext]["ext"] = ext
107 cols.insert(0, "ext")
108 def printheader():
109 for col in cols:
110 print("%*s" % (colwidth[col], col), end=' ')
111 print()
112 printheader()
113 for ext in exts:
114 for col in cols:
115 value = self.stats[ext].get(col, "")
116 print("%*s" % (colwidth[col], value), end=' ')
117 print()
118 printheader() # Another header at the bottom
120 def main():
121 args = sys.argv[1:]
122 if not args:
123 args = [os.curdir]
124 s = Stats()
125 s.statargs(args)
126 s.report()
128 if __name__ == "__main__":
129 main()