#!/usr/bin/env python import sys, os, time, getopt import pty mode = 'wb' shell = 'sh' filename = 'typescript' if 'SHELL' in os.environ: shell = os.environ['SHELL'] try: opts, args = getopt.getopt(sys.argv[1:], 'apc:') except getopt.error as msg: print('%s: %s' % (sys.argv[0], msg)) sys.exit(2) for opt, arg in opts: # option -a: append to typescript file if opt == '-a': mode = 'ab' # option -p: use a Python shell as the terminal command elif opt == '-p': shell = sys.executable # option -c: use the argument to -c as the command elif opt == '-c': shell = arg if args: filename = args[0] script = open(filename, mode) def read(fd): data = os.read(fd, 1024) script.write(data) return data if os.isatty(sys.stdin.fileno()): pty.spawn(shell, read) else: pty.spawn(shell, read, read)