Beispiel #1
0
def test_signal_failure(monkeypatch):
    import os
    import pty
    import signal
    from pyrepl.unix_console import UnixConsole

    def failing_signal(a, b):
        raise ValueError

    def really_failing_signal(a, b):
        raise AssertionError

    mfd, sfd = pty.openpty()
    try:
        with sane_term():
            c = UnixConsole(sfd, sfd)
            c.prepare()
            c.restore()
            monkeypatch.setattr(signal, 'signal', failing_signal)
            c.prepare()
            monkeypatch.setattr(signal, 'signal', really_failing_signal)
            c.restore()
    finally:
        os.close(mfd)
        os.close(sfd)
Beispiel #2
0
def test_signal_failure(monkeypatch):
    import os
    import pty
    import signal
    from pyrepl.unix_console import UnixConsole

    def failing_signal(a, b):
        raise ValueError

    def really_failing_signal(a, b):
        raise AssertionError

    mfd, sfd = pty.openpty()
    try:
        with sane_term():
            c = UnixConsole(sfd, sfd)
            c.prepare()
            c.restore()
            monkeypatch.setattr(signal, 'signal', failing_signal)
            c.prepare()
            monkeypatch.setattr(signal, 'signal', really_failing_signal)
            c.restore()
    finally:
        os.close(mfd)
        os.close(sfd)
Beispiel #3
0
    def __init__(self, ovsdb_server, command_module_paths=None):
        super(Opscli, self).__init__(UnixConsole())
        self.fix_syntax_table()
        # Initialize the OVSDB helper.
        ovsdb.Ovsdb(server=ovsdb_server)
        self.motd = CLI_MSG_MOTD
        self.prompt_base = 'Openswitch'
        try:
            # TODO shell hangs before prompt if this is down
            results = ovsdb.get_map('System', column='mgmt_intf_status')
            if 'hostname' in results:
                self.prompt_base = results['hostname']
        except Exception as e:
            cli_err("Unable to connect to %s: %s." % (ovsdb_server, str(e)))
            raise Exception

        # Initialize command tree.
        for path in command_module_paths:
            if not os.path.isdir(path):
                cli_warn("Ignoring invalid module path '%s'." % path)
                continue
            self.load_commands(path)
        self.fixup_contexts()
        context_push('root')
        if debug_is_on('cli'):
            context_get().cmdtree.dump_tree()

        self.init_ctrl_c()
        self.init_qhelp()
        self.init_completion()
        self.init_history()
Beispiel #4
0
def shell_command():
    """
    Starts an interactive shell with readline style input so you can
    work with Mongrel2 easier.
    """
    try:
        from pyrepl.unix_console import UnixConsole
        from pyrepl.historical_reader import HistoricalReader
    except:
        print("You don't have PyRepl installed, shell not available.")

    reader = HistoricalReader(UnixConsole())
    reader.ps1 = "m2> "
    reader.ps2 = "..> "
    reader.ps3 = "...> "
    reader.ps4 = "....> "

    try:
        while True:

            cmd = try_reading(reader)
            if cmd:
                try:
                    args.parse_and_run_command(cmd, mongrel2.config.commands)
                except Exception as e:
                    print("ERROR:", e)

    except EOFError:
        print("Bye.")
    except KeyboardInterrupt:
        print("BYE!")
Beispiel #5
0
def test():
    from pyrepl.unix_console import UnixConsole
    reader = Reader(UnixConsole())
    reader.ps1 = "**> "
    reader.ps2 = "/*> "
    reader.ps3 = "|*> "
    reader.ps4 = "\*> "
    while reader.readline():
        pass
Beispiel #6
0
def test():
    from pyrepl.unix_console import UnixConsole
    reader = HistoricalReader(UnixConsole())
    reader.ps1 = "h**> "
    reader.ps2 = "h/*> "
    reader.ps3 = "h|*> "
    reader.ps4 = "h\*> "
    while reader.readline():
        pass
Beispiel #7
0
def main(use_pygame_console=0,
         interactmethod=default_interactmethod,
         print_banner=True,
         clear_main=True):
    si, se, so = sys.stdin, sys.stderr, sys.stdout
    try:
        if 0 and use_pygame_console:  # pygame currently borked
            from pyrepl.pygame_console import PyGameConsole, FakeStdin, FakeStdout
            con = PyGameConsole()
            sys.stderr = sys.stdout = FakeStdout(con)
            sys.stdin = FakeStdin(con)
        else:
            from pyrepl.unix_console import UnixConsole
            try:
                import locale
            except ImportError:
                encoding = None
            else:
                if hasattr(locale, 'nl_langinfo') \
                       and hasattr(locale, 'CODESET'):
                    encoding = locale.nl_langinfo(locale.CODESET)
                elif os.environ.get('TERM_PROGRAM') == 'Apple_Terminal':
                    # /me whistles innocently...
                    code = int(
                        os.popen(
                            "defaults read com.apple.Terminal StringEncoding").
                        read())
                    if code == 4:
                        encoding = 'utf-8'
                        # More could go here -- and what's here isn't
                        # bulletproof.  What would be?  AppleScript?
                        # Doesn't seem to be possible.
                    else:
                        encoding = None
                else:
                    encoding = None  # so you get ASCII...
            con = UnixConsole(os.dup(0), os.dup(1), None, encoding)
        if print_banner:
            print("Python", sys.version, "on", sys.platform)
            print('Type "help", "copyright", "credits" or "license" '\
                  'for more information.')
        sys.path.insert(0, os.getcwd())

        if clear_main and __name__ != '__main__':
            mainmod = imp.new_module('__main__')
            sys.modules['__main__'] = mainmod
        else:
            mainmod = sys.modules['__main__']

        rc = ReaderConsole(con, mainmod.__dict__)
        rc.reader._module_list_ready = False
        rc.run_user_init_file()
        getattr(rc, interactmethod)()
    finally:
        sys.stdin, sys.stderr, sys.stdout = si, se, so
Beispiel #8
0
 def get_reader(self):
     if self.reader is None:
         console = UnixConsole(self.f_in, self.f_out, encoding=ENCODING)
         self.reader = ReadlineAlikeReader(console)
         self.reader.config = self.config
     return self.reader