3 # This script is obsolete -- it is kept for historical purposes only.
5 # Fix Python source files to use the new class definition syntax, i.e.,
6 # the syntax used in Python versions before 0.9.8:
7 # class C() = base(), base(), ...: ...
8 # is changed to the current syntax:
9 # class C(base, base, ...): ...
11 # The script uses heuristics to find class definitions that usually
12 # work but occasionally can fail; carefully check the output!
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).
21 # Changes made are reported to stdout in a diff-like format.
23 # Undoubtedly you can do this using find and sed or perl, but this is
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.
29 # NB: by changing only the function fixline() you can turn this
30 # into a program for a different change to Python programs...
37 err
= sys
.stderr
.write
39 rep
= sys
.stdout
.write
43 if not sys
.argv
[1:]: # No arguments
44 err('usage: ' + sys
.argv
[0] + ' file-or-directory ...\n')
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')
56 ispythonprog
= regex
.compile('^[a-zA-Z0-9_]+\.py$')
58 return ispythonprog
.match(name
) >= 0
60 def recursedown(dirname
):
61 dbg('recursedown(' + `dirname`
+ ')\n')
64 names
= os
.listdir(dirname
)
66 err(dirname
+ ': cannot list directory: ' + `msg`
+ '\n')
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
)
77 if fix(fullname
): bad
= 1
78 for fullname
in subdirs
:
79 if recursedown(fullname
): bad
= 1
83 ## dbg('fix(' + `filename` + ')\n')
85 f
= open(filename
, 'r')
87 err(filename
+ ': cannot open: ' + `msg`
+ '\n')
89 head
, tail
= os
.path
.split(filename
)
90 tempname
= os
.path
.join(head
, '@' + tail
)
92 # If we find a match, we rewind the file and start over but
93 # now copy everything to a temp file.
99 while line
[-2:] == '\\\n':
100 nextline
= f
.readline()
101 if not nextline
: break
102 line
= line
+ nextline
104 newline
= fixline(line
)
108 g
= open(tempname
, 'w')
111 err(tempname
+': cannot create: '+\
116 rep(filename
+ ':\n')
117 continue # restart from the beginning
126 if not g
: return 0 # No changes
128 # Finishing touch -- move files
130 # First copy the file's mode to the temp file
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~
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
143 os
.rename(tempname
, filename
)
144 except os
.error
, msg
:
145 err(filename
+ ': rename failed (' + `msg`
+ ')\n')
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
)
155 # Expressions for finding base class expressions.
156 baseexpr
= '^ *\(.*\) *( *) *$'
157 baseprog
= regex
.compile(baseexpr
)
162 if classprog
.match(line
) < 0: # No 'class' keyword -- no change
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 (=.*)
170 tail
= line
[b0
:] # Unmatched rest of line
172 if a2
== b2
: # No base classes -- easy case
173 return head
+ ':' + tail
175 # Get rid of leading '='
176 basepart
= line
[a2
+1:b2
]
178 # Extract list of base expressions
179 bases
= string
.splitfields(basepart
, ',')
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
]
187 # Join the bases back again and build the new line
188 basepart
= string
.joinfields(bases
, ', ')
190 return head
+ '(' + basepart
+ '):' + tail