Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 3 | """(Ostensibly) fix copyright notices in files. |
| 4 | |
| 5 | Actually, this sript will simply replace a block of text in a file from one |
| 6 | string to another. It will only do this once though, i.e. not globally |
| 7 | throughout the file. It writes a backup file and then does an os.rename() |
| 8 | dance for atomicity. |
| 9 | |
| 10 | Usage: fixnotices.py [options] [filenames] |
| 11 | Options: |
| 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 Rossum | 8586991 | 2000-09-01 23:29:29 +0000 | [diff] [blame] | 33 | OLD_NOTICE = """/*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 34 | Copyright (c) 2000, BeOpen.com. |
| 35 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 36 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 37 | All rights reserved. |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 39 | See the file "Misc/COPYRIGHT" for information on usage and |
| 40 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 8586991 | 2000-09-01 23:29:29 +0000 | [diff] [blame] | 41 | ******************************************************************/ |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 42 | """ |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 43 | import os |
| 44 | import sys |
| 45 | import getopt |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 8586991 | 2000-09-01 23:29:29 +0000 | [diff] [blame] | 47 | NEW_NOTICE = "" |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 48 | DRYRUN = 0 |
| 49 | VERBOSE = 0 |
Guido van Rossum | 8586991 | 2000-09-01 23:29:29 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 51 | |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 52 | def usage(code, msg=''): |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 53 | print(__doc__ % globals()) |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 54 | if msg: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 55 | print(msg) |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 56 | sys.exit(code) |
| 57 | |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 58 | |
| 59 | def main(): |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 60 | 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 Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 65 | except getopt.error as msg: |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 66 | 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 Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 84 | for arg in args: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 85 | process(arg) |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 87 | |
| 88 | def process(file): |
| 89 | f = open(file) |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 90 | data = f.read() |
| 91 | f.close() |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 92 | i = data.find(OLD_NOTICE) |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 93 | if i < 0: |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 94 | if VERBOSE: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 95 | print('no change:', file) |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 96 | return |
| 97 | elif DRYRUN or VERBOSE: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 98 | print(' change:', file) |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 99 | if DRYRUN: |
| 100 | # Don't actually change the file |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 101 | return |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 102 | data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):] |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 103 | new = file + ".new" |
| 104 | backup = file + ".bak" |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 105 | f = open(new, "w") |
| 106 | f.write(data) |
| 107 | f.close() |
Barry Warsaw | 56ab921 | 2002-02-08 23:25:46 +0000 | [diff] [blame] | 108 | os.rename(file, backup) |
| 109 | os.rename(new, file) |
| 110 | |
Guido van Rossum | fc05882 | 1996-11-27 19:41:55 +0000 | [diff] [blame] | 111 | |
| 112 | if __name__ == '__main__': |
| 113 | main() |