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