mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Use new, more generic structure for *fix.py scripts.
This commit is contained in:
parent
b824fc6773
commit
11e7f62dbc
1 changed files with 90 additions and 58 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#! /ufs/guido/bin/sgi/python
|
||||||
#! /usr/local/python
|
#! /usr/local/python
|
||||||
|
|
||||||
# Fix Python source files to use the new class definition syntax,
|
# Fix Python source files to use the new class definition syntax,
|
||||||
|
@ -13,19 +14,21 @@
|
||||||
# arguments). Of course, the original file is kept as a back-up
|
# arguments). Of course, the original file is kept as a back-up
|
||||||
# (with a "~" attached to its name).
|
# (with a "~" attached to its name).
|
||||||
#
|
#
|
||||||
# Undoubtedly you can do this using find and sed, but this is
|
# Changes made are reported to stdout in a diff-like format.
|
||||||
|
#
|
||||||
|
# Undoubtedly you can do this using find and sed or perl, but this is
|
||||||
# a nice example of Python code that recurses down a directory tree
|
# a nice example of Python code that recurses down a directory tree
|
||||||
# and uses regular expressions. Also note several subtleties like
|
# and uses regular expressions. Also note several subtleties like
|
||||||
# preserving the file's mode and avoiding to even write a temp file
|
# preserving the file's mode and avoiding to even write a temp file
|
||||||
# when no changes are needed for a file.
|
# when no changes are needed for a file.
|
||||||
#
|
#
|
||||||
# Changes made are reported to stdout in a diff-like format.
|
# NB: by changing only the function fixline() you can turn this
|
||||||
|
# into a program for a different change to Python programs...
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import regexp
|
import regex
|
||||||
import posix
|
import posix
|
||||||
import path
|
import path
|
||||||
import string
|
|
||||||
from stat import *
|
from stat import *
|
||||||
|
|
||||||
err = sys.stderr.write
|
err = sys.stderr.write
|
||||||
|
@ -35,7 +38,7 @@ rep = sys.stdout.write
|
||||||
def main():
|
def main():
|
||||||
bad = 0
|
bad = 0
|
||||||
if not sys.argv[1:]: # No arguments
|
if not sys.argv[1:]: # No arguments
|
||||||
err('usage: classfix file-or-directory ...\n')
|
err('usage: ' + argv[0] + ' file-or-directory ...\n')
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
for arg in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
if path.isdir(arg):
|
if path.isdir(arg):
|
||||||
|
@ -47,7 +50,9 @@ def main():
|
||||||
if fix(arg): bad = 1
|
if fix(arg): bad = 1
|
||||||
sys.exit(bad)
|
sys.exit(bad)
|
||||||
|
|
||||||
ispython = regexp.compile('^[a-zA-Z0-9_]+\.py$').match # This is a method!
|
ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$')
|
||||||
|
def ispython(name):
|
||||||
|
return ispythonprog.match(name) >= 0
|
||||||
|
|
||||||
def recursedown(dirname):
|
def recursedown(dirname):
|
||||||
dbg('recursedown(' + `dirname` + ')\n')
|
dbg('recursedown(' + `dirname` + ')\n')
|
||||||
|
@ -57,26 +62,22 @@ def recursedown(dirname):
|
||||||
except posix.error, msg:
|
except posix.error, msg:
|
||||||
err(dirname + ': cannot list directory: ' + `msg` + '\n')
|
err(dirname + ': cannot list directory: ' + `msg` + '\n')
|
||||||
return 1
|
return 1
|
||||||
|
names.sort()
|
||||||
|
subdirs = []
|
||||||
for name in names:
|
for name in names:
|
||||||
if name in ('.', '..'): continue
|
if name in ('.', '..'): continue
|
||||||
fullname = path.join(dirname, name)
|
fullname = path.join(dirname, name)
|
||||||
if path.islink(fullname): pass
|
if path.islink(fullname): pass
|
||||||
elif path.isdir(fullname):
|
elif path.isdir(fullname):
|
||||||
if recursedown(fullname): bad = 1
|
subdirs.append(fullname)
|
||||||
elif ispython(name):
|
elif ispython(name):
|
||||||
if fix(fullname): bad = 1
|
if fix(fullname): bad = 1
|
||||||
|
for fullname in subdirs:
|
||||||
|
if recursedown(fullname): bad = 1
|
||||||
return bad
|
return bad
|
||||||
|
|
||||||
# This expression doesn't catch *all* class definition headers,
|
|
||||||
# but it's darn pretty close.
|
|
||||||
classexpr = '^([ \t]*class +[a-zA-Z0-9_]+) *\( *\) *((=.*)?):'
|
|
||||||
findclass = regexp.compile(classexpr).match # This is a method!
|
|
||||||
|
|
||||||
baseexpr = '^ *(.*) *\( *\) *$'
|
|
||||||
findbase = regexp.compile(baseexpr).match # This is a method, too!
|
|
||||||
|
|
||||||
def fix(filename):
|
def fix(filename):
|
||||||
## dbg('fix(' + `filename` + ')\n')
|
dbg('fix(' + `filename` + ')\n')
|
||||||
try:
|
try:
|
||||||
f = open(filename, 'r')
|
f = open(filename, 'r')
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
|
@ -84,53 +85,42 @@ def fix(filename):
|
||||||
return 1
|
return 1
|
||||||
head, tail = path.split(filename)
|
head, tail = path.split(filename)
|
||||||
tempname = path.join(head, '@' + tail)
|
tempname = path.join(head, '@' + tail)
|
||||||
tf = None
|
g = None
|
||||||
# If we find a match, we rewind the file and start over but
|
# If we find a match, we rewind the file and start over but
|
||||||
# now copy everything to a temp file.
|
# now copy everything to a temp file.
|
||||||
|
lineno = 0
|
||||||
while 1:
|
while 1:
|
||||||
line = f.readline()
|
line = f.readline()
|
||||||
if not line: break
|
if not line: break
|
||||||
res = findclass(line)
|
lineno = lineno + 1
|
||||||
if not res:
|
while line[-2:] == '\\\n':
|
||||||
if tf: tf.write(line)
|
nextline = f.readline()
|
||||||
continue
|
if not nextline: break
|
||||||
if not tf:
|
line = line + nextline
|
||||||
|
lineno = lineno + 1
|
||||||
|
newline = fixline(line)
|
||||||
|
if newline != line:
|
||||||
|
if g is None:
|
||||||
try:
|
try:
|
||||||
tf = open(tempname, 'w')
|
g = open(tempname, 'w')
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
f.close()
|
f.close()
|
||||||
err(tempname+': cannot create: '+`msg`+'\n')
|
err(tempname+': cannot create: '+\
|
||||||
|
`msg`+'\n')
|
||||||
return 1
|
return 1
|
||||||
rep(filename + ':\n')
|
|
||||||
# Rewind the input file and start all over:
|
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
continue
|
lineno = 0
|
||||||
a0, b0 = res[0] # Whole match (up to ':')
|
rep(filename + ':\n')
|
||||||
a1, b1 = res[1] # First subexpression (up to classname)
|
continue # restart from the beginning
|
||||||
a2, b2 = res[2] # Second subexpression (=.*)
|
rep(`lineno` + '\n')
|
||||||
head = line[:b1]
|
|
||||||
tail = line[b0:] # Unmatched rest of line
|
|
||||||
if a2 = b2: # No base classes -- easy case
|
|
||||||
newline = head + ':' + tail
|
|
||||||
else:
|
|
||||||
# Get rid of leading '='
|
|
||||||
basepart = line[a2+1:b2]
|
|
||||||
# Extract list of base expressions
|
|
||||||
bases = string.splitfields(basepart, ',')
|
|
||||||
# Strip trailing '()' from each base expression
|
|
||||||
for i in range(len(bases)):
|
|
||||||
res = findbase(bases[i])
|
|
||||||
if res:
|
|
||||||
(x0, y0), (x1, y1) = res
|
|
||||||
bases[i] = bases[i][x1:y1]
|
|
||||||
# Join the bases back again and build the new line
|
|
||||||
basepart = string.joinfields(bases, ', ')
|
|
||||||
newline = head + '(' + basepart + '):' + tail
|
|
||||||
rep('< ' + line)
|
rep('< ' + line)
|
||||||
rep('> ' + newline)
|
rep('> ' + newline)
|
||||||
tf.write(newline)
|
if g is not None:
|
||||||
|
g.write(newline)
|
||||||
|
|
||||||
|
# End of file
|
||||||
f.close()
|
f.close()
|
||||||
if not tf: return 0 # No changes
|
if not g: return 0 # No changes
|
||||||
|
|
||||||
# Finishing touch -- move files
|
# Finishing touch -- move files
|
||||||
|
|
||||||
|
@ -154,4 +144,46 @@ def fix(filename):
|
||||||
# Return succes
|
# Return succes
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
# This expression doesn't catch *all* class definition headers,
|
||||||
|
# but it's pretty darn close.
|
||||||
|
classexpr = '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'
|
||||||
|
classprog = regex.compile(classexpr)
|
||||||
|
|
||||||
|
# Expressions for finding base class expressions.
|
||||||
|
baseexpr = '^ *\(.*\) *( *) *$'
|
||||||
|
baseprog = regex.compile(baseexpr)
|
||||||
|
|
||||||
|
import string
|
||||||
|
|
||||||
|
def fixline(line):
|
||||||
|
if classprog.match(line) < 0: # No 'class' keyword -- no change
|
||||||
|
return line
|
||||||
|
|
||||||
|
(a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]
|
||||||
|
# a0, b0 = Whole match (up to ':')
|
||||||
|
# a1, b1 = First subexpression (up to classname)
|
||||||
|
# a2, b2 = Second subexpression (=.*)
|
||||||
|
head = line[:b1]
|
||||||
|
tail = line[b0:] # Unmatched rest of line
|
||||||
|
|
||||||
|
if a2 == b2: # No base classes -- easy case
|
||||||
|
return head + ':' + tail
|
||||||
|
|
||||||
|
# Get rid of leading '='
|
||||||
|
basepart = line[a2+1:b2]
|
||||||
|
|
||||||
|
# Extract list of base expressions
|
||||||
|
bases = string.splitfields(basepart, ',')
|
||||||
|
|
||||||
|
# Strip trailing '()' from each base expression
|
||||||
|
for i in range(len(bases)):
|
||||||
|
if baseprog.match(bases[i]) >= 0:
|
||||||
|
x1, y1 = baseprog.regs[1]
|
||||||
|
bases[i] = bases[i][x1:y1]
|
||||||
|
|
||||||
|
# Join the bases back again and build the new line
|
||||||
|
basepart = string.joinfields(bases, ', ')
|
||||||
|
|
||||||
|
return head + '(' + basepart + '):' + tail
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue