home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / CLASSFIX.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  5.4 KB  |  193 lines

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