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