def mainimpl(options, command, term, lang, termenc): from tffstub import tff import input import output import controller import trace if not _check_output_device(options.output, options.monochrome): return tty = tff.DefaultPTY(term, lang, command, sys.stdin) try: tty.fitsize() controller = controller.ActionController(tty) tracer = trace.TraceHandler(options.output, termenc, controller) if options.breakstart: controller.set_break() session = tff.Session(tty) session.start(stdin=sys.stdin, stdout=sys.stdout, termenc=termenc, inputhandler=input.InputHandler(controller, tracer), outputhandler=output.OutputHandler(controller, tracer)) except IOError: e = sys.exc_info()[1] logging.exception(e) logging.exception("Connection closed.") print("Connection closed.") except Exception: e = sys.exc_info()[1] logging.exception(e) logging.exception("Aborted by exception.") print('trachet aborted by an uncaught exception.') print(' see $HOME/.trachet/log/log.txt.') finally: try: tty.restore_term() except Exception: e = sys.exc_info()[1] logging.exception(e)
else: template.disable_color() finally: os.close(fd) tty = tff.DefaultPTY(term, lang, command, sys.stdin) try: tty.fitsize() controller = controller.ActionController(tty) tracer = trace.TraceHandler(options.output, termenc, controller) if options.breakstart: controller.set_break() session = tff.Session(tty) session.start(stdin=sys.stdin, stdout=sys.stdout, termenc=termenc, inputhandler=input.InputHandler(controller, tracer), outputhandler=output.OutputHandler(controller, tracer)) except IOError, e: logging.exception(e) logging.exception("Connection closed.") print("Connection closed.") except Exception, e: logging.exception(e) logging.exception("Aborted by exception.") print('trachet aborted by an uncaught exception.'
def main(): ''' entry point function for command line program ''' import sys import os import optparse import logging # parse options and arguments usage = 'usage: %prog [options] command' parser = optparse.OptionParser(usage=usage) parser.add_option('-o', '--output', dest='output', default='/dev/null', help='specify output device or file') parser.add_option('-b', '--break', dest='breakstart', action="store_true", default=False, help='"break" the program at the startup time') parser.add_option('-m', '--monochrome', dest='monochrome', action="store_true", default=False, help='don\'t use color in output terminal') parser.add_option('--version', dest='version', action="store_true", default=False, help='show version') (options, args) = parser.parse_args() if options.version: _printver() return # retrive starting command if len(args) == 1: command = args[0] elif len(args) > 1: command = " ".join(args) elif os.getenv('SHELL'): command = os.getenv('SHELL') else: command = '/bin/sh' # retrive TERM setting if os.getenv('TERM'): term = os.getenv('TERM') else: term = 'xterm' # retrive LANG setting if os.getenv('LANG'): lang = os.getenv('LANG') else: import locale lang = '%s.%s' % locale.getdefaultlocale() # retrive terminal encoding setting import locale language, encoding = locale.getdefaultlocale() termenc = encoding if not termenc: termenc = "UTF-8" # fix for cygwin environment, such as utf_8_cjknarrow if termenc.lower().startswith("utf_8_"): termenc = "UTF-8" rcdir = os.path.join(os.getenv("HOME"), ".trachet") logdir = os.path.join(rcdir, "log") if not os.path.exists(logdir): os.makedirs(logdir) logfile = os.path.join(logdir, "log.txt") logging.basicConfig(filename=logfile, filemode="w") from tff import tff import input import output import controller import trace import template try: fd = os.open(options.output, os.O_WRONLY|os.O_CREAT|os.O_NONBLOCK) except: logging.exception("Connection closed.") print "Cannot access output file or device: %s." % options.output return try: if os.isatty(fd): if not os.path.exists(options.output): print "The output device %s is not found." % options.output return if options.output == os.ttyname(0): print ("The output device %s is busy (current TTY). " "Please specify another TTY device.") % options.output return if options.monochrome: template.disable_color() else: template.enable_color() else: template.disable_color() finally: os.close(fd) tty = tff.DefaultPTY(term, lang, command, sys.stdin) try: tty.fitsize() controller = controller.ActionController(tty) tracer = trace.TraceHandler(options.output, termenc, controller) if options.breakstart: controller.set_break() session = tff.Session(tty) session.start(stdin=sys.stdin, stdout=sys.stdout, termenc=termenc, inputhandler=input.InputHandler(controller, tracer), outputhandler=output.OutputHandler(controller, tracer)) except IOError: logging.exception("Connection closed.") print "connection closed." except: logging.exception("Aborted by exception.") print ("trachet aborted by an uncaught exception." " see $HOME/.trachet/log/log.txt.") finally: try: tty.restore_term() except: pass