blob: 583d54e0e7e0f0ba7f1b368cb1659426d1f28c20 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum0af9a281992-01-01 18:38:21 +00002
Guido van Rossum318a91c1992-01-01 19:22:25 +00003# Fix Python source files to use the new equality test operator, i.e.,
Guido van Rossum0af9a281992-01-01 18:38:21 +00004# if x = y: ...
Guido van Rossum318a91c1992-01-01 19:22:25 +00005# is changed to
6# if x == y: ...
7# The script correctly tokenizes the Python program to reliably
8# distinguish between assignments and equality tests.
Guido van Rossum0af9a281992-01-01 18:38:21 +00009#
10# Command line arguments are files or directories to be processed.
11# Directories are searched recursively for files whose name looks
12# like a python module.
13# Symbolic links are always ignored (except as explicit directory
14# arguments). Of course, the original file is kept as a back-up
15# (with a "~" attached to its name).
Guido van Rossumbecdad31992-03-02 16:18:31 +000016# It complains about binaries (files containing null bytes)
17# and about files that are ostensibly not Python files: if the first
18# line starts with '#!' and does not contain the string 'python'.
Guido van Rossum0af9a281992-01-01 18:38:21 +000019#
20# Changes made are reported to stdout in a diff-like format.
21#
22# Undoubtedly you can do this using find and sed or perl, but this is
23# a nice example of Python code that recurses down a directory tree
24# and uses regular expressions. Also note several subtleties like
25# preserving the file's mode and avoiding to even write a temp file
26# when no changes are needed for a file.
27#
28# NB: by changing only the function fixline() you can turn this
29# into a program for a different change to Python programs...
30
31import sys
32import regex
Guido van Rossumb2ac8091992-03-30 11:12:23 +000033import os
Guido van Rossum0af9a281992-01-01 18:38:21 +000034from stat import *
Guido van Rossumbecdad31992-03-02 16:18:31 +000035import string
Guido van Rossum0af9a281992-01-01 18:38:21 +000036
37err = sys.stderr.write
38dbg = err
39rep = sys.stdout.write
40
41def main():
42 bad = 0
43 if not sys.argv[1:]: # No arguments
Guido van Rossumd5c57e11993-04-01 20:46:40 +000044 err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
Guido van Rossum0af9a281992-01-01 18:38:21 +000045 sys.exit(2)
46 for arg in sys.argv[1:]:
Guido van Rossumb2ac8091992-03-30 11:12:23 +000047 if os.path.isdir(arg):
Guido van Rossum0af9a281992-01-01 18:38:21 +000048 if recursedown(arg): bad = 1
Guido van Rossumb2ac8091992-03-30 11:12:23 +000049 elif os.path.islink(arg):
Guido van Rossum0af9a281992-01-01 18:38:21 +000050 err(arg + ': will not process symbolic links\n')
51 bad = 1
52 else:
53 if fix(arg): bad = 1
54 sys.exit(bad)
55
56ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
57def ispython(name):
58 return ispythonprog.match(name) >= 0
59
60def recursedown(dirname):
61 dbg('recursedown(' + `dirname` + ')\n')
62 bad = 0
63 try:
Guido van Rossumb2ac8091992-03-30 11:12:23 +000064 names = os.listdir(dirname)
65 except os.error, msg:
Guido van Rossum0af9a281992-01-01 18:38:21 +000066 err(dirname + ': cannot list directory: ' + `msg` + '\n')
67 return 1
68 names.sort()
69 subdirs = []
70 for name in names:
Guido van Rossumb2ac8091992-03-30 11:12:23 +000071 if name in (os.curdir, os.pardir): continue
72 fullname = os.path.join(dirname, name)
73 if os.path.islink(fullname): pass
74 elif os.path.isdir(fullname):
Guido van Rossum0af9a281992-01-01 18:38:21 +000075 subdirs.append(fullname)
76 elif ispython(name):
77 if fix(fullname): bad = 1
78 for fullname in subdirs:
79 if recursedown(fullname): bad = 1
80 return bad
81
82def fix(filename):
Guido van Rossum318a91c1992-01-01 19:22:25 +000083## dbg('fix(' + `filename` + ')\n')
Guido van Rossum0af9a281992-01-01 18:38:21 +000084 try:
85 f = open(filename, 'r')
86 except IOError, msg:
87 err(filename + ': cannot open: ' + `msg` + '\n')
88 return 1
Guido van Rossumb2ac8091992-03-30 11:12:23 +000089 head, tail = os.path.split(filename)
90 tempname = os.path.join(head, '@' + tail)
Guido van Rossum0af9a281992-01-01 18:38:21 +000091 g = None
92 # If we find a match, we rewind the file and start over but
93 # now copy everything to a temp file.
94 lineno = 0
95 while 1:
96 line = f.readline()
97 if not line: break
98 lineno = lineno + 1
Guido van Rossumbecdad31992-03-02 16:18:31 +000099 if g is None and '\0' in line:
100 # Check for binary files
101 err(filename + ': contains null bytes; not fixed\n')
102 f.close()
103 return 1
104 if lineno == 1 and g is None and line[:2] == '#!':
105 # Check for non-Python scripts
106 words = string.split(line[2:])
107 if words and regex.search('[pP]ython', words[0]) < 0:
108 msg = filename + ': ' + words[0]
109 msg = msg + ' script; not fixed\n'
110 err(msg)
111 f.close()
112 return 1
Guido van Rossum0af9a281992-01-01 18:38:21 +0000113 while line[-2:] == '\\\n':
114 nextline = f.readline()
115 if not nextline: break
116 line = line + nextline
117 lineno = lineno + 1
118 newline = fixline(line)
119 if newline != line:
120 if g is None:
121 try:
122 g = open(tempname, 'w')
123 except IOError, msg:
124 f.close()
125 err(tempname+': cannot create: '+\
126 `msg`+'\n')
127 return 1
128 f.seek(0)
129 lineno = 0
130 rep(filename + ':\n')
131 continue # restart from the beginning
132 rep(`lineno` + '\n')
133 rep('< ' + line)
134 rep('> ' + newline)
135 if g is not None:
136 g.write(newline)
137
138 # End of file
139 f.close()
140 if not g: return 0 # No changes
141
142 # Finishing touch -- move files
143
144 # First copy the file's mode to the temp file
145 try:
Guido van Rossumb2ac8091992-03-30 11:12:23 +0000146 statbuf = os.stat(filename)
147 os.chmod(tempname, statbuf[ST_MODE] & 07777)
148 except os.error, msg:
Guido van Rossum0af9a281992-01-01 18:38:21 +0000149 err(tempname + ': warning: chmod failed (' + `msg` + ')\n')
150 # Then make a backup of the original file as filename~
151 try:
Guido van Rossumb2ac8091992-03-30 11:12:23 +0000152 os.rename(filename, filename + '~')
153 except os.error, msg:
Guido van Rossum0af9a281992-01-01 18:38:21 +0000154 err(filename + ': warning: backup failed (' + `msg` + ')\n')
155 # Now move the temp file to the original file
156 try:
Guido van Rossumb2ac8091992-03-30 11:12:23 +0000157 os.rename(tempname, filename)
158 except os.error, msg:
Guido van Rossum0af9a281992-01-01 18:38:21 +0000159 err(filename + ': rename failed (' + `msg` + ')\n')
160 return 1
161 # Return succes
162 return 0
163
Guido van Rossum318a91c1992-01-01 19:22:25 +0000164
165from tokenize import tokenprog
166
167match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \
168 '(':')', '[':']', '{':'}', '`':'`'}
Guido van Rossum0af9a281992-01-01 18:38:21 +0000169
170def fixline(line):
Guido van Rossum318a91c1992-01-01 19:22:25 +0000171 # Quick check for easy case
172 if '=' not in line: return line
173
174 i, n = 0, len(line)
175 stack = []
176 while i < n:
177 j = tokenprog.match(line, i)
178 if j < 0:
179 # A bad token; forget about the rest of this line
180 print '(Syntax error:)'
181 print line,
182 return line
183 a, b = tokenprog.regs[3] # Location of the token proper
184 token = line[a:b]
185 i = i+j
186 if stack and token == stack[-1]:
187 del stack[-1]
188 elif match.has_key(token):
189 stack.append(match[token])
190 elif token == '=' and stack:
191 line = line[:a] + '==' + line[b:]
192 i, n = a + len('=='), len(line)
193 elif token == '==' and not stack:
194 print '(Warning: \'==\' at top level:)'
195 print line,
Guido van Rossum0af9a281992-01-01 18:38:21 +0000196 return line
197
Guido van Rossum318a91c1992-01-01 19:22:25 +0000198
Guido van Rossum0af9a281992-01-01 18:38:21 +0000199main()