Added function ttob.
[python/dscho.git] / Tools / scripts / methfix.py
blobb7eb442431b28264990ac89f28f6541f56436c83
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...
30 import sys
31 import regex
32 import os
33 from stat import *
34 import string
36 err = sys.stderr.write
37 dbg = err
38 rep = sys.stdout.write
40 def main():
41 bad = 0
42 if not sys.argv[1:]: # No arguments
43 err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
44 sys.exit(2)
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')
50 bad = 1
51 else:
52 if fix(arg): bad = 1
53 sys.exit(bad)
55 ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
56 def ispython(name):
57 return ispythonprog.match(name) >= 0
59 def recursedown(dirname):
60 dbg('recursedown(' + `dirname` + ')\n')
61 bad = 0
62 try:
63 names = os.listdir(dirname)
64 except os.error, msg:
65 err(dirname + ': cannot list directory: ' + `msg` + '\n')
66 return 1
67 names.sort()
68 subdirs = []
69 for name in names:
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)
75 elif ispython(name):
76 if fix(fullname): bad = 1
77 for fullname in subdirs:
78 if recursedown(fullname): bad = 1
79 return bad
81 def fix(filename):
82 ## dbg('fix(' + `filename` + ')\n')
83 try:
84 f = open(filename, 'r')
85 except IOError, msg:
86 err(filename + ': cannot open: ' + `msg` + '\n')
87 return 1
88 head, tail = os.path.split(filename)
89 tempname = os.path.join(head, '@' + tail)
90 g = None
91 # If we find a match, we rewind the file and start over but
92 # now copy everything to a temp file.
93 lineno = 0
94 while 1:
95 line = f.readline()
96 if not line: break
97 lineno = lineno + 1
98 if g is None and '\0' in line:
99 # Check for binary files
100 err(filename + ': contains null bytes; not fixed\n')
101 f.close()
102 return 1
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'
109 err(msg)
110 f.close()
111 return 1
112 while line[-2:] == '\\\n':
113 nextline = f.readline()
114 if not nextline: break
115 line = line + nextline
116 lineno = lineno + 1
117 newline = fixline(line)
118 if newline != line:
119 if g is None:
120 try:
121 g = open(tempname, 'w')
122 except IOError, msg:
123 f.close()
124 err(tempname+': cannot create: '+\
125 `msg`+'\n')
126 return 1
127 f.seek(0)
128 lineno = 0
129 rep(filename + ':\n')
130 continue # restart from the beginning
131 rep(`lineno` + '\n')
132 rep('< ' + line)
133 rep('> ' + newline)
134 if g is not None:
135 g.write(newline)
137 # End of file
138 f.close()
139 if not g: return 0 # No changes
141 # Finishing touch -- move files
143 # First copy the file's mode to the temp file
144 try:
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~
150 try:
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
155 try:
156 os.rename(tempname, filename)
157 except os.error, msg:
158 err(filename + ': rename failed (' + `msg` + ')\n')
159 return 1
160 # Return succes
161 return 0
164 fixpat = '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:'
165 fixprog = regex.compile(fixpat)
167 def fixline(line):
168 if fixprog.match(line) >= 0:
169 (a, b), (c, d) = fixprog.regs[1:3]
170 line = line[:a] + line[c:d] + line[b:]
171 return line
174 main()