Esempio n. 1
0
def handle_failure(cmd, warn_only):
    if hasattr(cmd, '__name__'):
        cmd = cmd.__name__ + '()'
    message = 'Error running `%s`\n\n%s' % (cmd, indent(format_exc()))
    if warn_only:
        warn(message)
    else:
        abort(message)
Esempio n. 2
0
 def shell(
     self, builtins=SHELL_BUILTINS, shell=True, pty=True,
     combine_stderr=True, dir=None, format=True, warn_only=True
     ):
     ctx = self.ctx
     settings_list = self._settings
     if not settings_list:
         return
     global SHELL_HISTORY_FILE
     if (not SHELL_HISTORY_FILE) and readline:
         SHELL_HISTORY_FILE = expanduser(env.shell_history_file)
         try:
             readline.read_history_file(SHELL_HISTORY_FILE)
         except IOError:
             pass
         atexit.register(readline.write_history_file, SHELL_HISTORY_FILE)
     fastprint("shell mode\n\n", 'system')
     spec = ShellSpec(
         shell=shell, pty=pty, combine_stderr=combine_stderr, dir=dir,
         format=format
         )
     r = run
     count = 0
     prefix = '>> '
     if env.colors:
         prefix = env.color_settings['prefix'](prefix)
     try:
         while 1:
             try:
                 command = raw_input(prefix).strip()
             except EOFError:
                 raise KeyboardInterrupt
             if not command:
                 continue
             builtin_cmd = 0
             if command.startswith('.'):
                 if (len(command) > 1) and command[1].isalpha():
                     builtin_cmd = 1
             if builtin_cmd:
                 command = command.split(' ', 1)
                 if len(command) == 1:
                     command = command[0]
                     arg = ''
                 else:
                     command, arg = command
                 command = command[1:].strip()
                 if not command:
                     continue
                 command = command.replace('_', '-')
                 if command not in builtins:
                     warn("Couldn't find builtin command %r" % command)
                     continue
                 command = builtins[command]
                 if hasattr(command, '__single__'):
                     with settings(ctx=ctx, warn_only=warn_only):
                         try:
                             command(spec, arg)
                         except Exception:
                             handle_failure(command, warn_only)
                     continue
             for kwargs in settings_list:
                 with settings(ctx=ctx, warn_only=warn_only, **kwargs):
                     try:
                         if builtin_cmd:
                             try:
                                 command(spec, arg)
                             except Exception:
                                 handle_failure(command, warn_only)
                         else:
                             r(command, spec.shell, spec.pty,
                               spec.combine_stderr, spec.dir, spec.format)
                     except KeyboardInterrupt:
                         print
                         count += 1
                         if count > 2:
                             raise KeyboardInterrupt
                 count = 0
     except KeyboardInterrupt:
         print
         print
         fastprint("shell mode terminated\n", 'system')