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