1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """Show file statistics by extension.""" |
---|
4 | n/a | |
---|
5 | n/a | import os |
---|
6 | n/a | import sys |
---|
7 | n/a | |
---|
8 | n/a | |
---|
9 | n/a | class Stats: |
---|
10 | n/a | |
---|
11 | n/a | def __init__(self): |
---|
12 | n/a | self.stats = {} |
---|
13 | n/a | |
---|
14 | n/a | def statargs(self, args): |
---|
15 | n/a | for arg in args: |
---|
16 | n/a | if os.path.isdir(arg): |
---|
17 | n/a | self.statdir(arg) |
---|
18 | n/a | elif os.path.isfile(arg): |
---|
19 | n/a | self.statfile(arg) |
---|
20 | n/a | else: |
---|
21 | n/a | sys.stderr.write("Can't find %s\n" % arg) |
---|
22 | n/a | self.addstats("<???>", "unknown", 1) |
---|
23 | n/a | |
---|
24 | n/a | def statdir(self, dir): |
---|
25 | n/a | self.addstats("<dir>", "dirs", 1) |
---|
26 | n/a | try: |
---|
27 | n/a | names = os.listdir(dir) |
---|
28 | n/a | except OSError as err: |
---|
29 | n/a | sys.stderr.write("Can't list %s: %s\n" % (dir, err)) |
---|
30 | n/a | self.addstats("<dir>", "unlistable", 1) |
---|
31 | n/a | return |
---|
32 | n/a | for name in sorted(names): |
---|
33 | n/a | if name.startswith(".#"): |
---|
34 | n/a | continue # Skip CVS temp files |
---|
35 | n/a | if name.endswith("~"): |
---|
36 | n/a | continue # Skip Emacs backup files |
---|
37 | n/a | full = os.path.join(dir, name) |
---|
38 | n/a | if os.path.islink(full): |
---|
39 | n/a | self.addstats("<lnk>", "links", 1) |
---|
40 | n/a | elif os.path.isdir(full): |
---|
41 | n/a | self.statdir(full) |
---|
42 | n/a | else: |
---|
43 | n/a | self.statfile(full) |
---|
44 | n/a | |
---|
45 | n/a | def statfile(self, filename): |
---|
46 | n/a | head, ext = os.path.splitext(filename) |
---|
47 | n/a | head, base = os.path.split(filename) |
---|
48 | n/a | if ext == base: |
---|
49 | n/a | ext = "" # E.g. .cvsignore is deemed not to have an extension |
---|
50 | n/a | ext = os.path.normcase(ext) |
---|
51 | n/a | if not ext: |
---|
52 | n/a | ext = "<none>" |
---|
53 | n/a | self.addstats(ext, "files", 1) |
---|
54 | n/a | try: |
---|
55 | n/a | with open(filename, "rb") as f: |
---|
56 | n/a | data = f.read() |
---|
57 | n/a | except IOError as err: |
---|
58 | n/a | sys.stderr.write("Can't open %s: %s\n" % (filename, err)) |
---|
59 | n/a | self.addstats(ext, "unopenable", 1) |
---|
60 | n/a | return |
---|
61 | n/a | self.addstats(ext, "bytes", len(data)) |
---|
62 | n/a | if b'\0' in data: |
---|
63 | n/a | self.addstats(ext, "binary", 1) |
---|
64 | n/a | return |
---|
65 | n/a | if not data: |
---|
66 | n/a | self.addstats(ext, "empty", 1) |
---|
67 | n/a | # self.addstats(ext, "chars", len(data)) |
---|
68 | n/a | lines = str(data, "latin-1").splitlines() |
---|
69 | n/a | self.addstats(ext, "lines", len(lines)) |
---|
70 | n/a | del lines |
---|
71 | n/a | words = data.split() |
---|
72 | n/a | self.addstats(ext, "words", len(words)) |
---|
73 | n/a | |
---|
74 | n/a | def addstats(self, ext, key, n): |
---|
75 | n/a | d = self.stats.setdefault(ext, {}) |
---|
76 | n/a | d[key] = d.get(key, 0) + n |
---|
77 | n/a | |
---|
78 | n/a | def report(self): |
---|
79 | n/a | exts = sorted(self.stats) |
---|
80 | n/a | # Get the column keys |
---|
81 | n/a | columns = {} |
---|
82 | n/a | for ext in exts: |
---|
83 | n/a | columns.update(self.stats[ext]) |
---|
84 | n/a | cols = sorted(columns) |
---|
85 | n/a | colwidth = {} |
---|
86 | n/a | colwidth["ext"] = max([len(ext) for ext in exts]) |
---|
87 | n/a | minwidth = 6 |
---|
88 | n/a | self.stats["TOTAL"] = {} |
---|
89 | n/a | for col in cols: |
---|
90 | n/a | total = 0 |
---|
91 | n/a | cw = max(minwidth, len(col)) |
---|
92 | n/a | for ext in exts: |
---|
93 | n/a | value = self.stats[ext].get(col) |
---|
94 | n/a | if value is None: |
---|
95 | n/a | w = 0 |
---|
96 | n/a | else: |
---|
97 | n/a | w = len("%d" % value) |
---|
98 | n/a | total += value |
---|
99 | n/a | cw = max(cw, w) |
---|
100 | n/a | cw = max(cw, len(str(total))) |
---|
101 | n/a | colwidth[col] = cw |
---|
102 | n/a | self.stats["TOTAL"][col] = total |
---|
103 | n/a | exts.append("TOTAL") |
---|
104 | n/a | for ext in exts: |
---|
105 | n/a | self.stats[ext]["ext"] = ext |
---|
106 | n/a | cols.insert(0, "ext") |
---|
107 | n/a | |
---|
108 | n/a | def printheader(): |
---|
109 | n/a | for col in cols: |
---|
110 | n/a | print("%*s" % (colwidth[col], col), end=' ') |
---|
111 | n/a | print() |
---|
112 | n/a | |
---|
113 | n/a | printheader() |
---|
114 | n/a | for ext in exts: |
---|
115 | n/a | for col in cols: |
---|
116 | n/a | value = self.stats[ext].get(col, "") |
---|
117 | n/a | print("%*s" % (colwidth[col], value), end=' ') |
---|
118 | n/a | print() |
---|
119 | n/a | printheader() # Another header at the bottom |
---|
120 | n/a | |
---|
121 | n/a | |
---|
122 | n/a | def main(): |
---|
123 | n/a | args = sys.argv[1:] |
---|
124 | n/a | if not args: |
---|
125 | n/a | args = [os.curdir] |
---|
126 | n/a | s = Stats() |
---|
127 | n/a | s.statargs(args) |
---|
128 | n/a | s.report() |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | if __name__ == "__main__": |
---|
132 | n/a | main() |
---|