Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | be5e342 | 1996-09-11 23:34:32 +0000 | [diff] [blame] | 3 | # This script is obsolete -- it is kept for historical purposes only. |
| 4 | # |
Guido van Rossum | 0a60ee1 | 1992-01-01 19:22:09 +0000 | [diff] [blame] | 5 | # Fix Python source files to use the new class definition syntax, i.e., |
Guido van Rossum | be5e342 | 1996-09-11 23:34:32 +0000 | [diff] [blame] | 6 | # the syntax used in Python versions before 0.9.8: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 7 | # class C() = base(), base(), ...: ... |
Guido van Rossum | be5e342 | 1996-09-11 23:34:32 +0000 | [diff] [blame] | 8 | # is changed to the current syntax: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 9 | # class C(base, base, ...): ... |
Guido van Rossum | be5e342 | 1996-09-11 23:34:32 +0000 | [diff] [blame] | 10 | # |
Guido van Rossum | 0a60ee1 | 1992-01-01 19:22:09 +0000 | [diff] [blame] | 11 | # The script uses heuristics to find class definitions that usually |
| 12 | # work but occasionally can fail; carefully check the output! |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 13 | # |
| 14 | # Command line arguments are files or directories to be processed. |
| 15 | # Directories are searched recursively for files whose name looks |
| 16 | # like a python module. |
| 17 | # Symbolic links are always ignored (except as explicit directory |
| 18 | # arguments). Of course, the original file is kept as a back-up |
| 19 | # (with a "~" attached to its name). |
| 20 | # |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 21 | # Changes made are reported to stdout in a diff-like format. |
| 22 | # |
| 23 | # Undoubtedly you can do this using find and sed or perl, but this is |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 24 | # a nice example of Python code that recurses down a directory tree |
| 25 | # and uses regular expressions. Also note several subtleties like |
| 26 | # preserving the file's mode and avoiding to even write a temp file |
| 27 | # when no changes are needed for a file. |
| 28 | # |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 29 | # NB: by changing only the function fixline() you can turn this |
| 30 | # into a program for a different change to Python programs... |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 31 | |
| 32 | import sys |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 33 | import regex |
Guido van Rossum | b2ac809 | 1992-03-30 11:12:23 +0000 | [diff] [blame] | 34 | import os |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 35 | from stat import * |
| 36 | |
| 37 | err = sys.stderr.write |
| 38 | dbg = err |
| 39 | rep = sys.stdout.write |
| 40 | |
| 41 | def main(): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 42 | bad = 0 |
| 43 | if not sys.argv[1:]: # No arguments |
| 44 | err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') |
| 45 | sys.exit(2) |
| 46 | for arg in sys.argv[1:]: |
| 47 | if os.path.isdir(arg): |
| 48 | if recursedown(arg): bad = 1 |
| 49 | elif os.path.islink(arg): |
| 50 | err(arg + ': will not process symbolic links\n') |
| 51 | bad = 1 |
| 52 | else: |
| 53 | if fix(arg): bad = 1 |
| 54 | sys.exit(bad) |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 56 | ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$') |
| 57 | def ispython(name): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 58 | return ispythonprog.match(name) >= 0 |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 59 | |
| 60 | def recursedown(dirname): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 61 | dbg('recursedown(' + `dirname` + ')\n') |
| 62 | bad = 0 |
| 63 | try: |
| 64 | names = os.listdir(dirname) |
| 65 | except os.error, msg: |
| 66 | err(dirname + ': cannot list directory: ' + `msg` + '\n') |
| 67 | return 1 |
| 68 | names.sort() |
| 69 | subdirs = [] |
| 70 | for name in names: |
| 71 | if name in (os.curdir, os.pardir): continue |
| 72 | fullname = os.path.join(dirname, name) |
| 73 | if os.path.islink(fullname): pass |
| 74 | elif os.path.isdir(fullname): |
| 75 | subdirs.append(fullname) |
| 76 | elif ispython(name): |
| 77 | if fix(fullname): bad = 1 |
| 78 | for fullname in subdirs: |
| 79 | if recursedown(fullname): bad = 1 |
| 80 | return bad |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 82 | def fix(filename): |
Tim Peters | 79b334b | 2001-01-17 09:13:33 +0000 | [diff] [blame] | 83 | ## dbg('fix(' + `filename` + ')\n') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 84 | try: |
| 85 | f = open(filename, 'r') |
| 86 | except IOError, msg: |
| 87 | err(filename + ': cannot open: ' + `msg` + '\n') |
| 88 | return 1 |
| 89 | head, tail = os.path.split(filename) |
| 90 | tempname = os.path.join(head, '@' + tail) |
| 91 | g = None |
| 92 | # If we find a match, we rewind the file and start over but |
| 93 | # now copy everything to a temp file. |
| 94 | lineno = 0 |
| 95 | while 1: |
| 96 | line = f.readline() |
| 97 | if not line: break |
| 98 | lineno = lineno + 1 |
| 99 | while line[-2:] == '\\\n': |
| 100 | nextline = f.readline() |
| 101 | if not nextline: break |
| 102 | line = line + nextline |
| 103 | lineno = lineno + 1 |
| 104 | newline = fixline(line) |
| 105 | if newline != line: |
| 106 | if g is None: |
| 107 | try: |
| 108 | g = open(tempname, 'w') |
| 109 | except IOError, msg: |
| 110 | f.close() |
| 111 | err(tempname+': cannot create: '+\ |
| 112 | `msg`+'\n') |
| 113 | return 1 |
| 114 | f.seek(0) |
| 115 | lineno = 0 |
| 116 | rep(filename + ':\n') |
| 117 | continue # restart from the beginning |
| 118 | rep(`lineno` + '\n') |
| 119 | rep('< ' + line) |
| 120 | rep('> ' + newline) |
| 121 | if g is not None: |
| 122 | g.write(newline) |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 123 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 124 | # End of file |
| 125 | f.close() |
| 126 | if not g: return 0 # No changes |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 127 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 128 | # Finishing touch -- move files |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 129 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 130 | # First copy the file's mode to the temp file |
| 131 | try: |
| 132 | statbuf = os.stat(filename) |
| 133 | os.chmod(tempname, statbuf[ST_MODE] & 07777) |
| 134 | except os.error, msg: |
| 135 | err(tempname + ': warning: chmod failed (' + `msg` + ')\n') |
| 136 | # Then make a backup of the original file as filename~ |
| 137 | try: |
| 138 | os.rename(filename, filename + '~') |
| 139 | except os.error, msg: |
| 140 | err(filename + ': warning: backup failed (' + `msg` + ')\n') |
| 141 | # Now move the temp file to the original file |
| 142 | try: |
| 143 | os.rename(tempname, filename) |
| 144 | except os.error, msg: |
| 145 | err(filename + ': rename failed (' + `msg` + ')\n') |
| 146 | return 1 |
| 147 | # Return succes |
| 148 | return 0 |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 150 | # This expression doesn't catch *all* class definition headers, |
| 151 | # but it's pretty darn close. |
| 152 | classexpr = '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):' |
| 153 | classprog = regex.compile(classexpr) |
| 154 | |
| 155 | # Expressions for finding base class expressions. |
| 156 | baseexpr = '^ *\(.*\) *( *) *$' |
| 157 | baseprog = regex.compile(baseexpr) |
| 158 | |
| 159 | import string |
| 160 | |
| 161 | def fixline(line): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 162 | if classprog.match(line) < 0: # No 'class' keyword -- no change |
| 163 | return line |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 164 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 165 | (a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3] |
| 166 | # a0, b0 = Whole match (up to ':') |
| 167 | # a1, b1 = First subexpression (up to classname) |
| 168 | # a2, b2 = Second subexpression (=.*) |
| 169 | head = line[:b1] |
| 170 | tail = line[b0:] # Unmatched rest of line |
| 171 | |
| 172 | if a2 == b2: # No base classes -- easy case |
| 173 | return head + ':' + tail |
| 174 | |
| 175 | # Get rid of leading '=' |
| 176 | basepart = line[a2+1:b2] |
| 177 | |
| 178 | # Extract list of base expressions |
| 179 | bases = string.splitfields(basepart, ',') |
| 180 | |
| 181 | # Strip trailing '()' from each base expression |
| 182 | for i in range(len(bases)): |
| 183 | if baseprog.match(bases[i]) >= 0: |
| 184 | x1, y1 = baseprog.regs[1] |
| 185 | bases[i] = bases[i][x1:y1] |
| 186 | |
| 187 | # Join the bases back again and build the new line |
| 188 | basepart = string.joinfields(bases, ', ') |
| 189 | |
| 190 | return head + '(' + basepart + '):' + tail |
Guido van Rossum | 11e7f62 | 1992-01-01 18:38:09 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | 2c4d7e7 | 1991-12-26 12:58:17 +0000 | [diff] [blame] | 192 | main() |