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

Python code coverage for Tools/scripts/ptags.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# ptags
4n/a#
5n/a# Create a tags file for Python programs, usable with vi.
6n/a# Tagged are:
7n/a# - functions (even inside other defs or classes)
8n/a# - classes
9n/a# - filenames
10n/a# Warns about files it cannot open.
11n/a# No warnings about duplicate tags.
12n/a
13n/aimport sys, re, os
14n/a
15n/atags = [] # Modified global variable!
16n/a
17n/adef main():
18n/a args = sys.argv[1:]
19n/a for filename in args:
20n/a treat_file(filename)
21n/a if tags:
22n/a fp = open('tags', 'w')
23n/a tags.sort()
24n/a for s in tags: fp.write(s)
25n/a
26n/a
27n/aexpr = r'^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]'
28n/amatcher = re.compile(expr)
29n/a
30n/adef treat_file(filename):
31n/a try:
32n/a fp = open(filename, 'r')
33n/a except:
34n/a sys.stderr.write('Cannot open %s\n' % filename)
35n/a return
36n/a base = os.path.basename(filename)
37n/a if base[-3:] == '.py':
38n/a base = base[:-3]
39n/a s = base + '\t' + filename + '\t' + '1\n'
40n/a tags.append(s)
41n/a while 1:
42n/a line = fp.readline()
43n/a if not line:
44n/a break
45n/a m = matcher.match(line)
46n/a if m:
47n/a content = m.group(0)
48n/a name = m.group(2)
49n/a s = name + '\t' + filename + '\t/^' + content + '/\n'
50n/a tags.append(s)
51n/a
52n/aif __name__ == '__main__':
53n/a main()