blob: ad967f94781c2e32421e5a009382221e12c7488d [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossumfc058821996-11-27 19:41:55 +00002
Barry Warsaw56ab9212002-02-08 23:25:46 +00003"""(Ostensibly) fix copyright notices in files.
4
Ezio Melotti7c4a7e62013-08-26 01:32:56 +03005Actually, this script will simply replace a block of text in a file from one
Barry Warsaw56ab9212002-02-08 23:25:46 +00006string to another. It will only do this once though, i.e. not globally
7throughout the file. It writes a backup file and then does an os.rename()
8dance for atomicity.
9
10Usage: fixnotices.py [options] [filenames]
11Options:
12 -h / --help
13 Print this message and exit
14
15 --oldnotice=file
16 Use the notice in the file as the old (to be replaced) string, instead
17 of the hard coded value in the script.
18
19 --newnotice=file
20 Use the notice in the file as the new (replacement) string, instead of
21 the hard coded value in the script.
22
23 --dry-run
24 Don't actually make the changes, but print out the list of files that
25 would change. When used with -v, a status will be printed for every
26 file.
27
28 -v / --verbose
29 Print a message for every file looked at, indicating whether the file
30 is changed or not.
31"""
32
Guido van Rossum85869912000-09-01 23:29:29 +000033OLD_NOTICE = """/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000034Copyright (c) 2000, BeOpen.com.
35Copyright (c) 1995-2000, Corporation for National Research Initiatives.
36Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
37All rights reserved.
Guido van Rossumfc058821996-11-27 19:41:55 +000038
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000039See the file "Misc/COPYRIGHT" for information on usage and
40redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum85869912000-09-01 23:29:29 +000041******************************************************************/
Guido van Rossumfc058821996-11-27 19:41:55 +000042"""
Barry Warsaw56ab9212002-02-08 23:25:46 +000043import os
44import sys
45import getopt
Guido van Rossumfc058821996-11-27 19:41:55 +000046
Guido van Rossum85869912000-09-01 23:29:29 +000047NEW_NOTICE = ""
Barry Warsaw56ab9212002-02-08 23:25:46 +000048DRYRUN = 0
49VERBOSE = 0
Guido van Rossum85869912000-09-01 23:29:29 +000050
Guido van Rossumfc058821996-11-27 19:41:55 +000051
Barry Warsaw56ab9212002-02-08 23:25:46 +000052def usage(code, msg=''):
Collin Winter6afaeb72007-08-03 17:06:41 +000053 print(__doc__ % globals())
Barry Warsaw56ab9212002-02-08 23:25:46 +000054 if msg:
Collin Winter6afaeb72007-08-03 17:06:41 +000055 print(msg)
Barry Warsaw56ab9212002-02-08 23:25:46 +000056 sys.exit(code)
57
Guido van Rossumfc058821996-11-27 19:41:55 +000058
59def main():
Barry Warsaw56ab9212002-02-08 23:25:46 +000060 global DRYRUN, OLD_NOTICE, NEW_NOTICE, VERBOSE
61 try:
62 opts, args = getopt.getopt(sys.argv[1:], 'hv',
63 ['help', 'oldnotice=', 'newnotice=',
64 'dry-run', 'verbose'])
Guido van Rossumb940e112007-01-10 16:19:56 +000065 except getopt.error as msg:
Barry Warsaw56ab9212002-02-08 23:25:46 +000066 usage(1, msg)
67
68 for opt, arg in opts:
69 if opt in ('-h', '--help'):
70 usage(0)
71 elif opt in ('-v', '--verbose'):
72 VERBOSE = 1
73 elif opt == '--dry-run':
74 DRYRUN = 1
75 elif opt == '--oldnotice':
76 fp = open(arg)
77 OLD_NOTICE = fp.read()
78 fp.close()
79 elif opt == '--newnotice':
80 fp = open(arg)
81 NEW_NOTICE = fp.read()
82 fp.close()
83
Guido van Rossumfc058821996-11-27 19:41:55 +000084 for arg in args:
Guido van Rossumed5b3d81998-03-24 05:30:29 +000085 process(arg)
Guido van Rossumfc058821996-11-27 19:41:55 +000086
Barry Warsaw56ab9212002-02-08 23:25:46 +000087
88def process(file):
89 f = open(file)
Guido van Rossumfc058821996-11-27 19:41:55 +000090 data = f.read()
91 f.close()
Barry Warsaw56ab9212002-02-08 23:25:46 +000092 i = data.find(OLD_NOTICE)
Guido van Rossumfc058821996-11-27 19:41:55 +000093 if i < 0:
Barry Warsaw56ab9212002-02-08 23:25:46 +000094 if VERBOSE:
Collin Winter6afaeb72007-08-03 17:06:41 +000095 print('no change:', file)
Barry Warsaw56ab9212002-02-08 23:25:46 +000096 return
97 elif DRYRUN or VERBOSE:
Collin Winter6afaeb72007-08-03 17:06:41 +000098 print(' change:', file)
Barry Warsaw56ab9212002-02-08 23:25:46 +000099 if DRYRUN:
100 # Don't actually change the file
Guido van Rossumed5b3d81998-03-24 05:30:29 +0000101 return
Guido van Rossumfc058821996-11-27 19:41:55 +0000102 data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
Barry Warsaw56ab9212002-02-08 23:25:46 +0000103 new = file + ".new"
104 backup = file + ".bak"
Guido van Rossumfc058821996-11-27 19:41:55 +0000105 f = open(new, "w")
106 f.write(data)
107 f.close()
Barry Warsaw56ab9212002-02-08 23:25:46 +0000108 os.rename(file, backup)
109 os.rename(new, file)
110
Guido van Rossumfc058821996-11-27 19:41:55 +0000111
112if __name__ == '__main__':
113 main()