+++ /dev/null
-#!/usr/bin/env python\r
-\r
-"""List all those Python files that require a coding directive\r
-\r
-Usage: nocoding.py dir1 [dir2...]\r
-"""\r
-\r
-__author__ = "Oleg Broytmann, Georg Brandl"\r
-\r
-import sys, os, re, getopt\r
-\r
-# our pysource module finds Python source files\r
-try:\r
- import pysource\r
-except ImportError:\r
- # emulate the module with a simple os.walk\r
- class pysource:\r
- has_python_ext = looks_like_python = can_be_compiled = None\r
- def walk_python_files(self, paths, *args, **kwargs):\r
- for path in paths:\r
- if os.path.isfile(path):\r
- yield path.endswith(".py")\r
- elif os.path.isdir(path):\r
- for root, dirs, files in os.walk(path):\r
- for filename in files:\r
- if filename.endswith(".py"):\r
- yield os.path.join(root, filename)\r
- pysource = pysource()\r
-\r
-\r
- print >>sys.stderr, ("The pysource module is not available; "\r
- "no sophisticated Python source file search will be done.")\r
-\r
-\r
-decl_re = re.compile(r"coding[=:]\s*([-\w.]+)")\r
-\r
-def get_declaration(line):\r
- match = decl_re.search(line)\r
- if match:\r
- return match.group(1)\r
- return ''\r
-\r
-def has_correct_encoding(text, codec):\r
- try:\r
- unicode(text, codec)\r
- except UnicodeDecodeError:\r
- return False\r
- else:\r
- return True\r
-\r
-def needs_declaration(fullpath):\r
- try:\r
- infile = open(fullpath, 'rU')\r
- except IOError: # Oops, the file was removed - ignore it\r
- return None\r
-\r
- line1 = infile.readline()\r
- line2 = infile.readline()\r
-\r
- if get_declaration(line1) or get_declaration(line2):\r
- # the file does have an encoding declaration, so trust it\r
- infile.close()\r
- return False\r
-\r
- # check the whole file for non-ASCII characters\r
- rest = infile.read()\r
- infile.close()\r
-\r
- if has_correct_encoding(line1+line2+rest, "ascii"):\r
- return False\r
-\r
- return True\r
-\r
-\r
-usage = """Usage: %s [-cd] paths...\r
- -c: recognize Python source files trying to compile them\r
- -d: debug output""" % sys.argv[0]\r
-\r
-try:\r
- opts, args = getopt.getopt(sys.argv[1:], 'cd')\r
-except getopt.error, msg:\r
- print >>sys.stderr, msg\r
- print >>sys.stderr, usage\r
- sys.exit(1)\r
-\r
-is_python = pysource.looks_like_python\r
-debug = False\r
-\r
-for o, a in opts:\r
- if o == '-c':\r
- is_python = pysource.can_be_compiled\r
- elif o == '-d':\r
- debug = True\r
-\r
-if not args:\r
- print >>sys.stderr, usage\r
- sys.exit(1)\r
-\r
-for fullpath in pysource.walk_python_files(args, is_python):\r
- if debug:\r
- print "Testing for coding: %s" % fullpath\r
- result = needs_declaration(fullpath)\r
- if result:\r
- print fullpath\r