annotate Tools/scripts/byext.py @ 70626:aa07c1237f4e

(Merge 3.2) Issue #12016: Reindent decoders of HK and JP codecs
author Victor Stinner <victor.stinner@haypocalc.com>
date Fri, 03 Jun 2011 23:34:32 +0200
parents 5ae390ac6c61
children 6cfe2982de42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63624
7a47c4d30f6e Update README, remove obsolete scripts.
Georg Brandl <georg@python.org>
parents: 43448
diff changeset
1 #! /usr/bin/env python3
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
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
8
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 class Stats:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def __init__(self):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 self.stats = {}
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 def statargs(self, args):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 for arg in args:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 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
17 self.statdir(arg)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 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
19 self.statfile(arg)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 else:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 28557
diff changeset
21 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
22 self.addstats("<???>", "unknown", 1)
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 def statdir(self, dir):
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 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
26 try:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 names = os.listdir(dir)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
28 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
29 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
30 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
31 return
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
32 for name in sorted(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(".#"):
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
34 continue # Skip CVS temp files
28557
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("~"):
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
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:
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
49 ext = "" # E.g. .cvsignore is deemed not to have an extension
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 = 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:
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
55 with open(filename, "rb") as f:
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
56 data = f.read()
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
57 except IOError as err:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
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 self.addstats(ext, "bytes", len(data))
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
62 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
63 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
64 return
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if not data:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 self.addstats(ext, "empty", 1)
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
67 # self.addstats(ext, "chars", len(data))
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
68 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
69 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
70 del lines
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 words = data.split()
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 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
73
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 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
75 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
76 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
77
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def report(self):
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
79 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
80 # Get the column keys
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 columns = {}
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 for ext in exts:
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 columns.update(self.stats[ext])
43448
9bc985582cb2 Make byext.py really work.
Guido van Rossum <guido@python.org>
parents: 42427
diff changeset
84 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
85 colwidth = {}
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["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
87 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
88 self.stats["TOTAL"] = {}
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 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
90 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
91 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
92 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
93 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
94 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
95 w = 0
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 cols.insert(0, "ext")
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
107
28557
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()
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
112
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, "")
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
117 print("%*s" % (colwidth[col], value), end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
118 print()
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
119 printheader() # Another header at the bottom
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
120
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
63872
5ae390ac6c61 Clean up syntax for some scripts.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 63624
diff changeset
130
26999
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 if __name__ == "__main__":
8ed472e5bcd4 Silly little script to print statistics (files, lines, words) by
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 main()