3 """finddiv - a grep-like tool that looks for division operators.
5 Usage: finddiv [-l] file_or_directory ...
7 For directory arguments, all files in the directory whose name ends in
8 .py are processed, and subdirectories are processed recursively.
10 This actually tokenizes the files to avoid false hits in comments or
13 By default, this prints all lines containing a / or /= operator, in
14 grep -n style. With the -l option specified, it prints the filename
15 of files that contain at least one / or /= operator.
25 opts
, args
= getopt
.getopt(sys
.argv
[1:], "lh")
26 except getopt
.error
, msg
:
30 usage("at least one file argument is required")
41 x
= process(filename
, listnames
)
46 sys
.stderr
.write("%s: %s\n" % (sys
.argv
[0], msg
))
47 sys
.stderr
.write("Usage: %s [-l] file ...\n" % sys
.argv
[0])
48 sys
.stderr
.write("Try `%s -h' for more information.\n" % sys
.argv
[0])
50 def process(filename
, listnames
):
51 if os
.path
.isdir(filename
):
52 return processdir(filename
, listnames
)
56 sys
.stderr
.write("Can't open: %s\n" % msg
)
58 g
= tokenize
.generate_tokens(fp
.readline
)
60 for type, token
, (row
, col
), end
, line
in g
:
61 if token
in ("/", "/="):
67 print "%s:%d:%s" % (filename
, row
, line
),
70 def processdir(dir, listnames
):
72 names
= os
.listdir(dir)
74 sys
.stderr
.write("Can't list directory: %s\n" % dir)
78 fn
= os
.path
.join(dir, name
)
79 if os
.path
.normcase(fn
).endswith(".py") or os
.path
.isdir(fn
):
81 files
.sort(lambda a
, b
: cmp(os
.path
.normcase(a
), os
.path
.normcase(b
)))
84 x
= process(fn
, listnames
)
88 if __name__
== "__main__":