Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/fixnotice.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 4c1ccf36025c |
| children | 33b156e118c0 6b18a095c2e7 |
| rev | line source |
|---|---|
| 4567 | 1 #! /usr/bin/env python |
| 2 | |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
3 """(Ostensibly) fix copyright notices in files. |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
4 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
5 Actually, this sript will simply replace a block of text in a file from one |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
6 string to another. It will only do this once though, i.e. not globally |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
7 throughout the file. It writes a backup file and then does an os.rename() |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
8 dance for atomicity. |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
9 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
10 Usage: fixnotices.py [options] [filenames] |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
11 Options: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
12 -h / --help |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
13 Print this message and exit |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
14 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
15 --oldnotice=file |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
16 Use the notice in the file as the old (to be replaced) string, instead |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
17 of the hard coded value in the script. |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
18 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
19 --newnotice=file |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
20 Use the notice in the file as the new (replacement) string, instead of |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
21 the hard coded value in the script. |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
22 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
23 --dry-run |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
24 Don't actually make the changes, but print out the list of files that |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
25 would change. When used with -v, a status will be printed for every |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
26 file. |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
27 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
28 -v / --verbose |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
29 Print a message for every file looked at, indicating whether the file |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
30 is changed or not. |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
31 """ |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
32 |
|
14375
6f6f965c5e2d
REMOVED all CWI, CNRI and BeOpen copyright markings.
Guido van Rossum <guido@python.org>
parents:
13255
diff
changeset
|
33 OLD_NOTICE = """/*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
7735
diff
changeset
|
34 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
7735
diff
changeset
|
35 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
7735
diff
changeset
|
36 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
7735
diff
changeset
|
37 All rights reserved. |
| 4567 | 38 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
7735
diff
changeset
|
39 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
7735
diff
changeset
|
40 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|
14375
6f6f965c5e2d
REMOVED all CWI, CNRI and BeOpen copyright markings.
Guido van Rossum <guido@python.org>
parents:
13255
diff
changeset
|
41 ******************************************************************/ |
| 4567 | 42 """ |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
43 import os |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
44 import sys |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
45 import getopt |
| 4567 | 46 |
|
14375
6f6f965c5e2d
REMOVED all CWI, CNRI and BeOpen copyright markings.
Guido van Rossum <guido@python.org>
parents:
13255
diff
changeset
|
47 NEW_NOTICE = "" |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
48 DRYRUN = 0 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
49 VERBOSE = 0 |
|
14375
6f6f965c5e2d
REMOVED all CWI, CNRI and BeOpen copyright markings.
Guido van Rossum <guido@python.org>
parents:
13255
diff
changeset
|
50 |
| 4567 | 51 |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
52 def usage(code, msg=''): |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
53 print __doc__ % globals() |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
54 if msg: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
55 print msg |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
56 sys.exit(code) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
57 |
| 4567 | 58 |
| 59 def main(): | |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
60 global DRYRUN, OLD_NOTICE, NEW_NOTICE, VERBOSE |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
61 try: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
62 opts, args = getopt.getopt(sys.argv[1:], 'hv', |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
63 ['help', 'oldnotice=', 'newnotice=', |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
64 'dry-run', 'verbose']) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
65 except getopt.error, msg: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
66 usage(1, msg) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
67 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
68 for opt, arg in opts: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
69 if opt in ('-h', '--help'): |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
70 usage(0) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
71 elif opt in ('-v', '--verbose'): |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
72 VERBOSE = 1 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
73 elif opt == '--dry-run': |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
74 DRYRUN = 1 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
75 elif opt == '--oldnotice': |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
76 fp = open(arg) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
77 OLD_NOTICE = fp.read() |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
78 fp.close() |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
79 elif opt == '--newnotice': |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
80 fp = open(arg) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
81 NEW_NOTICE = fp.read() |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
82 fp.close() |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
83 |
| 4567 | 84 for arg in args: |
| 7735 | 85 process(arg) |
| 4567 | 86 |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
87 |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
88 def process(file): |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
89 f = open(file) |
| 4567 | 90 data = f.read() |
| 91 f.close() | |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
92 i = data.find(OLD_NOTICE) |
| 4567 | 93 if i < 0: |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
94 if VERBOSE: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
95 print 'no change:', file |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
96 return |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
97 elif DRYRUN or VERBOSE: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
98 print ' change:', file |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
99 if DRYRUN: |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
100 # Don't actually change the file |
| 7735 | 101 return |
| 4567 | 102 data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):] |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
103 new = file + ".new" |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
104 backup = file + ".bak" |
| 4567 | 105 f = open(new, "w") |
| 106 f.write(data) | |
| 107 f.close() | |
|
21901
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
108 os.rename(file, backup) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
109 os.rename(new, file) |
|
4c1ccf36025c
Added a module docstring, which doubles as the --help text.
Barry Warsaw <barry@python.org>
parents:
15932
diff
changeset
|
110 |
| 4567 | 111 |
| 112 if __name__ == '__main__': | |
| 113 main() |
