annotate Tools/scripts/byext.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents a18aa3fbdd4a
children b2d4e6fc4f19 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 #! /usr/bin/env python
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 """Show file statistics by extension."""
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import os
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import sys
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 class Stats:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 def __init__(self):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 self.stats = {}
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 def statargs(self, args):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 for arg in args:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 if os.path.isdir(arg):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 self.statdir(arg)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 elif os.path.isfile(arg):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 self.statfile(arg)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 else:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
20 sys.stderr.write("Can't find %s\n" % arg)
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 self.addstats("<???>", "unknown", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def statdir(self, dir):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 self.addstats("<dir>", "dirs", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 try:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 names = os.listdir(dir)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 except os.error, err:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
28 sys.stderr.write("Can't list %s: %s\n" % (dir, err))
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
29 self.addstats("<dir>", "unlistable", 1)
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 names.sort()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 for name in names:
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
33 if name.startswith(".#"):
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
34 continue # Skip CVS temp files
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
35 if name.endswith("~"):
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
36 continue# Skip Emacs backup files
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 full = os.path.join(dir, name)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 if os.path.islink(full):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self.addstats("<lnk>", "links", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 elif os.path.isdir(full):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self.statdir(full)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 else:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 self.statfile(full)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
45 def statfile(self, filename):
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
46 head, ext = os.path.splitext(filename)
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
47 head, base = os.path.split(filename)
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 if ext == base:
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
49 ext = "" # E.g. .cvsignore is deemed not to have an extension
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
50 ext = os.path.normcase(ext)
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
51 if not ext:
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
52 ext = "<none>"
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 self.addstats(ext, "files", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 try:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
55 f = open(filename, "rb")
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 except IOError, err:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
57 sys.stderr.write("Can't open %s: %s\n" % (filename, err))
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 self.addstats(ext, "unopenable", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 data = f.read()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 f.close()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.addstats(ext, "bytes", len(data))
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if '\0' in data:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 self.addstats(ext, "binary", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if not data:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 self.addstats(ext, "empty", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 #self.addstats(ext, "chars", len(data))
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 lines = data.splitlines()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 self.addstats(ext, "lines", len(lines))
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 del lines
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 words = data.split()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 self.addstats(ext, "words", len(words))
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def addstats(self, ext, key, n):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 d = self.stats.setdefault(ext, {})
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 d[key] = d.get(key, 0) + n
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 def report(self):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 exts = self.stats.keys()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 exts.sort()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 # Get the column keys
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 columns = {}
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 for ext in exts:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 columns.update(self.stats[ext])
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 cols = columns.keys()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 cols.sort()
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
88 colwidth = {}
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
89 colwidth["ext"] = max([len(ext) for ext in exts])
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
90 minwidth = 6
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
91 self.stats["TOTAL"] = {}
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 for col in cols:
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
93 total = 0
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
94 cw = max(minwidth, len(col))
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
95 for ext in exts:
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 value = self.stats[ext].get(col)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 if value is None:
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
98 w = 0
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 else:
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
100 w = len("%d" % value)
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
101 total += value
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
102 cw = max(cw, w)
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
103 cw = max(cw, len(str(total)))
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
104 colwidth[col] = cw
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
105 self.stats["TOTAL"][col] = total
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
106 exts.append("TOTAL")
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
107 for ext in exts:
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
108 self.stats[ext]["ext"] = ext
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
109 cols.insert(0, "ext")
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
110 def printheader():
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
111 for col in cols:
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
112 print "%*s" % (colwidth[col], col),
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 print
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
114 printheader()
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
115 for ext in exts:
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
116 for col in cols:
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
117 value = self.stats[ext].get(col, "")
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
118 print "%*s" % (colwidth[col], value),
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
119 print
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
120 printheader() # Another header at the bottom
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def main():
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 args = sys.argv[1:]
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 if not args:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 args = [os.curdir]
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 s = Stats()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 s.statargs(args)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 s.report()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 if __name__ == "__main__":
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 main()