ยปCore Development>Code coverage>Tools/scripts/finddiv.py

Python code coverage for Tools/scripts/finddiv.py

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