cpython/Tools/scripts/fixps.py

32 lines
892 B
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
1992-12-09 19:14:40 -04:00
# Fix Python script(s) to reference the interpreter via /usr/bin/env python.
# Warning: this overwrites the file without making a backup.
1992-12-09 19:14:40 -04:00
import sys
import re
1992-12-09 19:14:40 -04:00
def main():
for filename in sys.argv[1:]:
2001-01-17 04:48:39 -04:00
try:
f = open(filename, 'r')
except IOError as msg:
print(filename, ': can\'t open :', msg)
2001-01-17 04:48:39 -04:00
continue
with f:
line = f.readline()
if not re.match('^#! */usr/local/bin/python', line):
print(filename, ': not a /usr/local/bin/python script')
continue
rest = f.read()
2001-01-17 04:48:39 -04:00
line = re.sub('/usr/local/bin/python',
'/usr/bin/env python', line)
print(filename, ':', repr(line))
with open(filename, "w") as f:
f.write(line)
f.write(rest)
1992-12-09 19:14:40 -04:00
if __name__ == '__main__':
main()