home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / FIXPS.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  726 b   |  34 lines

  1. #!/usr/bin/env python
  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.  
  6. import sys
  7. import re
  8.  
  9.  
  10. def main():
  11.     for file in sys.argv[1:]:
  12.         try:
  13.             f = open(file, 'r')
  14.         except IOError, msg:
  15.             print file, ': can\'t open :', msg
  16.             continue
  17.         line = f.readline()
  18.         if not re.match('^#! */usr/local/bin/python', line):
  19.             print file, ': 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 file, ':', `line`
  27.         f = open(file, "w")
  28.         f.write(line)
  29.         f.write(rest)
  30.         f.close()
  31.  
  32.  
  33. main()
  34.