home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Misc / Fixcprt.py < prev    next >
Text File  |  1994-01-01  |  2KB  |  70 lines

  1. #! /usr/local/bin/python
  2.  
  3. import regex
  4. import regsub
  5. import glob
  6. import sys
  7. import os
  8. import stat
  9. import getopt
  10.  
  11. oldcprt = 'Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,\nAmsterdam, The Netherlands.'
  12. newcprt = 'Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,\nAmsterdam, The Netherlands.'
  13.  
  14. oldprog = regex.compile(oldcprt)
  15. newprog = regex.compile(newcprt)
  16.  
  17. def main():
  18.     opts, args = getopt.getopt(sys.argv[1:], 'y:')
  19.     agelimit = 0L
  20.     for opt, arg in opts:
  21.         if opt == '-y':
  22.             agelimit = os.stat(arg)[stat.ST_MTIME]
  23.     if not args:
  24.         args = glob.glob('*.[ch]')
  25.     for file in args:
  26.         try:
  27.             age = os.stat(file)[stat.ST_MTIME]
  28.         except os.error, msg:
  29.             print file, ': stat failed :', msg
  30.             continue
  31.         if age <= agelimit:
  32.             print file, ': too old, skipped'
  33.             continue
  34.         try:
  35.             f = open(file, 'r')
  36.         except IOError, msg:
  37.             print file, ': open failed :', msg
  38.             continue
  39.         head = f.read(1024)
  40.         if oldprog.search(head) < 0:
  41.             if newprog.search(head) < 0:
  42.                 print file, ': NO COPYRIGHT FOUND'
  43.             else:
  44.                 print file, ': (new copyright already there)'
  45.             f.close()
  46.             continue
  47.         newhead = regsub.sub(oldcprt, newcprt, head)
  48.         data = newhead + f.read()
  49.         f.close()
  50.         try:
  51.             f = open(file + '.new', 'w')
  52.         except IOError, msg:
  53.             print file, ': creat failed :', msg
  54.             continue
  55.         f.write(data)
  56.         f.close()
  57.         try:
  58.             os.rename(file, file + '~')
  59.         except IOError, msg:
  60.             print file, ': rename to backup failed :', msg
  61.             continue
  62.         try:
  63.             os.rename(file + '.new', file)
  64.         except IOError, msg:
  65.             print file, ': rename from .new failed :', msg
  66.             continue
  67.         print file, ': copyright changed.'
  68.  
  69. main()
  70.