1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """(Ostensibly) fix copyright notices in files. |
---|
4 | n/a | |
---|
5 | n/a | Actually, this script will simply replace a block of text in a file from one |
---|
6 | n/a | string to another. It will only do this once though, i.e. not globally |
---|
7 | n/a | throughout the file. It writes a backup file and then does an os.rename() |
---|
8 | n/a | dance for atomicity. |
---|
9 | n/a | |
---|
10 | n/a | Usage: fixnotices.py [options] [filenames] |
---|
11 | n/a | Options: |
---|
12 | n/a | -h / --help |
---|
13 | n/a | Print this message and exit |
---|
14 | n/a | |
---|
15 | n/a | --oldnotice=file |
---|
16 | n/a | Use the notice in the file as the old (to be replaced) string, instead |
---|
17 | n/a | of the hard coded value in the script. |
---|
18 | n/a | |
---|
19 | n/a | --newnotice=file |
---|
20 | n/a | Use the notice in the file as the new (replacement) string, instead of |
---|
21 | n/a | the hard coded value in the script. |
---|
22 | n/a | |
---|
23 | n/a | --dry-run |
---|
24 | n/a | Don't actually make the changes, but print out the list of files that |
---|
25 | n/a | would change. When used with -v, a status will be printed for every |
---|
26 | n/a | file. |
---|
27 | n/a | |
---|
28 | n/a | -v / --verbose |
---|
29 | n/a | Print a message for every file looked at, indicating whether the file |
---|
30 | n/a | is changed or not. |
---|
31 | n/a | """ |
---|
32 | n/a | |
---|
33 | n/a | OLD_NOTICE = """/*********************************************************** |
---|
34 | n/a | Copyright (c) 2000, BeOpen.com. |
---|
35 | n/a | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
---|
36 | n/a | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
---|
37 | n/a | All rights reserved. |
---|
38 | n/a | |
---|
39 | n/a | See the file "Misc/COPYRIGHT" for information on usage and |
---|
40 | n/a | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
41 | n/a | ******************************************************************/ |
---|
42 | n/a | """ |
---|
43 | n/a | import os |
---|
44 | n/a | import sys |
---|
45 | n/a | import getopt |
---|
46 | n/a | |
---|
47 | n/a | NEW_NOTICE = "" |
---|
48 | n/a | DRYRUN = 0 |
---|
49 | n/a | VERBOSE = 0 |
---|
50 | n/a | |
---|
51 | n/a | |
---|
52 | n/a | def usage(code, msg=''): |
---|
53 | n/a | print(__doc__ % globals()) |
---|
54 | n/a | if msg: |
---|
55 | n/a | print(msg) |
---|
56 | n/a | sys.exit(code) |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | def main(): |
---|
60 | n/a | global DRYRUN, OLD_NOTICE, NEW_NOTICE, VERBOSE |
---|
61 | n/a | try: |
---|
62 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'hv', |
---|
63 | n/a | ['help', 'oldnotice=', 'newnotice=', |
---|
64 | n/a | 'dry-run', 'verbose']) |
---|
65 | n/a | except getopt.error as msg: |
---|
66 | n/a | usage(1, msg) |
---|
67 | n/a | |
---|
68 | n/a | for opt, arg in opts: |
---|
69 | n/a | if opt in ('-h', '--help'): |
---|
70 | n/a | usage(0) |
---|
71 | n/a | elif opt in ('-v', '--verbose'): |
---|
72 | n/a | VERBOSE = 1 |
---|
73 | n/a | elif opt == '--dry-run': |
---|
74 | n/a | DRYRUN = 1 |
---|
75 | n/a | elif opt == '--oldnotice': |
---|
76 | n/a | fp = open(arg) |
---|
77 | n/a | OLD_NOTICE = fp.read() |
---|
78 | n/a | fp.close() |
---|
79 | n/a | elif opt == '--newnotice': |
---|
80 | n/a | fp = open(arg) |
---|
81 | n/a | NEW_NOTICE = fp.read() |
---|
82 | n/a | fp.close() |
---|
83 | n/a | |
---|
84 | n/a | for arg in args: |
---|
85 | n/a | process(arg) |
---|
86 | n/a | |
---|
87 | n/a | |
---|
88 | n/a | def process(file): |
---|
89 | n/a | f = open(file) |
---|
90 | n/a | data = f.read() |
---|
91 | n/a | f.close() |
---|
92 | n/a | i = data.find(OLD_NOTICE) |
---|
93 | n/a | if i < 0: |
---|
94 | n/a | if VERBOSE: |
---|
95 | n/a | print('no change:', file) |
---|
96 | n/a | return |
---|
97 | n/a | elif DRYRUN or VERBOSE: |
---|
98 | n/a | print(' change:', file) |
---|
99 | n/a | if DRYRUN: |
---|
100 | n/a | # Don't actually change the file |
---|
101 | n/a | return |
---|
102 | n/a | data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):] |
---|
103 | n/a | new = file + ".new" |
---|
104 | n/a | backup = file + ".bak" |
---|
105 | n/a | f = open(new, "w") |
---|
106 | n/a | f.write(data) |
---|
107 | n/a | f.close() |
---|
108 | n/a | os.rename(file, backup) |
---|
109 | n/a | os.rename(new, file) |
---|
110 | n/a | |
---|
111 | n/a | |
---|
112 | n/a | if __name__ == '__main__': |
---|
113 | n/a | main() |
---|