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

Python code coverage for Tools/scripts/checkpyc.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a# Check that all ".pyc" files exist and are up-to-date
3n/a# Uses module 'os'
4n/a
5n/aimport sys
6n/aimport os
7n/afrom stat import ST_MTIME
8n/aimport importlib.util
9n/a
10n/a# PEP 3147 compatibility (PYC Repository Directories)
11n/acache_from_source = (importlib.util.cache_from_source if sys.implementation.cache_tag
12n/a else lambda path: path + 'c')
13n/a
14n/a
15n/adef main():
16n/a if len(sys.argv) > 1:
17n/a verbose = (sys.argv[1] == '-v')
18n/a silent = (sys.argv[1] == '-s')
19n/a else:
20n/a verbose = silent = False
21n/a MAGIC = importlib.util.MAGIC_NUMBER
22n/a if not silent:
23n/a print('Using MAGIC word', repr(MAGIC))
24n/a for dirname in sys.path:
25n/a try:
26n/a names = os.listdir(dirname)
27n/a except OSError:
28n/a print('Cannot list directory', repr(dirname))
29n/a continue
30n/a if not silent:
31n/a print('Checking ', repr(dirname), '...')
32n/a for name in sorted(names):
33n/a if name.endswith('.py'):
34n/a name = os.path.join(dirname, name)
35n/a try:
36n/a st = os.stat(name)
37n/a except OSError:
38n/a print('Cannot stat', repr(name))
39n/a continue
40n/a if verbose:
41n/a print('Check', repr(name), '...')
42n/a name_c = cache_from_source(name)
43n/a try:
44n/a with open(name_c, 'rb') as f:
45n/a magic_str = f.read(4)
46n/a mtime_str = f.read(4)
47n/a except IOError:
48n/a print('Cannot open', repr(name_c))
49n/a continue
50n/a if magic_str != MAGIC:
51n/a print('Bad MAGIC word in ".pyc" file', end=' ')
52n/a print(repr(name_c))
53n/a continue
54n/a mtime = get_long(mtime_str)
55n/a if mtime in {0, -1}:
56n/a print('Bad ".pyc" file', repr(name_c))
57n/a elif mtime != st[ST_MTIME]:
58n/a print('Out-of-date ".pyc" file', end=' ')
59n/a print(repr(name_c))
60n/a
61n/a
62n/adef get_long(s):
63n/a if len(s) != 4:
64n/a return -1
65n/a return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24)
66n/a
67n/a
68n/aif __name__ == '__main__':
69n/a main()