3 # Fix Python source files to use the new equality test operator, i.e.,
7 # The script correctly tokenizes the Python program to reliably
8 # distinguish between assignments and equality tests.
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).
16 # It complains about binaries (files containing null bytes)
17 # and about files that are ostensibly not Python files: if the first
18 # line starts with '#!' and does not contain the string 'python'.
20 # Changes made are reported to stdout in a diff-like format.
22 # Undoubtedly you can do this using find and sed or perl, but this is
23 # a nice example of Python code that recurses down a directory tree
24 # and uses regular expressions. Also note several subtleties like
25 # preserving the file's mode and avoiding to even write a temp file
26 # when no changes are needed for a file.
28 # NB: by changing only the function fixline() you can turn this
29 # 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
= re
.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' % (dirname,))
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 if g
is None and '\0' in line
:
100 # Check for binary files
101 err(filename
+ ': contains null bytes; not fixed\n')
104 if lineno
== 1 and g
is None and line
[:2] == '#!':
105 # Check for non-Python scripts
106 words
= string
.split(line
[2:])
107 if words
and re
.search('[pP]ython', words
[0]) < 0:
108 msg
= filename
+ ': ' + words
[0]
109 msg
= msg
+ ' script; not fixed\n'
113 while line
[-2:] == '\\\n':
114 nextline
= f
.readline()
115 if not nextline
: break
116 line
= line
+ nextline
118 newline
= fixline(line
)
122 g
= open(tempname
, 'w')
125 err('%s: cannot create: %r\n' % (tempname
, msg
))
129 rep(filename
+ ':\n')
130 continue # restart from the beginning
131 rep(repr(lineno
) + '\n')
139 if not g
: return 0 # No changes
141 # Finishing touch -- move files
143 # First copy the file's mode to the temp file
145 statbuf
= os
.stat(filename
)
146 os
.chmod(tempname
, statbuf
[ST_MODE
] & 07777)
147 except os
.error
, msg
:
148 err('%s: warning: chmod failed (%r)\n' % (tempname
, msg
))
149 # Then make a backup of the original file as filename~
151 os
.rename(filename
, filename
+ '~')
152 except os
.error
, msg
:
153 err('%s: warning: backup failed (%r)\n' % (filename
, msg
))
154 # Now move the temp file to the original file
156 os
.rename(tempname
, filename
)
157 except os
.error
, msg
:
158 err('%s: rename failed (%r)\n' % (filename
, msg
))
164 from tokenize
import tokenprog
166 match
= {'if':':', 'elif':':', 'while':':', 'return':'\n', \
167 '(':')', '[':']', '{':'}', '`':'`'}
170 # Quick check for easy case
171 if '=' not in line
: return line
176 j
= tokenprog
.match(line
, i
)
178 # A bad token; forget about the rest of this line
179 print '(Syntax error:)'
182 a
, b
= tokenprog
.regs
[3] # Location of the token proper
185 if stack
and token
== stack
[-1]:
187 elif match
.has_key(token
):
188 stack
.append(match
[token
])
189 elif token
== '=' and stack
:
190 line
= line
[:a
] + '==' + line
[b
:]
191 i
, n
= a
+ len('=='), len(line
)
192 elif token
== '==' and not stack
:
193 print '(Warning: \'==\' at top level:)'
197 if __name__
== "__main__":