Home | History | Annotate | Download | only in scripts
      1 #! /usr/bin/env python

      2 
      3 # script.py -- Make typescript of terminal session.

      4 # Usage:

      5 #       -a      Append to typescript.

      6 #       -p      Use Python as shell.

      7 # Author: Steen Lumholt.

      8 
      9 
     10 import os, time, sys, getopt
     11 import pty
     12 
     13 def read(fd):
     14     data = os.read(fd, 1024)
     15     script.write(data)
     16     return data
     17 
     18 shell = 'sh'
     19 filename = 'typescript'
     20 mode = 'w'
     21 if os.environ.has_key('SHELL'):
     22     shell = os.environ['SHELL']
     23 
     24 try:
     25     opts, args = getopt.getopt(sys.argv[1:], 'ap')
     26 except getopt.error, msg:
     27     print '%s: %s' % (sys.argv[0], msg)
     28     sys.exit(2)
     29 
     30 for o, a in opts:
     31     if o == '-a':
     32         mode = 'a'
     33     elif o == '-p':
     34         shell = 'python'
     35 
     36 script = open(filename, mode)
     37 
     38 sys.stdout.write('Script started, file is %s\n' % filename)
     39 script.write('Script started on %s\n' % time.ctime(time.time()))
     40 pty.spawn(shell, read)
     41 script.write('Script done on %s\n' % time.ctime(time.time()))
     42 sys.stdout.write('Script done, file is %s\n' % filename)
     43