3 # Fix Python source files to avoid using
4 # def method(self, (arg1, ..., argn)):
5 # instead of the more rational
6 # def method(self, arg1, ..., argn):
8 # Command line arguments are files or directories to be processed.
9 # Directories are searched recursively for files whose name looks
10 # like a python module.
11 # Symbolic links are always ignored (except as explicit directory
12 # arguments). Of course, the original file is kept as a back-up
13 # (with a "~" attached to its name).
14 # It complains about binaries (files containing null bytes)
15 # and about files that are ostensibly not Python files: if the first
16 # line starts with '#!' and does not contain the string 'python'.
18 # Changes made are reported to stdout in a diff-like format.
20 # Undoubtedly you can do this using find and sed or perl, but this is
21 # a nice example of Python code that recurses down a directory tree
22 # and uses regular expressions. Also note several subtleties like
23 # preserving the file's mode and avoiding to even write a temp file
24 # when no changes are needed for a file.
26 # NB: by changing only the function fixline() you can turn this
27 # into a program for a different change to Python programs...
34 err
= sys
.stderr
.write
36 rep
= sys
.stdout
.write
40 if not sys
.argv
[1:]: # No arguments
41 err('usage: ' + sys
.argv
[0] + ' file-or-directory ...\n')
43 for arg
in sys
.argv
[1:]:
44 if os
.path
.isdir(arg
):
45 if recursedown(arg
): bad
= 1
46 elif os
.path
.islink(arg
):
47 err(arg
+ ': will not process symbolic links\n')
53 ispythonprog
= re
.compile('^[a-zA-Z0-9_]+\.py$')
55 return ispythonprog
.match(name
) >= 0
57 def recursedown(dirname
):
58 dbg('recursedown(%r)\n' % (dirname
,))
61 names
= os
.listdir(dirname
)
63 err('%s: cannot list directory: %r\n' % (dirname
, msg
))
68 if name
in (os
.curdir
, os
.pardir
): continue
69 fullname
= os
.path
.join(dirname
, name
)
70 if os
.path
.islink(fullname
): pass
71 elif os
.path
.isdir(fullname
):
72 subdirs
.append(fullname
)
74 if fix(fullname
): bad
= 1
75 for fullname
in subdirs
:
76 if recursedown(fullname
): bad
= 1
80 ## dbg('fix(%r)\n' % (filename,))
82 f
= open(filename
, 'r')
84 err('%s: cannot open: %r\n' % (filename
, msg
))
86 head
, tail
= os
.path
.split(filename
)
87 tempname
= os
.path
.join(head
, '@' + tail
)
89 # If we find a match, we rewind the file and start over but
90 # now copy everything to a temp file.
96 if g
is None and '\0' in line
:
97 # Check for binary files
98 err(filename
+ ': contains null bytes; not fixed\n')
101 if lineno
== 1 and g
is None and line
[:2] == '#!':
102 # Check for non-Python scripts
103 words
= line
[2:].split()
104 if words
and re
.search('[pP]ython', words
[0]) < 0:
105 msg
= filename
+ ': ' + words
[0]
106 msg
= msg
+ ' script; not fixed\n'
110 while line
[-2:] == '\\\n':
111 nextline
= f
.readline()
112 if not nextline
: break
113 line
= line
+ nextline
115 newline
= fixline(line
)
119 g
= open(tempname
, 'w')
122 err('%s: cannot create: %r\n' % (tempname
, msg
))
126 rep(filename
+ ':\n')
127 continue # restart from the beginning
128 rep(repr(lineno
) + '\n')
136 if not g
: return 0 # No changes
138 # Finishing touch -- move files
140 # First copy the file's mode to the temp file
142 statbuf
= os
.stat(filename
)
143 os
.chmod(tempname
, statbuf
[ST_MODE
] & 07777)
144 except os
.error
, msg
:
145 err('%s: warning: chmod failed (%r)\n' % (tempname
, msg
))
146 # Then make a backup of the original file as filename~
148 os
.rename(filename
, filename
+ '~')
149 except os
.error
, msg
:
150 err('%s: warning: backup failed (%r)\n' % (filename
, msg
))
151 # Now move the temp file to the original file
153 os
.rename(tempname
, filename
)
154 except os
.error
, msg
:
155 err('%s: rename failed (%r)\n' % (filename
, msg
))
161 fixpat
= '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *(( *(.*) *)) *) *:'
162 fixprog
= re
.compile(fixpat
)
165 if fixprog
.match(line
) >= 0:
166 (a
, b
), (c
, d
) = fixprog
.regs
[1:3]
167 line
= line
[:a
] + line
[c
:d
] + line
[b
:]
170 if __name__
== '__main__':