Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/classfix.py @ 1106:8ea546047f01 legacy-trunk
Fix bug when reporting error
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 10 Dec 1992 00:01:24 +0000 |
| parents | 6fbb03dc439e |
| children | ef082c1072a7 |
| rev | line source |
|---|---|
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
1 #! /ufs/guido/bin/sgi/python |
|
1106
8ea546047f01
Fix bug when reporting error
Guido van Rossum <guido@python.org>
parents:
754
diff
changeset
|
2 #! /usr/local/bin/python |
| 517 | 3 |
| 554 | 4 # Fix Python source files to use the new class definition syntax, i.e., |
| 5 # class C() = base(), base(), ...: ... | |
| 6 # is changed to | |
| 517 | 7 # class C(base, base, ...): ... |
| 554 | 8 # The script uses heuristics to find class definitions that usually |
| 9 # work but occasionally can fail; carefully check the output! | |
| 517 | 10 # |
| 11 # Command line arguments are files or directories to be processed. | |
| 12 # Directories are searched recursively for files whose name looks | |
| 13 # like a python module. | |
| 14 # Symbolic links are always ignored (except as explicit directory | |
| 15 # arguments). Of course, the original file is kept as a back-up | |
| 16 # (with a "~" attached to its name). | |
| 17 # | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
18 # Changes made are reported to stdout in a diff-like format. |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
19 # |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
20 # Undoubtedly you can do this using find and sed or perl, but this is |
| 517 | 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 # | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
26 # NB: by changing only the function fixline() you can turn this |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
27 # into a program for a different change to Python programs... |
| 517 | 28 |
| 29 import sys | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
30 import regex |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
31 import os |
| 517 | 32 from stat import * |
| 33 | |
| 34 err = sys.stderr.write | |
| 35 dbg = err | |
| 36 rep = sys.stdout.write | |
| 37 | |
| 38 def main(): | |
| 39 bad = 0 | |
| 40 if not sys.argv[1:]: # No arguments | |
|
1106
8ea546047f01
Fix bug when reporting error
Guido van Rossum <guido@python.org>
parents:
754
diff
changeset
|
41 err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') |
| 517 | 42 sys.exit(2) |
| 43 for arg in sys.argv[1:]: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
44 if os.path.isdir(arg): |
| 517 | 45 if recursedown(arg): bad = 1 |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
46 elif os.path.islink(arg): |
| 517 | 47 err(arg + ': will not process symbolic links\n') |
| 48 bad = 1 | |
| 49 else: | |
| 50 if fix(arg): bad = 1 | |
| 51 sys.exit(bad) | |
| 52 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
53 ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
54 def ispython(name): |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
55 return ispythonprog.match(name) >= 0 |
| 517 | 56 |
| 57 def recursedown(dirname): | |
| 58 dbg('recursedown(' + `dirname` + ')\n') | |
| 59 bad = 0 | |
| 60 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
61 names = os.listdir(dirname) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
62 except os.error, msg: |
| 517 | 63 err(dirname + ': cannot list directory: ' + `msg` + '\n') |
| 64 return 1 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
65 names.sort() |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
66 subdirs = [] |
| 517 | 67 for name in names: |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
68 if name in (os.curdir, os.pardir): continue |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
69 fullname = os.path.join(dirname, name) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
70 if os.path.islink(fullname): pass |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
71 elif os.path.isdir(fullname): |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
72 subdirs.append(fullname) |
| 517 | 73 elif ispython(name): |
| 74 if fix(fullname): bad = 1 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
75 for fullname in subdirs: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
76 if recursedown(fullname): bad = 1 |
| 517 | 77 return bad |
| 78 | |
| 79 def fix(filename): | |
| 554 | 80 ## dbg('fix(' + `filename` + ')\n') |
| 517 | 81 try: |
| 82 f = open(filename, 'r') | |
| 83 except IOError, msg: | |
| 84 err(filename + ': cannot open: ' + `msg` + '\n') | |
| 85 return 1 | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
86 head, tail = os.path.split(filename) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
87 tempname = os.path.join(head, '@' + tail) |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
88 g = None |
| 517 | 89 # If we find a match, we rewind the file and start over but |
| 90 # now copy everything to a temp file. | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
91 lineno = 0 |
| 517 | 92 while 1: |
| 93 line = f.readline() | |
| 94 if not line: break | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
95 lineno = lineno + 1 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
96 while line[-2:] == '\\\n': |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
97 nextline = f.readline() |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
98 if not nextline: break |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
99 line = line + nextline |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
100 lineno = lineno + 1 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
101 newline = fixline(line) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
102 if newline != line: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
103 if g is None: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
104 try: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
105 g = open(tempname, 'w') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
106 except IOError, msg: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
107 f.close() |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
108 err(tempname+': cannot create: '+\ |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
109 `msg`+'\n') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
110 return 1 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
111 f.seek(0) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
112 lineno = 0 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
113 rep(filename + ':\n') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
114 continue # restart from the beginning |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
115 rep(`lineno` + '\n') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
116 rep('< ' + line) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
117 rep('> ' + newline) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
118 if g is not None: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
119 g.write(newline) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
120 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
121 # End of file |
| 517 | 122 f.close() |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
123 if not g: return 0 # No changes |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
124 |
| 517 | 125 # Finishing touch -- move files |
| 126 | |
| 127 # First copy the file's mode to the temp file | |
| 128 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
129 statbuf = os.stat(filename) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
130 os.chmod(tempname, statbuf[ST_MODE] & 07777) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
131 except os.error, msg: |
| 517 | 132 err(tempname + ': warning: chmod failed (' + `msg` + ')\n') |
| 133 # Then make a backup of the original file as filename~ | |
| 134 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
135 os.rename(filename, filename + '~') |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
136 except os.error, msg: |
| 517 | 137 err(filename + ': warning: backup failed (' + `msg` + ')\n') |
| 138 # Now move the temp file to the original file | |
| 139 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
140 os.rename(tempname, filename) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
141 except os.error, msg: |
| 517 | 142 err(filename + ': rename failed (' + `msg` + ')\n') |
| 143 return 1 | |
| 144 # Return succes | |
| 145 return 0 | |
| 146 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
147 # This expression doesn't catch *all* class definition headers, |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
148 # but it's pretty darn close. |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
149 classexpr = '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):' |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
150 classprog = regex.compile(classexpr) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
151 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
152 # Expressions for finding base class expressions. |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
153 baseexpr = '^ *\(.*\) *( *) *$' |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
154 baseprog = regex.compile(baseexpr) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
155 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
156 import string |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
157 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
158 def fixline(line): |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
159 if classprog.match(line) < 0: # No 'class' keyword -- no change |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
160 return line |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
161 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
162 (a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3] |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
163 # a0, b0 = Whole match (up to ':') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
164 # a1, b1 = First subexpression (up to classname) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
165 # a2, b2 = Second subexpression (=.*) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
166 head = line[:b1] |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
167 tail = line[b0:] # Unmatched rest of line |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
168 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
169 if a2 == b2: # No base classes -- easy case |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
170 return head + ':' + tail |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
171 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
172 # Get rid of leading '=' |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
173 basepart = line[a2+1:b2] |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
174 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
175 # Extract list of base expressions |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
176 bases = string.splitfields(basepart, ',') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
177 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
178 # Strip trailing '()' from each base expression |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
179 for i in range(len(bases)): |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
180 if baseprog.match(bases[i]) >= 0: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
181 x1, y1 = baseprog.regs[1] |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
182 bases[i] = bases[i][x1:y1] |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
183 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
184 # Join the bases back again and build the new line |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
185 basepart = string.joinfields(bases, ', ') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
186 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
187 return head + '(' + basepart + '):' + tail |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
188 |
| 517 | 189 main() |
