1 #! /usr/local/bin/python
3 # Fix Python source files to use the new class definition syntax, i.e.,
4 # class C() = base(), base(), ...: ...
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...
33 err
= sys
.stderr
.write
35 rep
= sys
.stdout
.write
39 if not sys
.argv
[1:]: # No arguments
40 err('usage: ' + sys
.argv
[0] + ' file-or-directory ...\n')
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')
52 ispythonprog
= regex
.compile('^[a-zA-Z0-9_]+\.py$')
54 return ispythonprog
.match(name
) >= 0
56 def recursedown(dirname
):
57 dbg('recursedown(' + `dirname`
+ ')\n')
60 names
= os
.listdir(dirname
)
62 err(dirname
+ ': cannot list directory: ' + `msg`
+ '\n')
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
)
73 if fix(fullname
): bad
= 1
74 for fullname
in subdirs
:
75 if recursedown(fullname
): bad
= 1
79 ## dbg('fix(' + `filename` + ')\n')
81 f
= open(filename
, 'r')
83 err(filename
+ ': cannot open: ' + `msg`
+ '\n')
85 head
, tail
= os
.path
.split(filename
)
86 tempname
= os
.path
.join(head
, '@' + tail
)
88 # If we find a match, we rewind the file and start over but
89 # now copy everything to a temp file.
95 while line
[-2:] == '\\\n':
96 nextline
= f
.readline()
97 if not nextline
: break
98 line
= line
+ nextline
100 newline
= fixline(line
)
104 g
= open(tempname
, 'w')
107 err(tempname
+': cannot create: '+\
112 rep(filename
+ ':\n')
113 continue # restart from the beginning
122 if not g
: return 0 # No changes
124 # Finishing touch -- move files
126 # First copy the file's mode to the temp file
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~
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
139 os
.rename(tempname
, filename
)
140 except os
.error
, msg
:
141 err(filename
+ ': rename failed (' + `msg`
+ ')\n')
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
)
158 if classprog
.match(line
) < 0: # No 'class' keyword -- no change
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 (=.*)
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