Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2 | |
| 3 | import regex |
| 4 | import regsub |
| 5 | import glob |
| 6 | import sys |
| 7 | import os |
| 8 | import stat |
| 9 | import getopt |
| 10 | |
Guido van Rossum | 227a0a1 | 1995-01-04 19:21:21 +0000 | [diff] [blame] | 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.""" |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 17 | |
| 18 | oldprog = regex.compile(oldcprt) |
| 19 | newprog = regex.compile(newcprt) |
| 20 | |
Guido van Rossum | 227a0a1 | 1995-01-04 19:21:21 +0000 | [diff] [blame] | 21 | HEADSIZE = 1024 |
| 22 | |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 227a0a1 | 1995-01-04 19:21:21 +0000 | [diff] [blame] | 45 | head = f.read(HEADSIZE) |
Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 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() |