Merged revisions 76156 via svnmerge from
[python/dscho.git] / Tools / scripts / checkappend.py
blob4c74ee57698e2c1a16f0acbb2f4b496626ad366c
1 #! /usr/bin/env python
3 # Released to the public domain, by Tim Peters, 28 February 2000.
5 """checkappend.py -- search for multi-argument .append() calls.
7 Usage: specify one or more file or directory paths:
8 checkappend [-v] file_or_dir [file_or_dir] ...
10 Each file_or_dir is checked for multi-argument .append() calls. When
11 a directory, all .py files in the directory, and recursively in its
12 subdirectories, are checked.
14 Use -v for status msgs. Use -vv for more status msgs.
16 In the absence of -v, the only output is pairs of the form
18 filename(linenumber):
19 line containing the suspicious append
21 Note that this finds multi-argument append calls regardless of whether
22 they're attached to list objects. If a module defines a class with an
23 append method that takes more than one argument, calls to that method
24 will be listed.
26 Note that this will not find multi-argument list.append calls made via a
27 bound method object. For example, this is not caught:
29 somelist = []
30 push = somelist.append
31 push(1, 2, 3)
32 """
34 __version__ = 1, 0, 0
36 import os
37 import sys
38 import getopt
39 import tokenize
41 verbose = 0
43 def errprint(*args):
44 msg = ' '.join(args)
45 sys.stderr.write(msg)
46 sys.stderr.write("\n")
48 def main():
49 args = sys.argv[1:]
50 global verbose
51 try:
52 opts, args = getopt.getopt(sys.argv[1:], "v")
53 except getopt.error as msg:
54 errprint(str(msg) + "\n\n" + __doc__)
55 return
56 for opt, optarg in opts:
57 if opt == '-v':
58 verbose = verbose + 1
59 if not args:
60 errprint(__doc__)
61 return
62 for arg in args:
63 check(arg)
65 def check(file):
66 if os.path.isdir(file) and not os.path.islink(file):
67 if verbose:
68 print("%r: listing directory" % (file,))
69 names = os.listdir(file)
70 for name in names:
71 fullname = os.path.join(file, name)
72 if ((os.path.isdir(fullname) and
73 not os.path.islink(fullname))
74 or os.path.normcase(name[-3:]) == ".py"):
75 check(fullname)
76 return
78 try:
79 f = open(file)
80 except IOError as msg:
81 errprint("%r: I/O Error: %s" % (file, msg))
82 return
84 if verbose > 1:
85 print("checking %r ..." % (file,))
87 ok = AppendChecker(file, f).run()
88 if verbose and ok:
89 print("%r: Clean bill of health." % (file,))
91 [FIND_DOT,
92 FIND_APPEND,
93 FIND_LPAREN,
94 FIND_COMMA,
95 FIND_STMT] = range(5)
97 class AppendChecker:
98 def __init__(self, fname, file):
99 self.fname = fname
100 self.file = file
101 self.state = FIND_DOT
102 self.nerrors = 0
104 def run(self):
105 try:
106 tokens = tokenize.generate_tokens(self.file.readline)
107 for _token in tokens:
108 self.tokeneater(*_token)
109 except tokenize.TokenError as msg:
110 errprint("%r: Token Error: %s" % (self.fname, msg))
111 self.nerrors = self.nerrors + 1
112 return self.nerrors == 0
114 def tokeneater(self, type, token, start, end, line,
115 NEWLINE=tokenize.NEWLINE,
116 JUNK=(tokenize.COMMENT, tokenize.NL),
117 OP=tokenize.OP,
118 NAME=tokenize.NAME):
120 state = self.state
122 if type in JUNK:
123 pass
125 elif state is FIND_DOT:
126 if type is OP and token == ".":
127 state = FIND_APPEND
129 elif state is FIND_APPEND:
130 if type is NAME and token == "append":
131 self.line = line
132 self.lineno = start[0]
133 state = FIND_LPAREN
134 else:
135 state = FIND_DOT
137 elif state is FIND_LPAREN:
138 if type is OP and token == "(":
139 self.level = 1
140 state = FIND_COMMA
141 else:
142 state = FIND_DOT
144 elif state is FIND_COMMA:
145 if type is OP:
146 if token in ("(", "{", "["):
147 self.level = self.level + 1
148 elif token in (")", "}", "]"):
149 self.level = self.level - 1
150 if self.level == 0:
151 state = FIND_DOT
152 elif token == "," and self.level == 1:
153 self.nerrors = self.nerrors + 1
154 print("%s(%d):\n%s" % (self.fname, self.lineno,
155 self.line))
156 # don't gripe about this stmt again
157 state = FIND_STMT
159 elif state is FIND_STMT:
160 if type is NEWLINE:
161 state = FIND_DOT
163 else:
164 raise SystemError("unknown internal state '%r'" % (state,))
166 self.state = state
168 if __name__ == '__main__':
169 main()