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

Python code coverage for Tools/scripts/pysource.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a
3n/a"""\
4n/aList python source files.
5n/a
6n/aThere are three functions to check whether a file is a Python source, listed
7n/ahere with increasing complexity:
8n/a
9n/a- has_python_ext() checks whether a file name ends in '.py[w]'.
10n/a- look_like_python() checks whether the file is not binary and either has
11n/a the '.py[w]' extension or the first line contains the word 'python'.
12n/a- can_be_compiled() checks whether the file can be compiled by compile().
13n/a
14n/aThe file also must be of appropriate size - not bigger than a megabyte.
15n/a
16n/awalk_python_files() recursively lists all Python files under the given directories.
17n/a"""
18n/a__author__ = "Oleg Broytmann, Georg Brandl"
19n/a
20n/a__all__ = ["has_python_ext", "looks_like_python", "can_be_compiled", "walk_python_files"]
21n/a
22n/a
23n/aimport os, re
24n/a
25n/abinary_re = re.compile(br'[\x00-\x08\x0E-\x1F\x7F]')
26n/a
27n/adebug = False
28n/a
29n/adef print_debug(msg):
30n/a if debug: print(msg)
31n/a
32n/a
33n/adef _open(fullpath):
34n/a try:
35n/a size = os.stat(fullpath).st_size
36n/a except OSError as err: # Permission denied - ignore the file
37n/a print_debug("%s: permission denied: %s" % (fullpath, err))
38n/a return None
39n/a
40n/a if size > 1024*1024: # too big
41n/a print_debug("%s: the file is too big: %d bytes" % (fullpath, size))
42n/a return None
43n/a
44n/a try:
45n/a return open(fullpath, "rb")
46n/a except IOError as err: # Access denied, or a special file - ignore it
47n/a print_debug("%s: access denied: %s" % (fullpath, err))
48n/a return None
49n/a
50n/adef has_python_ext(fullpath):
51n/a return fullpath.endswith(".py") or fullpath.endswith(".pyw")
52n/a
53n/adef looks_like_python(fullpath):
54n/a infile = _open(fullpath)
55n/a if infile is None:
56n/a return False
57n/a
58n/a with infile:
59n/a line = infile.readline()
60n/a
61n/a if binary_re.search(line):
62n/a # file appears to be binary
63n/a print_debug("%s: appears to be binary" % fullpath)
64n/a return False
65n/a
66n/a if fullpath.endswith(".py") or fullpath.endswith(".pyw"):
67n/a return True
68n/a elif b"python" in line:
69n/a # disguised Python script (e.g. CGI)
70n/a return True
71n/a
72n/a return False
73n/a
74n/adef can_be_compiled(fullpath):
75n/a infile = _open(fullpath)
76n/a if infile is None:
77n/a return False
78n/a
79n/a with infile:
80n/a code = infile.read()
81n/a
82n/a try:
83n/a compile(code, fullpath, "exec")
84n/a except Exception as err:
85n/a print_debug("%s: cannot compile: %s" % (fullpath, err))
86n/a return False
87n/a
88n/a return True
89n/a
90n/a
91n/adef walk_python_files(paths, is_python=looks_like_python, exclude_dirs=None):
92n/a """\
93n/a Recursively yield all Python source files below the given paths.
94n/a
95n/a paths: a list of files and/or directories to be checked.
96n/a is_python: a function that takes a file name and checks whether it is a
97n/a Python source file
98n/a exclude_dirs: a list of directory base names that should be excluded in
99n/a the search
100n/a """
101n/a if exclude_dirs is None:
102n/a exclude_dirs=[]
103n/a
104n/a for path in paths:
105n/a print_debug("testing: %s" % path)
106n/a if os.path.isfile(path):
107n/a if is_python(path):
108n/a yield path
109n/a elif os.path.isdir(path):
110n/a print_debug(" it is a directory")
111n/a for dirpath, dirnames, filenames in os.walk(path):
112n/a for exclude in exclude_dirs:
113n/a if exclude in dirnames:
114n/a dirnames.remove(exclude)
115n/a for filename in filenames:
116n/a fullpath = os.path.join(dirpath, filename)
117n/a print_debug("testing: %s" % fullpath)
118n/a if is_python(fullpath):
119n/a yield fullpath
120n/a else:
121n/a print_debug(" unknown type")
122n/a
123n/a
124n/aif __name__ == "__main__":
125n/a # Two simple examples/tests
126n/a for fullpath in walk_python_files(['.']):
127n/a print(fullpath)
128n/a print("----------")
129n/a for fullpath in walk_python_files(['.'], is_python=can_be_compiled):
130n/a print(fullpath)