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

Python code coverage for Tools/scripts/which.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# Variant of "which".
4n/a# On stderr, near and total misses are reported.
5n/a# '-l<flags>' argument adds ls -l<flags> of each file found.
6n/a
7n/aimport sys
8n/aif sys.path[0] in (".", ""): del sys.path[0]
9n/a
10n/aimport sys, os
11n/afrom stat import *
12n/a
13n/adef msg(str):
14n/a sys.stderr.write(str + '\n')
15n/a
16n/adef main():
17n/a pathlist = os.environ['PATH'].split(os.pathsep)
18n/a
19n/a sts = 0
20n/a longlist = ''
21n/a
22n/a if sys.argv[1:] and sys.argv[1][:2] == '-l':
23n/a longlist = sys.argv[1]
24n/a del sys.argv[1]
25n/a
26n/a for prog in sys.argv[1:]:
27n/a ident = ()
28n/a for dir in pathlist:
29n/a filename = os.path.join(dir, prog)
30n/a try:
31n/a st = os.stat(filename)
32n/a except OSError:
33n/a continue
34n/a if not S_ISREG(st[ST_MODE]):
35n/a msg(filename + ': not a disk file')
36n/a else:
37n/a mode = S_IMODE(st[ST_MODE])
38n/a if mode & 0o111:
39n/a if not ident:
40n/a print(filename)
41n/a ident = st[:3]
42n/a else:
43n/a if st[:3] == ident:
44n/a s = 'same as: '
45n/a else:
46n/a s = 'also: '
47n/a msg(s + filename)
48n/a else:
49n/a msg(filename + ': not executable')
50n/a if longlist:
51n/a sts = os.system('ls ' + longlist + ' ' + filename)
52n/a if sts: msg('"ls -l" exit status: ' + repr(sts))
53n/a if not ident:
54n/a msg(prog + ': not found')
55n/a sts = 1
56n/a
57n/a sys.exit(sts)
58n/a
59n/aif __name__ == '__main__':
60n/a main()