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