script.py 962 Bytes
Newer Older
1
#! /usr/bin/env python
2

3 4
# script.py -- Make typescript of terminal session.
# Usage:
5 6
#       -a      Append to typescript.
#       -p      Use Python as shell.
7 8 9
# Author: Steen Lumholt.


10
import os, time, sys, getopt
11 12 13
import pty

def read(fd):
14
    data = os.read(fd, 1024)
Georg Brandl's avatar
Georg Brandl committed
15
    script.write(data)
16
    return data
17 18 19 20 21

shell = 'sh'
filename = 'typescript'
mode = 'w'
if os.environ.has_key('SHELL'):
22
    shell = os.environ['SHELL']
23

24 25 26 27 28 29 30 31 32 33 34 35 36
try:
    opts, args = getopt.getopt(sys.argv[1:], 'ap')
except getopt.error, msg:
    print '%s: %s' % (sys.argv[0], msg)
    sys.exit(2)

for o, a in opts:
    if o == '-a':
        mode = 'a'
    elif o == '-p':
        shell = 'python'

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)