+++ /dev/null
-#! /usr/bin/env python\r
-\r
-# This script is obsolete -- it is kept for historical purposes only.\r
-#\r
-# Fix Python source files to use the new class definition syntax, i.e.,\r
-# the syntax used in Python versions before 0.9.8:\r
-# class C() = base(), base(), ...: ...\r
-# is changed to the current syntax:\r
-# class C(base, base, ...): ...\r
-#\r
-# The script uses heuristics to find class definitions that usually\r
-# work but occasionally can fail; carefully check the output!\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
-#\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
-\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' % (filename,))\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
- 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
-# This expression doesn't catch *all* class definition headers,\r
-# but it's pretty darn close.\r
-classexpr = '^([ \t]*class +[a-zA-Z0-9_]+) *( *) *((=.*)?):'\r
-classprog = re.compile(classexpr)\r
-\r
-# Expressions for finding base class expressions.\r
-baseexpr = '^ *(.*) *( *) *$'\r
-baseprog = re.compile(baseexpr)\r
-\r
-def fixline(line):\r
- if classprog.match(line) < 0: # No 'class' keyword -- no change\r
- return line\r
-\r
- (a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]\r
- # a0, b0 = Whole match (up to ':')\r
- # a1, b1 = First subexpression (up to classname)\r
- # a2, b2 = Second subexpression (=.*)\r
- head = line[:b1]\r
- tail = line[b0:] # Unmatched rest of line\r
-\r
- if a2 == b2: # No base classes -- easy case\r
- return head + ':' + tail\r
-\r
- # Get rid of leading '='\r
- basepart = line[a2+1:b2]\r
-\r
- # Extract list of base expressions\r
- bases = basepart.split(',')\r
-\r
- # Strip trailing '()' from each base expression\r
- for i in range(len(bases)):\r
- if baseprog.match(bases[i]) >= 0:\r
- x1, y1 = baseprog.regs[1]\r
- bases[i] = bases[i][x1:y1]\r
-\r
- # Join the bases back again and build the new line\r
- basepart = ', '.join(bases)\r
-\r
- return head + '(' + basepart + '):' + tail\r
-\r
-if __name__ == '__main__':\r
- main()\r