1 #! /ufs/guido/bin/sgi/python
2 #! /usr/local/bin/python
4 # Fix Python source files to avoid using
5 # def method(self, (arg1, ..., argn)):
6 # instead of the more rational
7 # def method(self, arg1, ..., argn):
9 # Command line arguments are files or directories to be processed.
10 # Directories are searched recursively for files whose name looks
11 # like a python module.
12 # Symbolic links are always ignored (except as explicit directory
13 # arguments). Of course, the original file is kept as a back-up
14 # (with a "~" attached to its name).
15 # It complains about binaries (files containing null bytes)
16 # and about files that are ostensibly not Python files: if the first
17 # line starts with '#!' and does not contain the string 'python'.
19 # Changes made are reported to stdout in a diff-like format.
21 # Undoubtedly you can do this using find and sed or perl, but this is
22 # a nice example of Python code that recurses down a directory tree
23 # and uses regular expressions. Also note several subtleties like
24 # preserving the file's mode and avoiding to even write a temp file
25 # when no changes are needed for a file.
27 # NB: by changing only the function fixline() you can turn this
28 # into a program for a different change to Python programs...
36 err
= sys
.stderr
.write
38 rep
= sys
.stdout
.write
42 if not sys
.argv
[1:]: # No arguments
43 err('usage: ' + sys
.argv
[0] + ' file-or-directory ...\n')
45 for arg
in sys
.argv
[1:]:
46 if os
.path
.isdir(arg
):
47 if recursedown(arg
): bad
= 1
48 elif os
.path
.islink(arg
):
49 err(arg
+ ': will not process symbolic links\n')
55 ispythonprog
= regex
.compile('^[a-zA-Z0-9_]+\.py$')
57 return ispythonprog
.match(name
) >= 0
59 def recursedown(dirname
):
60 dbg('recursedown(' + `dirname`
+ ')\n')
63 names
= os
.listdir(dirname
)
65 err(dirname
+ ': cannot list directory: ' + `msg`
+ '\n')
70 if name
in (os
.curdir
, os
.pardir
): continue
71 fullname
= os
.path
.join(dirname
, name
)
72 if os
.path
.islink(fullname
): pass
73 elif os
.path
.isdir(fullname
):
74 subdirs
.append(fullname
)
76 if fix(fullname
): bad
= 1
77 for fullname
in subdirs
:
78 if recursedown(fullname
): bad
= 1
82 ## dbg('fix(' + `filename` + ')\n')
84 f
= open(filename
, 'r')
86 err(filename
+ ': cannot open: ' + `msg`
+ '\n')
88 head
, tail
= os
.path
.split(filename
)
89 tempname
= os
.path
.join(head
, '@' + tail
)
91 # If we find a match, we rewind the file and start over but
92 # now copy everything to a temp file.
98 if g
is None and '\0' in line
:
99 # Check for binary files
100 err(filename
+ ': contains null bytes; not fixed\n')
103 if lineno
== 1 and g
is None and line
[:2] == '#!':
104 # Check for non-Python scripts
105 words
= string
.split(line
[2:])
106 if words
and regex
.search('[pP]ython', words
[0]) < 0:
107 msg
= filename
+ ': ' + words
[0]
108 msg
= msg
+ ' script; not fixed\n'
112 while line
[-2:] == '\\\n':
113 nextline
= f
.readline()
114 if not nextline
: break
115 line
= line
+ nextline
117 newline
= fixline(line
)
121 g
= open(tempname
, 'w')
124 err(tempname
+': cannot create: '+\
129 rep(filename
+ ':\n')
130 continue # restart from the beginning
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(tempname
+ ': warning: chmod failed (' + `msg`
+ ')\n')
149 # Then make a backup of the original file as filename~
151 os
.rename(filename
, filename
+ '~')
152 except os
.error
, msg
:
153 err(filename
+ ': warning: backup failed (' + `msg`
+ ')\n')
154 # Now move the temp file to the original file
156 os
.rename(tempname
, filename
)
157 except os
.error
, msg
:
158 err(filename
+ ': rename failed (' + `msg`
+ ')\n')
164 fixpat
= '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:'
165 fixprog
= regex
.compile(fixpat
)
168 if fixprog
.match(line
) >= 0:
169 (a
, b
), (c
, d
) = fixprog
.regs
[1:3]
170 line
= line
[:a
] + line
[c
:d
] + line
[b
:]