1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Change the #! line occurring in Python scripts. The new interpreter |
---|
4 | n/a | # pathname must be given with a -i option. |
---|
5 | n/a | # |
---|
6 | n/a | # Command line arguments are files or directories to be processed. |
---|
7 | n/a | # Directories are searched recursively for files whose name looks |
---|
8 | n/a | # like a python module. |
---|
9 | n/a | # Symbolic links are always ignored (except as explicit directory |
---|
10 | n/a | # arguments). Of course, the original file is kept as a back-up |
---|
11 | n/a | # (with a "~" attached to its name). |
---|
12 | n/a | # |
---|
13 | n/a | # Undoubtedly you can do this using find and sed or perl, but this is |
---|
14 | n/a | # a nice example of Python code that recurses down a directory tree |
---|
15 | n/a | # and uses regular expressions. Also note several subtleties like |
---|
16 | n/a | # preserving the file's mode and avoiding to even write a temp file |
---|
17 | n/a | # when no changes are needed for a file. |
---|
18 | n/a | # |
---|
19 | n/a | # NB: by changing only the function fixfile() you can turn this |
---|
20 | n/a | # into a program for a different change to Python programs... |
---|
21 | n/a | |
---|
22 | n/a | import sys |
---|
23 | n/a | import re |
---|
24 | n/a | import os |
---|
25 | n/a | from stat import * |
---|
26 | n/a | import getopt |
---|
27 | n/a | |
---|
28 | n/a | err = sys.stderr.write |
---|
29 | n/a | dbg = err |
---|
30 | n/a | rep = sys.stdout.write |
---|
31 | n/a | |
---|
32 | n/a | new_interpreter = None |
---|
33 | n/a | preserve_timestamps = False |
---|
34 | n/a | |
---|
35 | n/a | def main(): |
---|
36 | n/a | global new_interpreter |
---|
37 | n/a | global preserve_timestamps |
---|
38 | n/a | usage = ('usage: %s -i /interpreter -p file-or-directory ...\n' % |
---|
39 | n/a | sys.argv[0]) |
---|
40 | n/a | try: |
---|
41 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'i:p') |
---|
42 | n/a | except getopt.error as msg: |
---|
43 | n/a | err(str(msg) + '\n') |
---|
44 | n/a | err(usage) |
---|
45 | n/a | sys.exit(2) |
---|
46 | n/a | for o, a in opts: |
---|
47 | n/a | if o == '-i': |
---|
48 | n/a | new_interpreter = a.encode() |
---|
49 | n/a | if o == '-p': |
---|
50 | n/a | preserve_timestamps = True |
---|
51 | n/a | if not new_interpreter or not new_interpreter.startswith(b'/') or \ |
---|
52 | n/a | not args: |
---|
53 | n/a | err('-i option or file-or-directory missing\n') |
---|
54 | n/a | err(usage) |
---|
55 | n/a | sys.exit(2) |
---|
56 | n/a | bad = 0 |
---|
57 | n/a | for arg in args: |
---|
58 | n/a | if os.path.isdir(arg): |
---|
59 | n/a | if recursedown(arg): bad = 1 |
---|
60 | n/a | elif os.path.islink(arg): |
---|
61 | n/a | err(arg + ': will not process symbolic links\n') |
---|
62 | n/a | bad = 1 |
---|
63 | n/a | else: |
---|
64 | n/a | if fix(arg): bad = 1 |
---|
65 | n/a | sys.exit(bad) |
---|
66 | n/a | |
---|
67 | n/a | ispythonprog = re.compile(r'^[a-zA-Z0-9_]+\.py$') |
---|
68 | n/a | def ispython(name): |
---|
69 | n/a | return bool(ispythonprog.match(name)) |
---|
70 | n/a | |
---|
71 | n/a | def recursedown(dirname): |
---|
72 | n/a | dbg('recursedown(%r)\n' % (dirname,)) |
---|
73 | n/a | bad = 0 |
---|
74 | n/a | try: |
---|
75 | n/a | names = os.listdir(dirname) |
---|
76 | n/a | except OSError as msg: |
---|
77 | n/a | err('%s: cannot list directory: %r\n' % (dirname, msg)) |
---|
78 | n/a | return 1 |
---|
79 | n/a | names.sort() |
---|
80 | n/a | subdirs = [] |
---|
81 | n/a | for name in names: |
---|
82 | n/a | if name in (os.curdir, os.pardir): continue |
---|
83 | n/a | fullname = os.path.join(dirname, name) |
---|
84 | n/a | if os.path.islink(fullname): pass |
---|
85 | n/a | elif os.path.isdir(fullname): |
---|
86 | n/a | subdirs.append(fullname) |
---|
87 | n/a | elif ispython(name): |
---|
88 | n/a | if fix(fullname): bad = 1 |
---|
89 | n/a | for fullname in subdirs: |
---|
90 | n/a | if recursedown(fullname): bad = 1 |
---|
91 | n/a | return bad |
---|
92 | n/a | |
---|
93 | n/a | def fix(filename): |
---|
94 | n/a | ## dbg('fix(%r)\n' % (filename,)) |
---|
95 | n/a | try: |
---|
96 | n/a | f = open(filename, 'rb') |
---|
97 | n/a | except IOError as msg: |
---|
98 | n/a | err('%s: cannot open: %r\n' % (filename, msg)) |
---|
99 | n/a | return 1 |
---|
100 | n/a | line = f.readline() |
---|
101 | n/a | fixed = fixline(line) |
---|
102 | n/a | if line == fixed: |
---|
103 | n/a | rep(filename+': no change\n') |
---|
104 | n/a | f.close() |
---|
105 | n/a | return |
---|
106 | n/a | head, tail = os.path.split(filename) |
---|
107 | n/a | tempname = os.path.join(head, '@' + tail) |
---|
108 | n/a | try: |
---|
109 | n/a | g = open(tempname, 'wb') |
---|
110 | n/a | except IOError as msg: |
---|
111 | n/a | f.close() |
---|
112 | n/a | err('%s: cannot create: %r\n' % (tempname, msg)) |
---|
113 | n/a | return 1 |
---|
114 | n/a | rep(filename + ': updating\n') |
---|
115 | n/a | g.write(fixed) |
---|
116 | n/a | BUFSIZE = 8*1024 |
---|
117 | n/a | while 1: |
---|
118 | n/a | buf = f.read(BUFSIZE) |
---|
119 | n/a | if not buf: break |
---|
120 | n/a | g.write(buf) |
---|
121 | n/a | g.close() |
---|
122 | n/a | f.close() |
---|
123 | n/a | |
---|
124 | n/a | # Finishing touch -- move files |
---|
125 | n/a | |
---|
126 | n/a | mtime = None |
---|
127 | n/a | atime = None |
---|
128 | n/a | # First copy the file's mode to the temp file |
---|
129 | n/a | try: |
---|
130 | n/a | statbuf = os.stat(filename) |
---|
131 | n/a | mtime = statbuf.st_mtime |
---|
132 | n/a | atime = statbuf.st_atime |
---|
133 | n/a | os.chmod(tempname, statbuf[ST_MODE] & 0o7777) |
---|
134 | n/a | except OSError as msg: |
---|
135 | n/a | err('%s: warning: chmod failed (%r)\n' % (tempname, msg)) |
---|
136 | n/a | # Then make a backup of the original file as filename~ |
---|
137 | n/a | try: |
---|
138 | n/a | os.rename(filename, filename + '~') |
---|
139 | n/a | except OSError as msg: |
---|
140 | n/a | err('%s: warning: backup failed (%r)\n' % (filename, msg)) |
---|
141 | n/a | # Now move the temp file to the original file |
---|
142 | n/a | try: |
---|
143 | n/a | os.rename(tempname, filename) |
---|
144 | n/a | except OSError as msg: |
---|
145 | n/a | err('%s: rename failed (%r)\n' % (filename, msg)) |
---|
146 | n/a | return 1 |
---|
147 | n/a | if preserve_timestamps: |
---|
148 | n/a | if atime and mtime: |
---|
149 | n/a | try: |
---|
150 | n/a | os.utime(filename, (atime, mtime)) |
---|
151 | n/a | except OSError as msg: |
---|
152 | n/a | err('%s: reset of timestamp failed (%r)\n' % (filename, msg)) |
---|
153 | n/a | return 1 |
---|
154 | n/a | # Return success |
---|
155 | n/a | return 0 |
---|
156 | n/a | |
---|
157 | n/a | def fixline(line): |
---|
158 | n/a | if not line.startswith(b'#!'): |
---|
159 | n/a | return line |
---|
160 | n/a | if b"python" not in line: |
---|
161 | n/a | return line |
---|
162 | n/a | return b'#! ' + new_interpreter + b'\n' |
---|
163 | n/a | |
---|
164 | n/a | if __name__ == '__main__': |
---|
165 | n/a | main() |
---|