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