home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / !Python / Misc / Fixcprt_py < prev    next >
Encoding:
Text File  |  1995-01-04  |  1.6 KB  |  76 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 = """\
  12. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  13. Amsterdam, The Netherlands."""
  14. newcprt = """\
  15. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  16. The Netherlands."""
  17.  
  18. oldprog = regex.compile(oldcprt)
  19. newprog = regex.compile(newcprt)
  20.  
  21. HEADSIZE = 1024
  22.  
  23. def main():
  24.     opts, args = getopt.getopt(sys.argv[1:], 'y:')
  25.     agelimit = 0L
  26.     for opt, arg in opts:
  27.         if opt == '-y':
  28.             agelimit = os.stat(arg)[stat.ST_MTIME]
  29.     if not args:
  30.         args = glob.glob('*.[ch]')
  31.     for file in args:
  32.         try:
  33.             age = os.stat(file)[stat.ST_MTIME]
  34.         except os.error, msg:
  35.             print file, ': stat failed :', msg
  36.             continue
  37.         if age <= agelimit:
  38.             print file, ': too old, skipped'
  39.             continue
  40.         try:
  41.             f = open(file, 'r')
  42.         except IOError, msg:
  43.             print file, ': open failed :', msg
  44.             continue
  45.         head = f.read(HEADSIZE)
  46.         if oldprog.search(head) < 0:
  47.             if newprog.search(head) < 0:
  48.                 print file, ': NO COPYRIGHT FOUND'
  49.             else:
  50.                 print file, ': (new copyright already there)'
  51.             f.close()
  52.             continue
  53.         newhead = regsub.sub(oldcprt, newcprt, head)
  54.         data = newhead + f.read()
  55.         f.close()
  56.         try:
  57.             f = open(file + '.new', 'w')
  58.         except IOError, msg:
  59.             print file, ': creat failed :', msg
  60.             continue
  61.         f.write(data)
  62.         f.close()
  63.         try:
  64.             os.rename(file, file + '~')
  65.         except IOError, msg:
  66.             print file, ': rename to backup failed :', msg
  67.             continue
  68.         try:
  69.             os.rename(file + '.new', file)
  70.         except IOError, msg:
  71.             print file, ': rename from .new failed :', msg
  72.             continue
  73.         print file, ': copyright changed.'
  74.  
  75. main()
  76.