blob: 26aa59908ca6ea834ac00ac10cd15ddd4c80a46e [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum2c4d7e71991-12-26 12:58:17 +00002
Guido van Rossumbe5e3421996-09-11 23:34:32 +00003# This script is obsolete -- it is kept for historical purposes only.
4#
Guido van Rossum0a60ee11992-01-01 19:22:09 +00005# Fix Python source files to use the new class definition syntax, i.e.,
Guido van Rossumbe5e3421996-09-11 23:34:32 +00006# the syntax used in Python versions before 0.9.8:
Tim Peters70c43782001-01-17 08:48:39 +00007# class C() = base(), base(), ...: ...
Guido van Rossumbe5e3421996-09-11 23:34:32 +00008# is changed to the current syntax:
Tim Peters70c43782001-01-17 08:48:39 +00009# class C(base, base, ...): ...
Guido van Rossumbe5e3421996-09-11 23:34:32 +000010#
Guido van Rossum0a60ee11992-01-01 19:22:09 +000011# The script uses heuristics to find class definitions that usually
12# work but occasionally can fail; carefully check the output!
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000013#
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 Rossum11e7f621992-01-01 18:38:09 +000021# 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 Rossum2c4d7e71991-12-26 12:58:17 +000024# 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 Rossum11e7f621992-01-01 18:38:09 +000029# NB: by changing only the function fixline() you can turn this
30# into a program for a different change to Python programs...
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000031
32import sys
Guido van Rossum11e7f621992-01-01 18:38:09 +000033import regex
Guido van Rossumb2ac8091992-03-30 11:12:23 +000034import os
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000035from stat import *
36
37err = sys.stderr.write
38dbg = err
39rep = sys.stdout.write
40
41def main():
Tim Peters70c43782001-01-17 08:48:39 +000042 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 Rossum2c4d7e71991-12-26 12:58:17 +000055
Guido van Rossum11e7f621992-01-01 18:38:09 +000056ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
57def ispython(name):
Tim Peters70c43782001-01-17 08:48:39 +000058 return ispythonprog.match(name) >= 0
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000059
60def recursedown(dirname):
Tim Peters70c43782001-01-17 08:48:39 +000061 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 Rossum2c4d7e71991-12-26 12:58:17 +000081
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000082def fix(filename):
Tim Peters79b334b2001-01-17 09:13:33 +000083## dbg('fix(' + `filename` + ')\n')
Tim Peters70c43782001-01-17 08:48:39 +000084 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 Rossum11e7f621992-01-01 18:38:09 +0000123
Tim Peters70c43782001-01-17 08:48:39 +0000124 # End of file
125 f.close()
126 if not g: return 0 # No changes
Guido van Rossum11e7f621992-01-01 18:38:09 +0000127
Tim Peters70c43782001-01-17 08:48:39 +0000128 # Finishing touch -- move files
Guido van Rossum2c4d7e71991-12-26 12:58:17 +0000129
Tim Peters70c43782001-01-17 08:48:39 +0000130 # 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 Rossum2c4d7e71991-12-26 12:58:17 +0000149
Guido van Rossum11e7f621992-01-01 18:38:09 +0000150# This expression doesn't catch *all* class definition headers,
151# but it's pretty darn close.
152classexpr = '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'
153classprog = regex.compile(classexpr)
154
155# Expressions for finding base class expressions.
156baseexpr = '^ *\(.*\) *( *) *$'
157baseprog = regex.compile(baseexpr)
158
159import string
160
161def fixline(line):
Tim Peters70c43782001-01-17 08:48:39 +0000162 if classprog.match(line) < 0: # No 'class' keyword -- no change
163 return line
Guido van Rossum11e7f621992-01-01 18:38:09 +0000164
Tim Peters70c43782001-01-17 08:48:39 +0000165 (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 Rossum11e7f621992-01-01 18:38:09 +0000191
Guido van Rossum2c4d7e71991-12-26 12:58:17 +0000192main()