blob: f5337756f9299122b49645ee232dcc1a5474a4c2 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossuma7925f11994-01-26 10:20:16 +00002
3import regex
4import regsub
5import glob
6import sys
7import os
8import stat
9import getopt
10
Guido van Rossum227a0a11995-01-04 19:21:21 +000011oldcprt = """\
12Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
13Amsterdam, The Netherlands."""
14newcprt = """\
15Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
16The Netherlands."""
Guido van Rossuma7925f11994-01-26 10:20:16 +000017
18oldprog = regex.compile(oldcprt)
19newprog = regex.compile(newcprt)
20
Guido van Rossum227a0a11995-01-04 19:21:21 +000021HEADSIZE = 1024
22
Guido van Rossuma7925f11994-01-26 10:20:16 +000023def 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 Rossum227a0a11995-01-04 19:21:21 +000045 head = f.read(HEADSIZE)
Guido van Rossuma7925f11994-01-26 10:20:16 +000046 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
75main()