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