/Demo/scripts/script.py

http://unladen-swallow.googlecode.com/ · Python · 33 lines · 21 code · 6 blank · 6 comment · 3 complexity · f68b095795044fe2930c721587a82184 MD5 · raw file

  1. #! /usr/bin/env python
  2. # script.py -- Make typescript of terminal session.
  3. # Usage:
  4. # -a Append to typescript.
  5. # -p Use Python as shell.
  6. # Author: Steen Lumholt.
  7. import os, time, sys
  8. import pty
  9. def read(fd):
  10. data = os.read(fd, 1024)
  11. file.write(data)
  12. return data
  13. shell = 'sh'
  14. filename = 'typescript'
  15. mode = 'w'
  16. if os.environ.has_key('SHELL'):
  17. shell = os.environ['SHELL']
  18. if '-a' in sys.argv:
  19. mode = 'a'
  20. if '-p' in sys.argv:
  21. shell = 'python'
  22. file = open(filename, mode)
  23. sys.stdout.write('Script started, file is %s\n' % filename)
  24. file.write('Script started on %s\n' % time.ctime(time.time()))
  25. pty.spawn(shell, read)
  26. file.write('Script done on %s\n' % time.ctime(time.time()))
  27. sys.stdout.write('Script done, file is %s\n' % filename)