/Tools/scripts/classfix.py

http://unladen-swallow.googlecode.com/ · Python · 190 lines · 121 code · 21 blank · 48 comment · 40 complexity · 4d56d113d3663bbdd8d99f21e4168e13 MD5 · raw file

  1. #! /usr/bin/env python
  2. # This script is obsolete -- it is kept for historical purposes only.
  3. #
  4. # Fix Python source files to use the new class definition syntax, i.e.,
  5. # the syntax used in Python versions before 0.9.8:
  6. # class C() = base(), base(), ...: ...
  7. # is changed to the current syntax:
  8. # class C(base, base, ...): ...
  9. #
  10. # The script uses heuristics to find class definitions that usually
  11. # work but occasionally can fail; carefully check the output!
  12. #
  13. # Command line arguments are files or directories to be processed.
  14. # Directories are searched recursively for files whose name looks
  15. # like a python module.
  16. # Symbolic links are always ignored (except as explicit directory
  17. # arguments). Of course, the original file is kept as a back-up
  18. # (with a "~" attached to its name).
  19. #
  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. import sys
  31. import re
  32. import os
  33. from stat import *
  34. err = sys.stderr.write
  35. dbg = err
  36. rep = sys.stdout.write
  37. def main():
  38. bad = 0
  39. if not sys.argv[1:]: # No arguments
  40. err('usage: ' + sys.argv[0] + ' file-or-directory ...\n')
  41. sys.exit(2)
  42. for arg in sys.argv[1:]:
  43. if os.path.isdir(arg):
  44. if recursedown(arg): bad = 1
  45. elif os.path.islink(arg):
  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. ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$')
  52. def ispython(name):
  53. return ispythonprog.match(name) >= 0
  54. def recursedown(dirname):
  55. dbg('recursedown(%r)\n' % (dirname,))
  56. bad = 0
  57. try:
  58. names = os.listdir(dirname)
  59. except os.error, msg:
  60. err('%s: cannot list directory: %r\n' % (dirname, msg))
  61. return 1
  62. names.sort()
  63. subdirs = []
  64. for name in names:
  65. if name in (os.curdir, os.pardir): continue
  66. fullname = os.path.join(dirname, name)
  67. if os.path.islink(fullname): pass
  68. elif os.path.isdir(fullname):
  69. subdirs.append(fullname)
  70. elif ispython(name):
  71. if fix(fullname): bad = 1
  72. for fullname in subdirs:
  73. if recursedown(fullname): bad = 1
  74. return bad
  75. def fix(filename):
  76. ## dbg('fix(%r)\n' % (filename,))
  77. try:
  78. f = open(filename, 'r')
  79. except IOError, msg:
  80. err('%s: cannot open: %r\n' % (filename, msg))
  81. return 1
  82. head, tail = os.path.split(filename)
  83. tempname = os.path.join(head, '@' + tail)
  84. g = None
  85. # If we find a match, we rewind the file and start over but
  86. # now copy everything to a temp file.
  87. lineno = 0
  88. while 1:
  89. line = f.readline()
  90. if not line: break
  91. lineno = lineno + 1
  92. while line[-2:] == '\\\n':
  93. nextline = f.readline()
  94. if not nextline: break
  95. line = line + nextline
  96. lineno = lineno + 1
  97. newline = fixline(line)
  98. if newline != line:
  99. if g is None:
  100. try:
  101. g = open(tempname, 'w')
  102. except IOError, msg:
  103. f.close()
  104. err('%s: cannot create: %r\n' % (tempname, msg))
  105. return 1
  106. f.seek(0)
  107. lineno = 0
  108. rep(filename + ':\n')
  109. continue # restart from the beginning
  110. rep(repr(lineno) + '\n')
  111. rep('< ' + line)
  112. rep('> ' + newline)
  113. if g is not None:
  114. g.write(newline)
  115. # End of file
  116. f.close()
  117. if not g: return 0 # No changes
  118. # Finishing touch -- move files
  119. # First copy the file's mode to the temp file
  120. try:
  121. statbuf = os.stat(filename)
  122. os.chmod(tempname, statbuf[ST_MODE] & 07777)
  123. except os.error, msg:
  124. err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
  125. # Then make a backup of the original file as filename~
  126. try:
  127. os.rename(filename, filename + '~')
  128. except os.error, msg:
  129. err('%s: warning: backup failed (%r)\n' % (filename, msg))
  130. # Now move the temp file to the original file
  131. try:
  132. os.rename(tempname, filename)
  133. except os.error, msg:
  134. err('%s: rename failed (%r)\n' % (filename, msg))
  135. return 1
  136. # Return succes
  137. return 0
  138. # This expression doesn't catch *all* class definition headers,
  139. # but it's pretty darn close.
  140. classexpr = '^([ \t]*class +[a-zA-Z0-9_]+) *( *) *((=.*)?):'
  141. classprog = re.compile(classexpr)
  142. # Expressions for finding base class expressions.
  143. baseexpr = '^ *(.*) *( *) *$'
  144. baseprog = re.compile(baseexpr)
  145. def fixline(line):
  146. if classprog.match(line) < 0: # No 'class' keyword -- no change
  147. return line
  148. (a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]
  149. # a0, b0 = Whole match (up to ':')
  150. # a1, b1 = First subexpression (up to classname)
  151. # a2, b2 = Second subexpression (=.*)
  152. head = line[:b1]
  153. tail = line[b0:] # Unmatched rest of line
  154. if a2 == b2: # No base classes -- easy case
  155. return head + ':' + tail
  156. # Get rid of leading '='
  157. basepart = line[a2+1:b2]
  158. # Extract list of base expressions
  159. bases = basepart.split(',')
  160. # Strip trailing '()' from each base expression
  161. for i in range(len(bases)):
  162. if baseprog.match(bases[i]) >= 0:
  163. x1, y1 = baseprog.regs[1]
  164. bases[i] = bases[i][x1:y1]
  165. # Join the bases back again and build the new line
  166. basepart = ', '.join(bases)
  167. return head + '(' + basepart + '):' + tail
  168. if __name__ == '__main__':
  169. main()