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