annotate Tools/scripts/byext.py @ 64166:d0e9febfa00a 3.1

Merged revisions 84172 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines Restore GIL in nis_cat in case of error. ........
author Martin v. Löwis <martin@v.loewis.de>
date Thu, 19 Aug 2010 09:03:03 +0000
parents 9bc985582cb2
children 7a47c4d30f6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
1 #! /usr/bin/env python3.0
26999
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:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
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)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
27 except os.error as err:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 28557
diff changeset
28 sys.stderr.write("Can't list %s: %s\n" % (dir, err))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
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
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 28557
diff changeset
45 def statfile(self, filename):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 28557
diff changeset
46 head, ext = os.path.splitext(filename)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
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:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 28557
diff changeset
55 f = open(filename, "rb")
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
56 except IOError as err:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
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))
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
63 if b'\0' in data:
26999
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))
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
69 lines = str(data, "latin-1").splitlines()
26999
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):
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
80 exts = sorted(self.stats)
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # Get the column keys
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 columns = {}
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 for ext in exts:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 columns.update(self.stats[ext])
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
85 cols = sorted(columns)
28557
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
86 colwidth = {}
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
87 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
88 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
89 self.stats["TOTAL"] = {}
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 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
91 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
92 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
93 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
94 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
95 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
96 w = 0
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 for col in cols:
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
110 print("%*s" % (colwidth[col], col), end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
111 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
112 printheader()
ba2d8921a8da Various improvements to the way the table is formatted, to deal with
Guido van Rossum <guido@python.org>
parents: 26999
diff changeset
113 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
114 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
115 value = self.stats[ext].get(col, "")
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
116 print("%*s" % (colwidth[col], value), end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
117 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
118 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
119
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 def main():
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 args = sys.argv[1:]
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if not args:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 args = [os.curdir]
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 s = Stats()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 s.statargs(args)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 s.report()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 if __name__ == "__main__":
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 main()