1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """finddiv - a grep-like tool that looks for division operators. |
---|
4 | n/a | |
---|
5 | n/a | Usage: finddiv [-l] file_or_directory ... |
---|
6 | n/a | |
---|
7 | n/a | For directory arguments, all files in the directory whose name ends in |
---|
8 | n/a | .py are processed, and subdirectories are processed recursively. |
---|
9 | n/a | |
---|
10 | n/a | This actually tokenizes the files to avoid false hits in comments or |
---|
11 | n/a | strings literals. |
---|
12 | n/a | |
---|
13 | n/a | By default, this prints all lines containing a / or /= operator, in |
---|
14 | n/a | grep -n style. With the -l option specified, it prints the filename |
---|
15 | n/a | of files that contain at least one / or /= operator. |
---|
16 | n/a | """ |
---|
17 | n/a | |
---|
18 | n/a | import os |
---|
19 | n/a | import sys |
---|
20 | n/a | import getopt |
---|
21 | n/a | import tokenize |
---|
22 | n/a | |
---|
23 | n/a | def main(): |
---|
24 | n/a | try: |
---|
25 | n/a | opts, args = getopt.getopt(sys.argv[1:], "lh") |
---|
26 | n/a | except getopt.error as msg: |
---|
27 | n/a | usage(msg) |
---|
28 | n/a | return 2 |
---|
29 | n/a | if not args: |
---|
30 | n/a | usage("at least one file argument is required") |
---|
31 | n/a | return 2 |
---|
32 | n/a | listnames = 0 |
---|
33 | n/a | for o, a in opts: |
---|
34 | n/a | if o == "-h": |
---|
35 | n/a | print(__doc__) |
---|
36 | n/a | return |
---|
37 | n/a | if o == "-l": |
---|
38 | n/a | listnames = 1 |
---|
39 | n/a | exit = None |
---|
40 | n/a | for filename in args: |
---|
41 | n/a | x = process(filename, listnames) |
---|
42 | n/a | exit = exit or x |
---|
43 | n/a | return exit |
---|
44 | n/a | |
---|
45 | n/a | def usage(msg): |
---|
46 | n/a | sys.stderr.write("%s: %s\n" % (sys.argv[0], msg)) |
---|
47 | n/a | sys.stderr.write("Usage: %s [-l] file ...\n" % sys.argv[0]) |
---|
48 | n/a | sys.stderr.write("Try `%s -h' for more information.\n" % sys.argv[0]) |
---|
49 | n/a | |
---|
50 | n/a | def process(filename, listnames): |
---|
51 | n/a | if os.path.isdir(filename): |
---|
52 | n/a | return processdir(filename, listnames) |
---|
53 | n/a | try: |
---|
54 | n/a | fp = open(filename) |
---|
55 | n/a | except IOError as msg: |
---|
56 | n/a | sys.stderr.write("Can't open: %s\n" % msg) |
---|
57 | n/a | return 1 |
---|
58 | n/a | g = tokenize.generate_tokens(fp.readline) |
---|
59 | n/a | lastrow = None |
---|
60 | n/a | for type, token, (row, col), end, line in g: |
---|
61 | n/a | if token in ("/", "/="): |
---|
62 | n/a | if listnames: |
---|
63 | n/a | print(filename) |
---|
64 | n/a | break |
---|
65 | n/a | if row != lastrow: |
---|
66 | n/a | lastrow = row |
---|
67 | n/a | print("%s:%d:%s" % (filename, row, line), end=' ') |
---|
68 | n/a | fp.close() |
---|
69 | n/a | |
---|
70 | n/a | def processdir(dir, listnames): |
---|
71 | n/a | try: |
---|
72 | n/a | names = os.listdir(dir) |
---|
73 | n/a | except OSError as msg: |
---|
74 | n/a | sys.stderr.write("Can't list directory: %s\n" % dir) |
---|
75 | n/a | return 1 |
---|
76 | n/a | files = [] |
---|
77 | n/a | for name in names: |
---|
78 | n/a | fn = os.path.join(dir, name) |
---|
79 | n/a | if os.path.normcase(fn).endswith(".py") or os.path.isdir(fn): |
---|
80 | n/a | files.append(fn) |
---|
81 | n/a | files.sort(key=os.path.normcase) |
---|
82 | n/a | exit = None |
---|
83 | n/a | for fn in files: |
---|
84 | n/a | x = process(fn, listnames) |
---|
85 | n/a | exit = exit or x |
---|
86 | n/a | return exit |
---|
87 | n/a | |
---|
88 | n/a | if __name__ == "__main__": |
---|
89 | n/a | sys.exit(main()) |
---|