| 1 | #!/usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | """List all those Python files that require a coding directive
|
|---|
| 4 |
|
|---|
| 5 | Usage: nocoding.py dir1 [dir2...]
|
|---|
| 6 | """
|
|---|
| 7 |
|
|---|
| 8 | __author__ = "Oleg Broytmann, Reinhold Birkenfeld"
|
|---|
| 9 |
|
|---|
| 10 | import sys, os, re, getopt
|
|---|
| 11 |
|
|---|
| 12 | # our pysource module finds Python source files
|
|---|
| 13 | try:
|
|---|
| 14 | import pysource
|
|---|
| 15 | except:
|
|---|
| 16 | # emulate the module with a simple os.walk
|
|---|
| 17 | class pysource:
|
|---|
| 18 | has_python_ext = looks_like_python = can_be_compiled = None
|
|---|
| 19 | def walk_python_files(self, paths, *args, **kwargs):
|
|---|
| 20 | for path in paths:
|
|---|
| 21 | if os.path.isfile(path):
|
|---|
| 22 | yield path.endswith(".py")
|
|---|
| 23 | elif os.path.isdir(path):
|
|---|
| 24 | for root, dirs, files in os.walk(path):
|
|---|
| 25 | for filename in files:
|
|---|
| 26 | if filename.endswith(".py"):
|
|---|
| 27 | yield os.path.join(root, filename)
|
|---|
| 28 | pysource = pysource()
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | print >>sys.stderr, ("The pysource module is not available; "
|
|---|
| 32 | "no sophisticated Python source file search will be done.")
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | decl_re = re.compile(r"coding[=:]\s*([-\w.]+)")
|
|---|
| 36 |
|
|---|
| 37 | def get_declaration(line):
|
|---|
| 38 | match = decl_re.search(line)
|
|---|
| 39 | if match:
|
|---|
| 40 | return match.group(1)
|
|---|
| 41 | return ''
|
|---|
| 42 |
|
|---|
| 43 | def has_correct_encoding(text, codec):
|
|---|
| 44 | try:
|
|---|
| 45 | unicode(text, codec)
|
|---|
| 46 | except UnicodeDecodeError:
|
|---|
| 47 | return False
|
|---|
| 48 | else:
|
|---|
| 49 | return True
|
|---|
| 50 |
|
|---|
| 51 | def needs_declaration(fullpath):
|
|---|
| 52 | try:
|
|---|
|
|---|