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(%r)\n' % (dirname
,))
64 names
= os
.listdir(dirname
)
66 err('%s: cannot list directory: %r\n' % (dirname
, msg
))
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(%r)\n' % (filename,))
85 f
= open(filename
, 'r')
87 err('%s: cannot open: %r\n' % (filename
, msg
))
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('%s: cannot create: %r\n' % (tempname
, msg
))
115 rep(filename
+ ':\n')
116 continue # restart from the beginning
117 rep(repr(lineno
) + '\n')
125 if not g
: return 0 # No changes
127 # Finishing touch -- move files
129 # First copy the file's mode to the temp file
131 statbuf
= os
.stat(filename
)
132 os
.chmod(tempname
, statbuf
[ST_MODE
] & 07777)
133 except os
.error
, msg
:
134 err('%s: warning: chmod failed (%r)\n' % (tempname
, msg
))
135 # Then make a backup of the original file as filename~
137 os
.rename(filename
, filename
+ '~')
138 except os
.error
, msg
:
139 err('%s: warning: backup failed (%r)\n' % (filename
, msg
))
140 # Now move the temp file to the original file
142 os
.rename(tempname
, filename
)
143 except os
.error
, msg
:
144 err('%s: rename failed (%r)\n' % (filename
, msg
))
149 # This expression doesn't catch *all* class definition headers,
150 # but it's pretty darn close.
151 classexpr
= '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'
152 classprog
= regex
.compile(classexpr
)
154 # Expressions for finding base class expressions.
155 baseexpr
= '^ *\(.*\) *( *) *$'
156 baseprog
= regex
.compile(baseexpr
)
159 if classprog
.match(line
) < 0: # No 'class' keyword -- no change
162 (a0
, b0
), (a1
, b1
), (a2
, b2
) = classprog
.regs
[:3]
163 # a0, b0 = Whole match (up to ':')
164 # a1, b1 = First subexpression (up to classname)
165 # a2, b2 = Second subexpression (=.*)
167 tail
= line
[b0
:] # Unmatched rest of line
169 if a2
== b2
: # No base classes -- easy case
170 return head
+ ':' + tail
172 # Get rid of leading '='
173 basepart
= line
[a2
+1:b2
]
175 # Extract list of base expressions
176 bases
= basepart
.split(',')
178 # Strip trailing '()' from each base expression
179 for i
in range(len(bases
)):
180 if baseprog
.match(bases
[i
]) >= 0:
181 x1
, y1
= baseprog
.regs
[1]
182 bases
[i
] = bases
[i
][x1
:y1
]
184 # Join the bases back again and build the new line
185 basepart
= ', '.join(bases
)
187 return head
+ '(' + basepart
+ '):' + tail