1 #! /usr/local/bin/python
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...
35 err
= sys
.stderr
.write
37 rep
= sys
.stdout
.write
41 if not sys
.argv
[1:]: # No arguments
42 err('usage: ' + sys
.argv
[0] + ' file-or-directory ...\n')
44 for arg
in sys
.argv
[1:]:
45 if os
.path
.isdir(arg
):
46 if recursedown(arg
): bad
= 1
47 elif os
.path
.islink(arg
):
48 err(arg
+ ': will not process symbolic links\n')
54 ispythonprog
= regex
.compile('^[a-zA-Z0-9_]+\.py$')
56 return ispythonprog
.match(name
) >= 0
58 def recursedown(dirname
):
59 dbg('recursedown(' + `dirname`
+ ')\n')
62 names
= os
.listdir(dirname
)
64 err(dirname
+ ': cannot list directory: ' + `msg`
+ '\n')
69 if name
in (os
.curdir
, os
.pardir
): continue
70 fullname
= os
.path
.join(dirname
, name
)
71 if os
.path
.islink(fullname
): pass
72 elif os
.path
.isdir(fullname
):
73 subdirs
.append(fullname
)
75 if fix(fullname
): bad
= 1
76 for fullname
in subdirs
:
77 if recursedown(fullname
): bad
= 1
81 ## dbg('fix(' + `filename` + ')\n')
83 f
= open(filename
, 'r')
85 err(filename
+ ': cannot open: ' + `msg`
+ '\n')
87 head
, tail
= os
.path
.split(filename
)
88 tempname
= os
.path
.join(head
, '@' + tail
)
90 # If we find a match, we rewind the file and start over but
91 # now copy everything to a temp file.
97 if g
is None and '\0' in line
:
98 # Check for binary files
99 err(filename
+ ': contains null bytes; not fixed\n')
102 if lineno
== 1 and g
is None and line
[:2] == '#!':
103 # Check for non-Python scripts
104 words
= string
.split(line
[2:])
105 if words
and regex
.search('[pP]ython', words
[0]) < 0:
106 msg
= filename
+ ': ' + words
[0]
107 msg
= msg
+ ' script; not fixed\n'
111 while line
[-2:] == '\\\n':
112 nextline
= f
.readline()
113 if not nextline
: break
114 line
= line
+ nextline
116 newline
= fixline(line
)
120 g
= open(tempname
, 'w')
123 err(tempname
+': cannot create: '+\
128 rep(filename
+ ':\n')
129 continue # restart from the beginning
138 if not g
: return 0 # No changes
140 # Finishing touch -- move files
142 # First copy the file's mode to the temp file
144 statbuf
= os
.stat(filename
)
145 os
.chmod(tempname
, statbuf
[ST_MODE
] & 07777)
146 except os
.error
, msg
:
147 err(tempname
+ ': warning: chmod failed (' + `msg`
+ ')\n')
148 # Then make a backup of the original file as filename~
150 os
.rename(filename
, filename
+ '~')
151 except os
.error
, msg
:
152 err(filename
+ ': warning: backup failed (' + `msg`
+ ')\n')
153 # Now move the temp file to the original file
155 os
.rename(tempname
, filename
)
156 except os
.error
, msg
:
157 err(filename
+ ': rename failed (' + `msg`
+ ')\n')
163 fixpat
= '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:'
164 fixprog
= regex
.compile(fixpat
)
167 if fixprog
.match(line
) >= 0:
168 (a
, b
), (c
, d
) = fixprog
.regs
[1:3]
169 line
= line
[:a
] + line
[c
:d
] + line
[b
:]