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

Python code coverage for Tools/scripts/findnocoding.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a
3n/a"""List all those Python files that require a coding directive
4n/a
5n/aUsage: findnocoding.py dir1 [dir2...]
6n/a"""
7n/a
8n/a__author__ = "Oleg Broytmann, Georg Brandl"
9n/a
10n/aimport sys, os, re, getopt
11n/a
12n/a# our pysource module finds Python source files
13n/atry:
14n/a import pysource
15n/aexcept ImportError:
16n/a # emulate the module with a simple os.walk
17n/a class pysource:
18n/a has_python_ext = looks_like_python = can_be_compiled = None
19n/a def walk_python_files(self, paths, *args, **kwargs):
20n/a for path in paths:
21n/a if os.path.isfile(path):
22n/a yield path.endswith(".py")
23n/a elif os.path.isdir(path):
24n/a for root, dirs, files in os.walk(path):
25n/a for filename in files:
26n/a if filename.endswith(".py"):
27n/a yield os.path.join(root, filename)
28n/a pysource = pysource()
29n/a
30n/a
31n/a print("The pysource module is not available; "
32n/a "no sophisticated Python source file search will be done.", file=sys.stderr)
33n/a
34n/a
35n/adecl_re = re.compile(rb'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)')
36n/ablank_re = re.compile(rb'^[ \t\f]*(?:[#\r\n]|$)')
37n/a
38n/adef get_declaration(line):
39n/a match = decl_re.match(line)
40n/a if match:
41n/a return match.group(1)
42n/a return b''
43n/a
44n/adef has_correct_encoding(text, codec):
45n/a try:
46n/a str(text, codec)
47n/a except UnicodeDecodeError:
48n/a return False
49n/a else:
50n/a return True
51n/a
52n/adef needs_declaration(fullpath):
53n/a try:
54n/a infile = open(fullpath, 'rb')
55n/a except IOError: # Oops, the file was removed - ignore it
56n/a return None
57n/a
58n/a with infile:
59n/a line1 = infile.readline()
60n/a line2 = infile.readline()
61n/a
62n/a if (get_declaration(line1) or
63n/a blank_re.match(line1) and get_declaration(line2)):
64n/a # the file does have an encoding declaration, so trust it
65n/a return False
66n/a
67n/a # check the whole file for non utf-8 characters
68n/a rest = infile.read()
69n/a
70n/a if has_correct_encoding(line1+line2+rest, "utf-8"):
71n/a return False
72n/a
73n/a return True
74n/a
75n/a
76n/ausage = """Usage: %s [-cd] paths...
77n/a -c: recognize Python source files trying to compile them
78n/a -d: debug output""" % sys.argv[0]
79n/a
80n/aif __name__ == '__main__':
81n/a
82n/a try:
83n/a opts, args = getopt.getopt(sys.argv[1:], 'cd')
84n/a except getopt.error as msg:
85n/a print(msg, file=sys.stderr)
86n/a print(usage, file=sys.stderr)
87n/a sys.exit(1)
88n/a
89n/a is_python = pysource.looks_like_python
90n/a debug = False
91n/a
92n/a for o, a in opts:
93n/a if o == '-c':
94n/a is_python = pysource.can_be_compiled
95n/a elif o == '-d':
96n/a debug = True
97n/a
98n/a if not args:
99n/a print(usage, file=sys.stderr)
100n/a sys.exit(1)
101n/a
102n/a for fullpath in pysource.walk_python_files(args, is_python):
103n/a if debug:
104n/a print("Testing for coding: %s" % fullpath)
105n/a result = needs_declaration(fullpath)
106n/a if result:
107n/a print(fullpath)