+++ /dev/null
-#! /usr/bin/env python\r
-\r
-# Fix Python source files to use the new equality test operator, i.e.,\r
-# if x = y: ...\r
-# is changed to\r
-# if x == y: ...\r
-# The script correctly tokenizes the Python program to reliably\r
-# distinguish between assignments and equality tests.\r
-#\r
-# Command line arguments are files or directories to be processed.\r
-# Directories are searched recursively for files whose name looks\r
-# like a python module.\r
-# Symbolic links are always ignored (except as explicit directory\r
-# arguments). Of course, the original file is kept as a back-up\r
-# (with a "~" attached to its name).\r
-# It complains about binaries (files containing null bytes)\r
-# and about files that are ostensibly not Python files: if the first\r
-# line starts with '#!' and does not contain the string 'python'.\r
-#\r
-# Changes made are reported to stdout in a diff-like format.\r
-#\r
-# Undoubtedly you can do this using find and sed or perl, but this is\r
-# a nice example of Python code that recurses down a directory tree\r
-# and uses regular expressions. Also note several subtleties like\r
-# preserving the file's mode and avoiding to even write a temp file\r
-# when no changes are needed for a file.\r
-#\r
-# NB: by changing only the function fixline() you can turn this\r
-# into a program for a different change to Python programs...\r
-\r
-import sys\r
-import re\r
-import os\r
-from stat import *\r
-import string\r
-\r
-err = sys.stderr.write\r
-dbg = err\r
-rep = sys.stdout.write\r
-\r
-def main():\r
- bad = 0\r
- if not sys.argv[1:]: # No arguments\r
- err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')\r
- sys.exit(2)\r
- for arg in sys.argv[1:]:\r
- if os.path.isdir(arg):\r
- if recursedown(arg): bad = 1\r
- elif os.path.islink(arg):\r
- err(arg + ': will not process symbolic links\n')\r
- bad = 1\r
- else:\r
- if fix(arg): bad = 1\r
- sys.exit(bad)\r
-\r
-ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$')\r
-def ispython(name):\r
- return ispythonprog.match(name) >= 0\r
-\r
-def recursedown(dirname):\r
- dbg('recursedown(%r)\n' % (dirname,))\r
- bad = 0\r
- try:\r
- names = os.listdir(dirname)\r
- except os.error, msg:\r
- err('%s: cannot list directory: %r\n' % (dirname, msg))\r
- return 1\r
- names.sort()\r
- subdirs = []\r
- for name in names:\r
- if name in (os.curdir, os.pardir): continue\r
- fullname = os.path.join(dirname, name)\r
- if os.path.islink(fullname): pass\r
- elif os.path.isdir(fullname):\r
- subdirs.append(fullname)\r
- elif ispython(name):\r
- if fix(fullname): bad = 1\r
- for fullname in subdirs:\r
- if recursedown(fullname): bad = 1\r
- return bad\r
-\r
-def fix(filename):\r
-## dbg('fix(%r)\n' % (dirname,))\r
- try:\r
- f = open(filename, 'r')\r
- except IOError, msg:\r
- err('%s: cannot open: %r\n' % (filename, msg))\r
- return 1\r
- head, tail = os.path.split(filename)\r
- tempname = os.path.join(head, '@' + tail)\r
- g = None\r
- # If we find a match, we rewind the file and start over but\r
- # now copy everything to a temp file.\r
- lineno = 0\r
- while 1:\r
- line = f.readline()\r
- if not line: break\r
- lineno = lineno + 1\r
- if g is None and '\0' in line:\r
- # Check for binary files\r
- err(filename + ': contains null bytes; not fixed\n')\r
- f.close()\r
- return 1\r
- if lineno == 1 and g is None and line[:2] == '#!':\r
- # Check for non-Python scripts\r
- words = string.split(line[2:])\r
- if words and re.search('[pP]ython', words[0]) < 0:\r
- msg = filename + ': ' + words[0]\r
- msg = msg + ' script; not fixed\n'\r
- err(msg)\r
- f.close()\r
- return 1\r
- while line[-2:] == '\\\n':\r
- nextline = f.readline()\r
- if not nextline: break\r
- line = line + nextline\r
- lineno = lineno + 1\r
- newline = fixline(line)\r
- if newline != line:\r
- if g is None:\r
- try:\r
- g = open(tempname, 'w')\r
- except IOError, msg:\r
- f.close()\r
- err('%s: cannot create: %r\n' % (tempname, msg))\r
- return 1\r
- f.seek(0)\r
- lineno = 0\r
- rep(filename + ':\n')\r
- continue # restart from the beginning\r
- rep(repr(lineno) + '\n')\r
- rep('< ' + line)\r
- rep('> ' + newline)\r
- if g is not None:\r
- g.write(newline)\r
-\r
- # End of file\r
- f.close()\r
- if not g: return 0 # No changes\r
-\r
- # Finishing touch -- move files\r
-\r
- # First copy the file's mode to the temp file\r
- try:\r
- statbuf = os.stat(filename)\r
- os.chmod(tempname, statbuf[ST_MODE] & 07777)\r
- except os.error, msg:\r
- err('%s: warning: chmod failed (%r)\n' % (tempname, msg))\r
- # Then make a backup of the original file as filename~\r
- try:\r
- os.rename(filename, filename + '~')\r
- except os.error, msg:\r
- err('%s: warning: backup failed (%r)\n' % (filename, msg))\r
- # Now move the temp file to the original file\r
- try:\r
- os.rename(tempname, filename)\r
- except os.error, msg:\r
- err('%s: rename failed (%r)\n' % (filename, msg))\r
- return 1\r
- # Return succes\r
- return 0\r
-\r
-\r
-from tokenize import tokenprog\r
-\r
-match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \\r
- '(':')', '[':']', '{':'}', '`':'`'}\r
-\r
-def fixline(line):\r
- # Quick check for easy case\r
- if '=' not in line: return line\r
-\r
- i, n = 0, len(line)\r
- stack = []\r
- while i < n:\r
- j = tokenprog.match(line, i)\r
- if j < 0:\r
- # A bad token; forget about the rest of this line\r
- print '(Syntax error:)'\r
- print line,\r
- return line\r
- a, b = tokenprog.regs[3] # Location of the token proper\r
- token = line[a:b]\r
- i = i+j\r
- if stack and token == stack[-1]:\r
- del stack[-1]\r
- elif match.has_key(token):\r
- stack.append(match[token])\r
- elif token == '=' and stack:\r
- line = line[:a] + '==' + line[b:]\r
- i, n = a + len('=='), len(line)\r
- elif token == '==' and not stack:\r
- print '(Warning: \'==\' at top level:)'\r
- print line,\r
- return line\r
-\r
-if __name__ == "__main__":\r
- main()\r