| 1 | #! /usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | """Show file statistics by extension."""
|
|---|
| 4 |
|
|---|
| 5 | import os
|
|---|
| 6 | import sys
|
|---|
| 7 |
|
|---|
| 8 | class Stats:
|
|---|
| 9 |
|
|---|
| 10 | def __init__(self):
|
|---|
| 11 | self.stats = {}
|
|---|
| 12 |
|
|---|
| 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)
|
|---|
| 22 |
|
|---|
| 23 | def statdir(self, dir):
|
|---|
| 24 | self.addstats("<dir>", "dirs", 1)
|
|---|
| 25 | try:
|
|---|
| 26 | names = os.listdir(dir)
|
|---|
| 27 | except os.error, 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)
|
|---|
| 44 |
|
|---|
| 45 | def statfile(self, filename):
|
|---|
| 46 | head, ext = os.path.splitext(filename)
|
|---|
|
|---|