annotate Tools/scripts/pathfix.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 4727f260f6f8
children 33b156e118c0 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4568
4341574e163b Changed logic so it now replaces anything that has #! and python in
Guido van Rossum <guido@python.org>
parents: 1799
diff changeset
1 #! /usr/bin/env python
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Change the #! line occurring in Python scripts. The new interpreter
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # pathname must be given with a -i option.
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # Command line arguments are files or directories to be processed.
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # Directories are searched recursively for files whose name looks
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # like a python module.
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # Symbolic links are always ignored (except as explicit directory
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # arguments). Of course, the original file is kept as a back-up
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # (with a "~" attached to its name).
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # Undoubtedly you can do this using find and sed or perl, but this is
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # a nice example of Python code that recurses down a directory tree
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # and uses regular expressions. Also note several subtleties like
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # preserving the file's mode and avoiding to even write a temp file
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # when no changes are needed for a file.
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 #
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # NB: by changing only the function fixfile() you can turn this
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # into a program for a different change to Python programs...
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 import sys
37021
4727f260f6f8 Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents: 33076
diff changeset
23 import re
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 import os
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 from stat import *
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 import getopt
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 err = sys.stderr.write
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 dbg = err
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 rep = sys.stdout.write
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 new_interpreter = None
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def main():
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
35 global new_interpreter
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
36 usage = ('usage: %s -i /interpreter file-or-directory ...\n' %
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
37 sys.argv[0])
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
38 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
39 opts, args = getopt.getopt(sys.argv[1:], 'i:')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
40 except getopt.error, msg:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
41 err(msg + '\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
42 err(usage)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
43 sys.exit(2)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
44 for o, a in opts:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
45 if o == '-i':
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
46 new_interpreter = a
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
47 if not new_interpreter or new_interpreter[0] != '/' or not args:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
48 err('-i option or file-or-directory missing\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
49 err(usage)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
50 sys.exit(2)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
51 bad = 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
52 for arg in args:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
53 if os.path.isdir(arg):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
54 if recursedown(arg): bad = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
55 elif os.path.islink(arg):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
56 err(arg + ': will not process symbolic links\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
57 bad = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
58 else:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
59 if fix(arg): bad = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
60 sys.exit(bad)
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
37021
4727f260f6f8 Remove regsub, reconvert, regex, regex_syntax and everything under lib-old.
Neal Norwitz <nnorwitz@gmail.com>
parents: 33076
diff changeset
62 ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$')
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 def ispython(name):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
64 return ispythonprog.match(name) >= 0
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def recursedown(dirname):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
67 dbg('recursedown(%r)\n' % (dirname,))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
68 bad = 0
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
69 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
70 names = os.listdir(dirname)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
71 except os.error, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
72 err('%s: cannot list directory: %r\n' % (dirname, msg))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
73 return 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
74 names.sort()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
75 subdirs = []
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
76 for name in names:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
77 if name in (os.curdir, os.pardir): continue
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
78 fullname = os.path.join(dirname, name)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
79 if os.path.islink(fullname): pass
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
80 elif os.path.isdir(fullname):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
81 subdirs.append(fullname)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
82 elif ispython(name):
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
83 if fix(fullname): bad = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
84 for fullname in subdirs:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
85 if recursedown(fullname): bad = 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
86 return bad
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def fix(filename):
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
89 ## dbg('fix(%r)\n' % (filename,))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
90 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
91 f = open(filename, 'r')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
92 except IOError, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
93 err('%s: cannot open: %r\n' % (filename, msg))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
94 return 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
95 line = f.readline()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
96 fixed = fixline(line)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
97 if line == fixed:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
98 rep(filename+': no change\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
99 f.close()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
100 return
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
101 head, tail = os.path.split(filename)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
102 tempname = os.path.join(head, '@' + tail)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
103 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
104 g = open(tempname, 'w')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
105 except IOError, msg:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
106 f.close()
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
107 err('%s: cannot create: %r\n' % (tempname, msg))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
108 return 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
109 rep(filename + ': updating\n')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
110 g.write(fixed)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
111 BUFSIZE = 8*1024
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
112 while 1:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
113 buf = f.read(BUFSIZE)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
114 if not buf: break
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
115 g.write(buf)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
116 g.close()
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
117 f.close()
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
119 # Finishing touch -- move files
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
121 # First copy the file's mode to the temp file
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
122 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
123 statbuf = os.stat(filename)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
124 os.chmod(tempname, statbuf[ST_MODE] & 07777)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
125 except os.error, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
126 err('%s: warning: chmod failed (%r)\n' % (tempname, msg))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
127 # Then make a backup of the original file as filename~
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
128 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
129 os.rename(filename, filename + '~')
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
130 except os.error, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
131 err('%s: warning: backup failed (%r)\n' % (filename, msg))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
132 # Now move the temp file to the original file
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
133 try:
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
134 os.rename(tempname, filename)
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
135 except os.error, msg:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 25219
diff changeset
136 err('%s: rename failed (%r)\n' % (filename, msg))
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
137 return 1
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
138 # Return succes
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
139 return 0
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 def fixline(line):
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15932
diff changeset
142 if not line.startswith('#!'):
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
143 return line
25219
8a7a2f1f88f0 Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents: 15932
diff changeset
144 if "python" not in line:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
145 return line
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4568
diff changeset
146 return '#! %s\n' % new_interpreter
1799
60660341ee10 newslist.py: Added search for .newslistrc.py;
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
148 if __name__ == '__main__':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
149 main()