1#!/usr/bin/env python3
2
3# Fix Python script(s) to reference the interpreter via /usr/bin/env python.
4# Warning: this overwrites the file without making a backup.
5
6import sys
7import re
8
9
10def main():
11    for filename in sys.argv[1:]:
12        try:
13            f = open(filename, 'r')
14        except IOError as msg:
15            print(filename, ': can\'t open :', msg)
16            continue
17        line = f.readline()
18        if not re.match('^#! */usr/local/bin/python', line):
19            print(filename, ': not a /usr/local/bin/python script')
20            f.close()
21            continue
22        rest = f.read()
23        f.close()
24        line = re.sub('/usr/local/bin/python',
25                      '/usr/bin/env python', line)
26        print(filename, ':', repr(line))
27        f = open(filename, "w")
28        f.write(line)
29        f.write(rest)
30        f.close()
31
32if __name__ == '__main__':
33    main()
34