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

Python code coverage for Tools/scripts/findlinksto.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# findlinksto
4n/a#
5n/a# find symbolic links to a path matching a regular expression
6n/a
7n/aimport os
8n/aimport sys
9n/aimport re
10n/aimport getopt
11n/a
12n/adef main():
13n/a try:
14n/a opts, args = getopt.getopt(sys.argv[1:], '')
15n/a if len(args) < 2:
16n/a raise getopt.GetoptError('not enough arguments', None)
17n/a except getopt.GetoptError as msg:
18n/a sys.stdout = sys.stderr
19n/a print(msg)
20n/a print('usage: findlinksto pattern directory ...')
21n/a sys.exit(2)
22n/a pat, dirs = args[0], args[1:]
23n/a prog = re.compile(pat)
24n/a for dirname in dirs:
25n/a os.walk(dirname, visit, prog)
26n/a
27n/adef visit(prog, dirname, names):
28n/a if os.path.islink(dirname):
29n/a names[:] = []
30n/a return
31n/a if os.path.ismount(dirname):
32n/a print('descend into', dirname)
33n/a for name in names:
34n/a name = os.path.join(dirname, name)
35n/a try:
36n/a linkto = os.readlink(name)
37n/a if prog.search(linkto) is not None:
38n/a print(name, '->', linkto)
39n/a except OSError:
40n/a pass
41n/a
42n/aif __name__ == '__main__':
43n/a main()