Example #1
0
    def execute(self):
        """Given the command-line arguments, this figures out which
        subcommand is being run, creates a parser appropriate to that command,
        and runs it.
        """
        parser, args, config = self.preparse()
        try:
            subcommand = args[1]
        except IndexError:
            subcommand = 'help'  # Display help if no arguments were given.

        # First search in aliases
        if subcommand in config.get('alias', {}):
            alias = shutil.split(config.get('alias.%s' %
                                            subcommand).encode('utf-8'))
            args = [args[0]] + alias + args[2:]
            subcommand = args[1]

        if subcommand == 'help':
            if len(args) <= 2:
                parser.print_help()
                sys.stdout.write(self.main_help_text(config) + '\n')
            elif args[2] == '--commands':
                sys.stdout.write(self.main_help_text(
                    config, commands_only=True) + '\n')
            else:
                self.fetch_command_klass(config, args[2])(config, self._ui)\
                    .print_help(self.prog_name, args[2])
        elif args[1:] in (['--help'], ['-h']):
            parser.print_help()
            sys.stdout.write(self.main_help_text(config) + '\n')
        else:
            self.fetch_command_klass(config, subcommand)(config, self._ui)\
                .run_from_argv(args)
Example #2
0
 def _command(self, config):
     status, output, error = (0, '', '')
     for command in self.commands:
         if isinstance(command, basestring):
             command = shutil.split(string.Template(command).safe_substitute(**config.environment))
         else:
             command = map(lambda part: string.Template(part).safe_substitute(**config.environment), command)
         s, o, e = execute_in_environment(config.environment, *command)
         status += 0 if s in self.success else 1
         output += o
         error  += e
         if status != 0:
             break
     return status, output, error
Example #3
0
 def _command(self, config):
     status, output, error = (0, '', '')
     for command in self.commands:
         if isinstance(command, basestring):
             command = shutil.split(
                 string.Template(command).safe_substitute(
                     **config.environment))
         else:
             command = map(
                 lambda part: string.Template(part).safe_substitute(
                     **config.environment), command)
         s, o, e = execute_in_environment(config.environment, *command)
         status += 0 if s in self.success else 1
         output += o
         error += e
         if status != 0:
             break
     return status, output, error
Example #4
0
        readline.set_completer(completer)
    except:
        pass


if __name__ == "__main__":
    if len(sys.argv) > 1:
        from samovar.main import main

        sys.exit(main(sys.argv))
    else:
        try:
            while True:
                import subprocess

                setup_readline()
                command = input("> ").strip()
                if command in ("exit", "quite"):
                    break
                if command != "":
                    timeit = False
                    if command.startswith("timeit "):
                        command = command.replace("timeit ", "", 1)
                        timeit = True
                    start_time = get_time()
                    subprocess.call([sys.executable, os.path.abspath(__file__)] + shutil.split(command), shell=False)
                    if timeit:
                        print("Duration: %.4fs", get_time() - start_time)
        except KeyboardInterrupt:
            print()
Example #5
0
        readline.rl.read_inputrc(os.path.join(os.path.dirname(__file__), 'svreadline.ini'))
        readline.parse_and_bind('tab: complete')
        readline.set_completer(completer)
    except:
        pass


if __name__ == '__main__':
    if len(sys.argv) > 1:
        from samovar.main import main
        sys.exit(main(sys.argv))
    else:
        try:
            while True:
                import subprocess
                setup_readline()
                command = input('> ').strip()
                if command in ('exit', 'quite'):
                    break
                if command != '':
                    timeit = False
                    if command.startswith('timeit '):
                        command = command.replace('timeit ', '', 1)
                        timeit = True
                    start_time = get_time()
                    subprocess.call([sys.executable, os.path.abspath(__file__)] + shutil.split(command), shell=False)
                    if timeit:
                        print('Duration: %.4fs', get_time() - start_time)
        except KeyboardInterrupt:
            print()