blob: b81871ff6a348a18c1028d4c0c0dc62d951b4f13 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum1d974171992-12-09 23:14:40 +00002
Tim Peters70c43782001-01-17 08:48:39 +00003# Fix Python source files to avoid using
4# def method(self, (arg1, ..., argn)):
Guido van Rossum1d974171992-12-09 23:14:40 +00005# instead of the more rational
Tim Peters70c43782001-01-17 08:48:39 +00006# def method(self, arg1, ..., argn):
Guido van Rossum1d974171992-12-09 23:14:40 +00007#
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'.
17#
18# Changes made are reported to stdout in a diff-like format.
19#
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.
25#
26# NB: by changing only the function fixline() you can turn this
27# into a program for a different change to Python programs...
28
29import sys
Neal Norwitz10be10c2006-03-16 06:50:13 +000030import re
Guido van Rossum1d974171992-12-09 23:14:40 +000031import os
32from stat import *
Guido van Rossum1d974171992-12-09 23:14:40 +000033
34err = sys.stderr.write
35dbg = err
36rep = sys.stdout.write
37
38def main():
Tim Peters70c43782001-01-17 08:48:39 +000039 bad = 0
40 if not sys.argv[1:]: # No arguments
41 err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
42 sys.exit(2)
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')
48 bad = 1
49 else:
50 if fix(arg): bad = 1
51 sys.exit(bad)
Guido van Rossum1d974171992-12-09 23:14:40 +000052
Neal Norwitz10be10c2006-03-16 06:50:13 +000053ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$')
Guido van Rossum1d974171992-12-09 23:14:40 +000054def ispython(name):
Tim Peters70c43782001-01-17 08:48:39 +000055 return ispythonprog.match(name) >= 0
Guido van Rossum1d974171992-12-09 23:14:40 +000056
57def recursedown(dirname):
Walter Dörwald70a6b492004-02-12 17:35:32 +000058 dbg('recursedown(%r)\n' % (dirname,))
Tim Peters70c43782001-01-17 08:48:39 +000059 bad = 0
60 try:
61 names = os.listdir(dirname)
62 except os.error, msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +000063 err('%s: cannot list directory: %r\n' % (dirname, msg))
Tim Peters70c43782001-01-17 08:48:39 +000064 return 1
65 names.sort()
66 subdirs = []
67 for name in names:
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)
73 elif ispython(name):
74 if fix(fullname): bad = 1
75 for fullname in subdirs:
76 if recursedown(fullname): bad = 1
77 return bad
Guido van Rossum1d974171992-12-09 23:14:40 +000078
79def fix(filename):
Walter Dörwald70a6b492004-02-12 17:35:32 +000080## dbg('fix(%r)\n' % (filename,))
Tim Peters70c43782001-01-17 08:48:39 +000081 try:
82 f = open(filename, 'r')
83 except IOError, msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +000084 err('%s: cannot open: %r\n' % (filename, msg))
Tim Peters70c43782001-01-17 08:48:39 +000085 return 1
86 head, tail = os.path.split(filename)
87 tempname = os.path.join(head, '@' + tail)
88 g = None
89 # If we find a match, we rewind the file and start over but
90 # now copy everything to a temp file.
91 lineno = 0
92 while 1:
93 line = f.readline()
94 if not line: break
95 lineno = lineno + 1
96 if g is None and '\0' in line:
97 # Check for binary files
98 err(filename + ': contains null bytes; not fixed\n')
99 f.close()
100 return 1
101 if lineno == 1 and g is None and line[:2] == '#!':
102 # Check for non-Python scripts
Walter Dörwaldaaab30e2002-09-11 20:36:02 +0000103 words = line[2:].split()
Neal Norwitz10be10c2006-03-16 06:50:13 +0000104 if words and re.search('[pP]ython', words[0]) < 0:
Tim Peters70c43782001-01-17 08:48:39 +0000105 msg = filename + ': ' + words[0]
106 msg = msg + ' script; not fixed\n'
107 err(msg)
108 f.close()
109 return 1
110 while line[-2:] == '\\\n':
111 nextline = f.readline()
112 if not nextline: break
113 line = line + nextline
114 lineno = lineno + 1
115 newline = fixline(line)
116 if newline != line:
117 if g is None:
118 try:
119 g = open(tempname, 'w')
120 except IOError, msg:
121 f.close()
Walter Dörwald70a6b492004-02-12 17:35:32 +0000122 err('%s: cannot create: %r\n' % (tempname, msg))
Tim Peters70c43782001-01-17 08:48:39 +0000123 return 1
124 f.seek(0)
125 lineno = 0
126 rep(filename + ':\n')
127 continue # restart from the beginning
Walter Dörwald70a6b492004-02-12 17:35:32 +0000128 rep(repr(lineno) + '\n')
Tim Peters70c43782001-01-17 08:48:39 +0000129 rep('< ' + line)
130 rep('> ' + newline)
131 if g is not None:
132 g.write(newline)
Guido van Rossum1d974171992-12-09 23:14:40 +0000133
Tim Peters70c43782001-01-17 08:48:39 +0000134 # End of file
135 f.close()
136 if not g: return 0 # No changes
Guido van Rossum1d974171992-12-09 23:14:40 +0000137
Tim Peters70c43782001-01-17 08:48:39 +0000138 # Finishing touch -- move files
139
140 # First copy the file's mode to the temp file
141 try:
142 statbuf = os.stat(filename)
143 os.chmod(tempname, statbuf[ST_MODE] & 07777)
144 except os.error, msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000145 err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
Tim Peters70c43782001-01-17 08:48:39 +0000146 # Then make a backup of the original file as filename~
147 try:
148 os.rename(filename, filename + '~')
149 except os.error, msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000150 err('%s: warning: backup failed (%r)\n' % (filename, msg))
Tim Peters70c43782001-01-17 08:48:39 +0000151 # Now move the temp file to the original file
152 try:
153 os.rename(tempname, filename)
154 except os.error, msg:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000155 err('%s: rename failed (%r)\n' % (filename, msg))
Tim Peters70c43782001-01-17 08:48:39 +0000156 return 1
157 # Return succes
158 return 0
Guido van Rossum1d974171992-12-09 23:14:40 +0000159
160
Neal Norwitz10be10c2006-03-16 06:50:13 +0000161fixpat = '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *(( *(.*) *)) *) *:'
162fixprog = re.compile(fixpat)
Guido van Rossum1d974171992-12-09 23:14:40 +0000163
164def fixline(line):
Tim Peters70c43782001-01-17 08:48:39 +0000165 if fixprog.match(line) >= 0:
166 (a, b), (c, d) = fixprog.regs[1:3]
167 line = line[:a] + line[c:d] + line[b:]
168 return line
Guido van Rossum1d974171992-12-09 23:14:40 +0000169
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +0000170if __name__ == '__main__':
171 main()