blob: 97f63316c774acfbb5372dfbcccc1344c3b5217b [file] [log] [blame]
Guido van Rossum43db62e2001-09-02 04:43:30 +00001#! /usr/bin/env python
2
3"""finddiv - a grep-like tool that looks for division operators.
4
5Usage: finddiv [-l] file_or_directory ...
6
7For directory arguments, all files in the directory whose name ends in
8.py are processed, and subdirectories are processed recursively.
9
10This actually tokenizes the files to avoid false hits in comments or
11strings literals.
12
13By default, this prints all lines containing a / or /= operator, in
14grep -n style. With the -l option specified, it prints the filename
15of files that contain at least one / or /= operator.
16"""
17
18import os
19import sys
20import getopt
21import tokenize
22
23def main():
24 try:
25 opts, args = getopt.getopt(sys.argv[1:], "lh")
26 except getopt.error, msg:
27 usage(msg)
28 return 2
29 if not args:
30 usage("at least one file argument is required")
31 return 2
32 listnames = 0
33 for o, a in opts:
34 if o == "-h":
35 print __doc__
36 return
37 if o == "-l":
38 listnames = 1
39 exit = None
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000040 for filename in args:
41 x = process(filename, listnames)
Guido van Rossum43db62e2001-09-02 04:43:30 +000042 exit = exit or x
43 return exit
44
45def usage(msg):
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])
49
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000050def process(filename, listnames):
51 if os.path.isdir(filename):
52 return processdir(filename, listnames)
Guido van Rossum43db62e2001-09-02 04:43:30 +000053 try:
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000054 fp = open(filename)
Guido van Rossum43db62e2001-09-02 04:43:30 +000055 except IOError, msg:
56 sys.stderr.write("Can't open: %s\n" % msg)
57 return 1
58 g = tokenize.generate_tokens(fp.readline)
59 lastrow = None
60 for type, token, (row, col), end, line in g:
61 if token in ("/", "/="):
62 if listnames:
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000063 print filename
Guido van Rossum43db62e2001-09-02 04:43:30 +000064 break
65 if row != lastrow:
66 lastrow = row
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000067 print "%s:%d:%s" % (filename, row, line),
Guido van Rossum43db62e2001-09-02 04:43:30 +000068 fp.close()
69
70def processdir(dir, listnames):
71 try:
72 names = os.listdir(dir)
73 except os.error, msg:
74 sys.stderr.write("Can't list directory: %s\n" % dir)
75 return 1
76 files = []
77 for name in names:
78 fn = os.path.join(dir, name)
79 if os.path.normcase(fn).endswith(".py") or os.path.isdir(fn):
80 files.append(fn)
81 files.sort(lambda a, b: cmp(os.path.normcase(a), os.path.normcase(b)))
82 exit = None
83 for fn in files:
84 x = process(fn, listnames)
85 exit = exit or x
86 return exit
87
88if __name__ == "__main__":
89 sys.exit(main())