blob: 63f0e2b8d781a64444f7be787153f48a94d2f254 [file] [log] [blame]
Guido van Rossum11e7f621992-01-01 18:38:09 +00001#! /ufs/guido/bin/sgi/python
Guido van Rossum2c4d7e71991-12-26 12:58:17 +00002#! /usr/local/python
3
Guido van Rossum0a60ee11992-01-01 19:22:09 +00004# Fix Python source files to use the new class definition syntax, i.e.,
Guido van Rossum2c4d7e71991-12-26 12:58:17 +00005# class C() = base(), base(), ...: ...
Guido van Rossum0a60ee11992-01-01 19:22:09 +00006# is changed to
7# class C(base, base, ...): ...
8# The script uses heuristics to find class definitions that usually
9# work but occasionally can fail; carefully check the output!
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000010#
11# Command line arguments are files or directories to be processed.
12# Directories are searched recursively for files whose name looks
13# like a python module.
14# Symbolic links are always ignored (except as explicit directory
15# arguments). Of course, the original file is kept as a back-up
16# (with a "~" attached to its name).
17#
Guido van Rossum11e7f621992-01-01 18:38:09 +000018# Changes made are reported to stdout in a diff-like format.
19#
20# Undoubtedly you can do this using find and sed or perl, but this is
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000021# a nice example of Python code that recurses down a directory tree
22# and uses regular expressions. Also note several subtleties like
23# preserving the file's mode and avoiding to even write a temp file
24# when no changes are needed for a file.
25#
Guido van Rossum11e7f621992-01-01 18:38:09 +000026# NB: by changing only the function fixline() you can turn this
27# into a program for a different change to Python programs...
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000028
29import sys
Guido van Rossum11e7f621992-01-01 18:38:09 +000030import regex
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000031import posix
32import path
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000033from stat import *
34
35err = sys.stderr.write
36dbg = err
37rep = sys.stdout.write
38
39def main():
40 bad = 0
41 if not sys.argv[1:]: # No arguments
Guido van Rossum11e7f621992-01-01 18:38:09 +000042 err('usage: ' + argv[0] + ' file-or-directory ...\n')
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000043 sys.exit(2)
44 for arg in sys.argv[1:]:
45 if path.isdir(arg):
46 if recursedown(arg): bad = 1
47 elif path.islink(arg):
48 err(arg + ': will not process symbolic links\n')
49 bad = 1
50 else:
51 if fix(arg): bad = 1
52 sys.exit(bad)
53
Guido van Rossum11e7f621992-01-01 18:38:09 +000054ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
55def ispython(name):
56 return ispythonprog.match(name) >= 0
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000057
58def recursedown(dirname):
59 dbg('recursedown(' + `dirname` + ')\n')
60 bad = 0
61 try:
62 names = posix.listdir(dirname)
63 except posix.error, msg:
64 err(dirname + ': cannot list directory: ' + `msg` + '\n')
65 return 1
Guido van Rossum11e7f621992-01-01 18:38:09 +000066 names.sort()
67 subdirs = []
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000068 for name in names:
69 if name in ('.', '..'): continue
70 fullname = path.join(dirname, name)
71 if path.islink(fullname): pass
72 elif path.isdir(fullname):
Guido van Rossum11e7f621992-01-01 18:38:09 +000073 subdirs.append(fullname)
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000074 elif ispython(name):
75 if fix(fullname): bad = 1
Guido van Rossum11e7f621992-01-01 18:38:09 +000076 for fullname in subdirs:
77 if recursedown(fullname): bad = 1
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000078 return bad
79
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000080def fix(filename):
Guido van Rossum0a60ee11992-01-01 19:22:09 +000081## dbg('fix(' + `filename` + ')\n')
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000082 try:
83 f = open(filename, 'r')
84 except IOError, msg:
85 err(filename + ': cannot open: ' + `msg` + '\n')
86 return 1
87 head, tail = path.split(filename)
88 tempname = path.join(head, '@' + tail)
Guido van Rossum11e7f621992-01-01 18:38:09 +000089 g = None
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000090 # If we find a match, we rewind the file and start over but
91 # now copy everything to a temp file.
Guido van Rossum11e7f621992-01-01 18:38:09 +000092 lineno = 0
Guido van Rossum2c4d7e71991-12-26 12:58:17 +000093 while 1:
94 line = f.readline()
95 if not line: break
Guido van Rossum11e7f621992-01-01 18:38:09 +000096 lineno = lineno + 1
97 while line[-2:] == '\\\n':
98 nextline = f.readline()
99 if not nextline: break
100 line = line + nextline
101 lineno = lineno + 1
102 newline = fixline(line)
103 if newline != line:
104 if g is None:
105 try:
106 g = open(tempname, 'w')
107 except IOError, msg:
108 f.close()
109 err(tempname+': cannot create: '+\
110 `msg`+'\n')
111 return 1
112 f.seek(0)
113 lineno = 0
114 rep(filename + ':\n')
115 continue # restart from the beginning
116 rep(`lineno` + '\n')
117 rep('< ' + line)
118 rep('> ' + newline)
119 if g is not None:
120 g.write(newline)
121
122 # End of file
Guido van Rossum2c4d7e71991-12-26 12:58:17 +0000123 f.close()
Guido van Rossum11e7f621992-01-01 18:38:09 +0000124 if not g: return 0 # No changes
125
Guido van Rossum2c4d7e71991-12-26 12:58:17 +0000126 # Finishing touch -- move files
127
128 # First copy the file's mode to the temp file
129 try:
130 statbuf = posix.stat(filename)
Guido van Rossum7e73fd01991-12-26 13:23:22 +0000131 posix.chmod(tempname, statbuf[ST_MODE] & 07777)
Guido van Rossum2c4d7e71991-12-26 12:58:17 +0000132 except posix.error, msg:
133 err(tempname + ': warning: chmod failed (' + `msg` + ')\n')
134 # Then make a backup of the original file as filename~
135 try:
136 posix.rename(filename, filename + '~')
137 except posix.error, msg:
138 err(filename + ': warning: backup failed (' + `msg` + ')\n')
139 # Now move the temp file to the original file
140 try:
141 posix.rename(tempname, filename)
142 except posix.error, msg:
143 err(filename + ': rename failed (' + `msg` + ')\n')
144 return 1
145 # Return succes
146 return 0
147
Guido van Rossum11e7f621992-01-01 18:38:09 +0000148# This expression doesn't catch *all* class definition headers,
149# but it's pretty darn close.
150classexpr = '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'
151classprog = regex.compile(classexpr)
152
153# Expressions for finding base class expressions.
154baseexpr = '^ *\(.*\) *( *) *$'
155baseprog = regex.compile(baseexpr)
156
157import string
158
159def fixline(line):
160 if classprog.match(line) < 0: # No 'class' keyword -- no change
161 return line
162
163 (a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]
164 # a0, b0 = Whole match (up to ':')
165 # a1, b1 = First subexpression (up to classname)
166 # a2, b2 = Second subexpression (=.*)
167 head = line[:b1]
168 tail = line[b0:] # Unmatched rest of line
169
170 if a2 == b2: # No base classes -- easy case
171 return head + ':' + tail
172
173 # Get rid of leading '='
174 basepart = line[a2+1:b2]
175
176 # Extract list of base expressions
177 bases = string.splitfields(basepart, ',')
178
179 # Strip trailing '()' from each base expression
180 for i in range(len(bases)):
181 if baseprog.match(bases[i]) >= 0:
182 x1, y1 = baseprog.regs[1]
183 bases[i] = bases[i][x1:y1]
184
185 # Join the bases back again and build the new line
186 basepart = string.joinfields(bases, ', ')
187
188 return head + '(' + basepart + '):' + tail
189
Guido van Rossum2c4d7e71991-12-26 12:58:17 +0000190main()