byext.py 3.83 KB
Newer Older
1
#! /usr/bin/env python3
2 3 4 5 6 7

"""Show file statistics by extension."""

import os
import sys

8

9 10 11 12 13 14 15 16 17 18 19 20
class Stats:

    def __init__(self):
        self.stats = {}

    def statargs(self, args):
        for arg in args:
            if os.path.isdir(arg):
                self.statdir(arg)
            elif os.path.isfile(arg):
                self.statfile(arg)
            else:
21
                sys.stderr.write("Can't find %s\n" % arg)
22 23 24 25 26 27
                self.addstats("<???>", "unknown", 1)

    def statdir(self, dir):
        self.addstats("<dir>", "dirs", 1)
        try:
            names = os.listdir(dir)
28
        except os.error as err:
29 30
            sys.stderr.write("Can't list %s: %s\n" % (dir, err))
            self.addstats("<dir>", "unlistable", 1)
31
            return
32
        for name in sorted(names):
33
            if name.startswith(".#"):
34
                continue  # Skip CVS temp files
35
            if name.endswith("~"):
36
                continue  # Skip Emacs backup files
37 38 39 40 41 42 43 44
            full = os.path.join(dir, name)
            if os.path.islink(full):
                self.addstats("<lnk>", "links", 1)
            elif os.path.isdir(full):
                self.statdir(full)
            else:
                self.statfile(full)

45 46 47
    def statfile(self, filename):
        head, ext = os.path.splitext(filename)
        head, base = os.path.split(filename)
48
        if ext == base:
49
            ext = ""  # E.g. .cvsignore is deemed not to have an extension
50 51 52
        ext = os.path.normcase(ext)
        if not ext:
            ext = "<none>"
53 54
        self.addstats(ext, "files", 1)
        try:
55 56
            with open(filename, "rb") as f:
                data = f.read()
57
        except IOError as err:
58
            sys.stderr.write("Can't open %s: %s\n" % (filename, err))
59 60 61
            self.addstats(ext, "unopenable", 1)
            return
        self.addstats(ext, "bytes", len(data))
62
        if b'\0' in data:
63 64 65 66
            self.addstats(ext, "binary", 1)
            return
        if not data:
            self.addstats(ext, "empty", 1)
67
        # self.addstats(ext, "chars", len(data))
68
        lines = str(data, "latin-1").splitlines()
69 70 71 72 73 74 75 76 77 78
        self.addstats(ext, "lines", len(lines))
        del lines
        words = data.split()
        self.addstats(ext, "words", len(words))

    def addstats(self, ext, key, n):
        d = self.stats.setdefault(ext, {})
        d[key] = d.get(key, 0) + n

    def report(self):
79
        exts = sorted(self.stats)
80 81 82 83
        # Get the column keys
        columns = {}
        for ext in exts:
            columns.update(self.stats[ext])
84
        cols = sorted(columns)
85 86 87 88
        colwidth = {}
        colwidth["ext"] = max([len(ext) for ext in exts])
        minwidth = 6
        self.stats["TOTAL"] = {}
89
        for col in cols:
90 91 92
            total = 0
            cw = max(minwidth, len(col))
            for ext in exts:
93 94
                value = self.stats[ext].get(col)
                if value is None:
95
                    w = 0
96
                else:
97 98 99 100 101 102 103 104 105 106
                    w = len("%d" % value)
                    total += value
                cw = max(cw, w)
            cw = max(cw, len(str(total)))
            colwidth[col] = cw
            self.stats["TOTAL"][col] = total
        exts.append("TOTAL")
        for ext in exts:
            self.stats[ext]["ext"] = ext
        cols.insert(0, "ext")
107

108 109
        def printheader():
            for col in cols:
110 111
                print("%*s" % (colwidth[col], col), end=' ')
            print()
112

113 114 115 116
        printheader()
        for ext in exts:
            for col in cols:
                value = self.stats[ext].get(col, "")
117 118
                print("%*s" % (colwidth[col], value), end=' ')
            print()
119 120
        printheader()  # Another header at the bottom

121 122 123 124 125 126 127 128 129

def main():
    args = sys.argv[1:]
    if not args:
        args = [os.curdir]
    s = Stats()
    s.statargs(args)
    s.report()

130

131 132
if __name__ == "__main__":
    main()