Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/classfix.py @ 1573:eaaba2cafeeb legacy-trunk
*** empty log message ***
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 07 Mar 1994 12:05:46 +0000 |
| parents | ef082c1072a7 |
| children | e05da8043b50 |
| rev | line source |
|---|---|
|
1106
8ea546047f01
Fix bug when reporting error
Guido van Rossum <guido@python.org>
parents:
754
diff
changeset
|
1 #! /usr/local/bin/python |
| 517 | 2 |
| 554 | 3 # Fix Python source files to use the new class definition syntax, i.e., |
| 4 # class C() = base(), base(), ...: ... | |
| 5 # is changed to | |
| 517 | 6 # class C(base, base, ...): ... |
| 554 | 7 # The script uses heuristics to find class definitions that usually |
| 8 # work but occasionally can fail; carefully check the output! | |
| 517 | 9 # |
| 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). | |
| 16 # | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
17 # 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
|
18 # |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
19 # Undoubtedly you can do this using find and sed or perl, but this is |
| 517 | 20 # a nice example of Python code that recurses down a directory tree |
| 21 # and uses regular expressions. Also note several subtleties like | |
| 22 # preserving the file's mode and avoiding to even write a temp file | |
| 23 # when no changes are needed for a file. | |
| 24 # | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
25 # 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
|
26 # into a program for a different change to Python programs... |
| 517 | 27 |
| 28 import sys | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
29 import regex |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
30 import os |
| 517 | 31 from stat import * |
| 32 | |
| 33 err = sys.stderr.write | |
| 34 dbg = err | |
| 35 rep = sys.stdout.write | |
| 36 | |
| 37 def main(): | |
| 38 bad = 0 | |
| 39 if not sys.argv[1:]: # No arguments | |
|
1106
8ea546047f01
Fix bug when reporting error
Guido van Rossum <guido@python.org>
parents:
754
diff
changeset
|
40 err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') |
| 517 | 41 sys.exit(2) |
| 42 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
|
43 if os.path.isdir(arg): |
| 517 | 44 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
|
45 elif os.path.islink(arg): |
| 517 | 46 err(arg + ': will not process symbolic links\n') |
| 47 bad = 1 | |
| 48 else: | |
| 49 if fix(arg): bad = 1 | |
| 50 sys.exit(bad) | |
| 51 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
52 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
|
53 def ispython(name): |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
54 return ispythonprog.match(name) >= 0 |
| 517 | 55 |
| 56 def recursedown(dirname): | |
| 57 dbg('recursedown(' + `dirname` + ')\n') | |
| 58 bad = 0 | |
| 59 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
60 names = os.listdir(dirname) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
61 except os.error, msg: |
| 517 | 62 err(dirname + ': cannot list directory: ' + `msg` + '\n') |
| 63 return 1 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
64 names.sort() |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
65 subdirs = [] |
| 517 | 66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 subdirs.append(fullname) |
| 517 | 72 elif ispython(name): |
| 73 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
|
74 for fullname in subdirs: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
75 if recursedown(fullname): bad = 1 |
| 517 | 76 return bad |
| 77 | |
| 78 def fix(filename): | |
| 554 | 79 ## dbg('fix(' + `filename` + ')\n') |
| 517 | 80 try: |
| 81 f = open(filename, 'r') | |
| 82 except IOError, msg: | |
| 83 err(filename + ': cannot open: ' + `msg` + '\n') | |
| 84 return 1 | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
85 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
|
86 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
|
87 g = None |
| 517 | 88 # If we find a match, we rewind the file and start over but |
| 89 # 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
|
90 lineno = 0 |
| 517 | 91 while 1: |
| 92 line = f.readline() | |
| 93 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
|
94 lineno = lineno + 1 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
95 while line[-2:] == '\\\n': |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
96 nextline = f.readline() |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
97 if not nextline: break |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
98 line = line + nextline |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
99 lineno = lineno + 1 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
100 newline = fixline(line) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
101 if newline != line: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
102 if g is None: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
103 try: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
104 g = open(tempname, 'w') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
105 except IOError, msg: |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
106 f.close() |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
107 err(tempname+': cannot create: '+\ |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
108 `msg`+'\n') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
109 return 1 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
110 f.seek(0) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
111 lineno = 0 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
112 rep(filename + ':\n') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
113 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
|
114 rep(`lineno` + '\n') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
115 rep('< ' + line) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
116 rep('> ' + newline) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
117 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
|
118 g.write(newline) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
119 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
120 # End of file |
| 517 | 121 f.close() |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
122 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
|
123 |
| 517 | 124 # Finishing touch -- move files |
| 125 | |
| 126 # First copy the file's mode to the temp file | |
| 127 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
128 statbuf = os.stat(filename) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
129 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
|
130 except os.error, msg: |
| 517 | 131 err(tempname + ': warning: chmod failed (' + `msg` + ')\n') |
| 132 # Then make a backup of the original file as filename~ | |
| 133 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
134 os.rename(filename, filename + '~') |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
135 except os.error, msg: |
| 517 | 136 err(filename + ': warning: backup failed (' + `msg` + ')\n') |
| 137 # Now move the temp file to the original file | |
| 138 try: | |
|
754
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
139 os.rename(tempname, filename) |
|
6fbb03dc439e
change posix to os and path to os.path
Guido van Rossum <guido@python.org>
parents:
554
diff
changeset
|
140 except os.error, msg: |
| 517 | 141 err(filename + ': rename failed (' + `msg` + ')\n') |
| 142 return 1 | |
| 143 # Return succes | |
| 144 return 0 | |
| 145 | |
|
552
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
146 # 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
|
147 # 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
|
148 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
|
149 classprog = regex.compile(classexpr) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
150 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
151 # 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
|
152 baseexpr = '^ *\(.*\) *( *) *$' |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
153 baseprog = regex.compile(baseexpr) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
154 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
155 import string |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
156 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
157 def fixline(line): |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
158 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
|
159 return line |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
160 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
161 (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
|
162 # 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
|
163 # 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
|
164 # a2, b2 = Second subexpression (=.*) |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
165 head = line[:b1] |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
166 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
|
167 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
168 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
|
169 return head + ':' + tail |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
170 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
171 # Get rid of leading '=' |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
172 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
|
173 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
174 # 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
|
175 bases = string.splitfields(basepart, ',') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
176 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
177 # 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
183 # 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
|
184 basepart = string.joinfields(bases, ', ') |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
185 |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
186 return head + '(' + basepart + '):' + tail |
|
c158c8347654
Use new, more generic structure for *fix.py scripts.
Guido van Rossum <guido@python.org>
parents:
535
diff
changeset
|
187 |
| 517 | 188 main() |
