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