annotate Tools/scripts/byext.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 9a57c9d4b0b4
children
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
77922
9a57c9d4b0b4 Avoid SyntaxError in script using print function
Éric Araujo <merwok@netwok.org>
parents: 71038
diff changeset
5 from __future__ import print_function
9a57c9d4b0b4 Avoid SyntaxError in script using print function
Éric Araujo <merwok@netwok.org>
parents: 71038
diff changeset
6
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 import os
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import sys
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 class Stats:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 def __init__(self):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 self.stats = {}
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 def statargs(self, args):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 for arg in args:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 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
18 self.statdir(arg)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 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
20 self.statfile(arg)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 else:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
22 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
23 self.addstats("<???>", "unknown", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def statdir(self, dir):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 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
27 try:
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
28 names = sorted(os.listdir(dir))
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
29 except os.error as err:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
30 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
31 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
32 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 self.statdir(full)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 else:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 self.statfile(full)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 ext = "<none>"
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 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
55 try:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
56 f = open(filename, "rb")
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
57 except IOError as err:
37175
a18aa3fbdd4a SF bug #1457411, fix errors using variables that don't exist.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28557
diff changeset
58 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
59 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
60 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 data = f.read()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 f.close()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 self.addstats(ext, "bytes", len(data))
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
64 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
65 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
66 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 if not data:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 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
69 #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
70 lines = data.splitlines()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 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
72 del lines
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 words = data.split()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 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
75
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 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
77 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
78 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
79
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 def report(self):
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
81 exts = sorted(self.stats.keys())
26999
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])
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
86 cols = sorted(columns.keys())
28557
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 = {}
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["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
89 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
90 self.stats["TOTAL"] = {}
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 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
92 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
93 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
94 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
95 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
96 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
97 w = 0
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 for col in cols:
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
111 print("%*s" % (colwidth[col], col), end=" ")
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
112 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
113 printheader()
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 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
115 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
116 value = self.stats[ext].get(col, "")
71038
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
117 print("%*s" % (colwidth[col], value), end=" ")
b2d4e6fc4f19 Minimal changes to make byext.py script work with Python 3 syntax.
Guido van Rossum <guido@python.org>
parents: 37175
diff changeset
118 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
119 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
120
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def main():
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 args = sys.argv[1:]
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 if not args:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 args = [os.curdir]
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 s = Stats()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 s.statargs(args)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 s.report()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 if __name__ == "__main__":
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 main()