Пример #1
0
    def test_print_source_ja(self):
        self.maxDiff = None

        root_meta = Meta('/path/to/work', {'ENV1': '値1', 'ENV2': '値2'})
        sub_meta = Meta('/tmp2', {'ENV1': '値1', 'ENV2': '値2'})
        special_meta = Meta('/path/to/work2', {'ENV1': '値9', 'ENV2': '値2', 'ENV3': '値3'})

        root_menu = Menu('メイン メニュー', [
            Command('メニュー 1', [CommandLine('コマンド 1', root_meta)]),
            Command('メニュー 2', [CommandLine('コマンド 2', root_meta)]),
            Command('メニュー 3', [CommandLine('コマンド 3', special_meta), CommandLine('command 4', root_meta)]),
            Menu('サブ メニュー', [
                Command('メニュー s1', [CommandLine('コマンド 5', sub_meta)])
            ], sub_meta),
            Command('メニュー 9', [CommandLine('コマンド 9', root_meta)]),
        ], root_meta)

        _in = FakeInput('s\nx\n0\n')
        path = os.path.join('tests', 'resources', 'expect', 'terminal_test_print_source_ja.txt')
        with self.withAssertOutputFile(path, {'': ''}) as out:
            t = Terminal(
                root_menu, 'host', 'user', self.get_exec(encoding='utf-8', stdout=out, stderr=out),
                handler=TerminalHandler(stdin=_in, stdout=out, stderr=out, keep_input_clean=False, getch_enabled=False),
                _input=_in, _output=out, encoding='utf-8', lang='ja_JP', width=80, timing=False)
            t.loop()
Пример #2
0
    def test_wait_input_menu(self):
        self.maxDiff = None

        _in = FakeInput('a\n9\n0\n')

        expected = '\n'.join([
            'Host: host                                                            User: user',
            '================================================================================',
            '  ',
            '--------------------------------------------------------------------------------',
            '------+-------------------------------------------------------------------------',
            '  [0] | Quit',
            '================================================================================',
            'Press menu number (0-0): ',
        ]) * 3
        with self.withAssertOutput(expected, '') as (out, err):
            t = Terminal(Menu('', [], Meta()),
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=err),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=err,
                                                 keep_input_clean=False,
                                                 getch_enabled=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()
Пример #3
0
    def test_print_source(self):
        self.maxDiff = None

        root_meta = Meta('/path/to/work', {'ENV1': 'VAL1', 'ENV2': 'VAL2'})
        sub_meta = Meta('/tmp2', {'ENV1': 'VAL1', 'ENV2': 'VAL2'})
        special_meta = Meta('/path/to/work2', {'ENV1': 'VAL9', 'ENV2': 'VAL2', 'ENV3': 'VAL3'})

        root_menu = Menu('Main menu', [
            Command('label 1', [CommandLine('command 1', root_meta)]),
            Command('label 2', [CommandLine('command 2', root_meta)]),
            Command('label 3', [CommandLine('command 3', special_meta), CommandLine('command 4', root_meta)]),
            Menu('sub menu', [
                Command('label s1', [CommandLine('command 5', sub_meta)])
            ], sub_meta),
            Command('label 9', [CommandLine('command 9', root_meta)]),
        ], root_meta)

        _in = FakeInput('s\nx\n0\n')
        path = os.path.join('tests', 'resources', 'expect', 'terminal_test_print_source.txt')
        with self.withAssertOutputFile(path, {'': ''}) as out:
            t = Terminal(
                root_menu, 'host', 'user', self.get_exec(encoding='utf-8', stdout=out, stderr=out),
                handler=TerminalHandler(stdin=_in, stdout=out, stderr=out, keep_input_clean=False, getch_enabled=False),
                _input=_in, _output=out, encoding='utf-8', lang='en_US', width=80, timing=False)
            t.loop()
Пример #4
0
    def test_loop(self):
        self.maxDiff = None

        root_menu = Menu('Main menu', [
            Command('Menu a', [CommandLine('echo executing a', Meta())]),
            Command('Menu b',
                    [CommandLine('echo executing b && exit 130', Meta())]),
            Menu('Sub Menu 1', [
                Command('Menu 1', [CommandLine('echo executing 1', Meta())]),
                Command('Menu 2', [CommandLine('echo executing 2', Meta())]),
                Command('Menu 3', [CommandLine('echo executing 3', Meta())]),
                Command('Menu 4', [CommandLine('echo executing 4', Meta())]),
                Command('Menu 5', [CommandLine('echo executing 5', Meta())]),
                Command('Menu 6', [CommandLine('echo executing 6', Meta())]),
                Command('Menu 7', [CommandLine('echo executing 7', Meta())]),
                Command('Menu 8', [CommandLine('echo executing 8', Meta())]),
                Command('Menu 9', [CommandLine('echo executing 9', Meta())]),
                Command('Menu 10', [CommandLine('echo executing 10', Meta())]),
            ], Meta())
        ], Meta())

        _in = FakeInput(''.join([
            '1n', '1N', '1\n', '1yx', '2Yx', '3n', '1yx', 'p', '9yx', '0', '-0'
        ]))

        # We use a temporary file due to capture the output of subprocess#call.
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_loop.txt')
        with self.withAssertOutputFile(path) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()

        self.assertEqual(t.executor.logger.buffer, [
            (6, '[INFO] Command started: echo executing a'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo executing b && exit 130'),
            (6, '[INFO] Command ended with return code: 130'),
            (6, '[INFO] Command started: echo executing 10'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo executing 9'),
            (6, '[INFO] Command ended with return code: 0'),
        ])
Пример #5
0
    def test_loop_multiple_commands(self):
        self.maxDiff = None

        root_menu = Menu('Main Menu', [
            Menu('Sub Menu 1', [
                Command('Menu 1', [
                    CommandLine('echo 1', Meta()),
                    CommandLine('echo 2', Meta()),
                ])
            ], Meta()),
            Menu('Sub Menu 2', [
                Menu('Sub Menu 3', [
                    Command('Menu 3', [CommandLine('echo 3', Meta())]),
                    Command('Menu 4', [CommandLine('echo 4', Meta())]),
                ], Meta()),
                Command('Menu 5', [CommandLine('echo 5', Meta())])
            ], Meta()),
            Command('Menu 6', [
                CommandLine('echo 6', Meta()),
                CommandLine('echo 7', Meta()),
                CommandLine('false', Meta()),
                CommandLine('echo 8', Meta()),
            ])
        ], Meta())

        _in = FakeInput(''.join(['1', '.1yx', '0', '21.1yx', '0.0', '3yx', '0']))

        # We use a temporary file due to capture the output of subprocess#call.
        with self.withAssertOutputFile('tests/resources/expect/terminal_test_loop_multiple_commands.txt') as out:
            t = Terminal(
                root_menu,
                'host', 'user', self.get_exec(encoding='utf-8', stdout=out, stderr=out),
                handler=TerminalHandler(stdin=_in, stdout=out, stderr=out, keep_input_clean=False),
                _input=_in, _output=out, encoding='utf-8', lang='en_US', width=80, timing=False)
            t.loop()

        self.assertEqual(t.executor.logger.buffer, [
            (6, '[INFO] Command started: echo 1'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 2'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 3'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 6'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 7'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: false'),
            (6, '[INFO] Command ended with return code: 1'),
        ])
Пример #6
0
def main(stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, keep_input_clean=True, timing=True):
    """
    Main function
    """

    base_setting = Setting(stdin=stdin, stdout=stdout, stderr=stderr).parse_args(sys.argv)

    # for terminal restoration
    handler = TerminalHandler(stdin=stdin, stdout=stdout, stderr=stderr,
                              keep_input_clean=keep_input_clean, getch_enabled=base_setting.getch_enabled)
    signal.signal(signal.SIGTERM, handler.restore_terminal)

    try:
        setting = base_setting.resolve_encoding(handler).lookup_config().load_config()
        executor = CommandExecutor(
            SystemLogger(setting.encoding), setting.encoding, stdin, stdout, stderr, setting.pid_dir
        )

        t = Terminal(
            setting.root_menu,
            get_hostname(),
            get_username(),
            executor,
            handler=handler,
            _input=setting.stdin,
            _output=setting.stdout,
            encoding=setting.encoding,
            lang=setting.lang,
            width=setting.width,
            timing=timing
        )

        t.loop()
    except (KeyboardInterrupt, EOFError):
        pass
    except EasyMenuError as e:
        base_setting.stdout.write('%s: %s\n' % (e.__class__.__name__, e))
        return 2
    except IOError as e:
        # maybe killed by outside
        base_setting.stdout.write('\n%s: %s\n' % (e.__class__.__name__, e))
        return 3
    except OSError as e:
        # e.g. working directory does not exist
        base_setting.stdout.write('%s: %s\n' % (e.__class__.__name__, e))
        return 4
    finally:
        # assume to restore original terminal settings
        handler.restore_terminal(None, None)
    return 0
Пример #7
0
    def test_print_source_ja(self):
        self.maxDiff = None

        root_meta = Meta('/path/to/work', {'ENV1': '値1', 'ENV2': '値2'})
        sub_meta = Meta('/tmp2', {'ENV1': '値1', 'ENV2': '値2'})
        special_meta = Meta('/path/to/work2', {
            'ENV1': '値9',
            'ENV2': '値2',
            'ENV3': '値3'
        })

        root_menu = Menu('メイン メニュー', [
            Command('メニュー 1', [CommandLine('コマンド 1', root_meta)]),
            Command('メニュー 2', [CommandLine('コマンド 2', root_meta)]),
            Command('メニュー 3', [
                CommandLine('コマンド 3', special_meta),
                CommandLine('command 4', root_meta)
            ]),
            Menu('サブ メニュー',
                 [Command('メニュー s1', [CommandLine('コマンド 5', sub_meta)])],
                 sub_meta),
            Command('メニュー 9', [CommandLine('コマンド 9', root_meta)]),
        ], root_meta)

        _in = FakeInput('s\nx\n0\n')
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_print_source_ja.txt')
        with self.withAssertOutputFile(path, {'': ''}) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False,
                                                 getch_enabled=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='ja_JP',
                         width=80,
                         timing=False)
            t.loop()
Пример #8
0
    def test_print_source(self):
        self.maxDiff = None

        root_meta = Meta('/path/to/work', {'ENV1': 'VAL1', 'ENV2': 'VAL2'})
        sub_meta = Meta('/tmp2', {'ENV1': 'VAL1', 'ENV2': 'VAL2'})
        special_meta = Meta('/path/to/work2', {
            'ENV1': 'VAL9',
            'ENV2': 'VAL2',
            'ENV3': 'VAL3'
        })

        root_menu = Menu('Main menu', [
            Command('label 1', [CommandLine('command 1', root_meta)]),
            Command('label 2', [CommandLine('command 2', root_meta)]),
            Command('label 3', [
                CommandLine('command 3', special_meta),
                CommandLine('command 4', root_meta)
            ]),
            Menu('sub menu',
                 [Command('label s1', [CommandLine('command 5', sub_meta)])],
                 sub_meta),
            Command('label 9', [CommandLine('command 9', root_meta)]),
        ], root_meta)

        _in = FakeInput('s\nx\n0\n')
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_print_source.txt')
        with self.withAssertOutputFile(path, {'': ''}) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False,
                                                 getch_enabled=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()
Пример #9
0
    def test_loop(self):
        self.maxDiff = None

        root_menu = Menu('Main menu', [
            Command('Menu a', [CommandLine('echo executing a', Meta())]),
            Command('Menu b', [CommandLine('echo executing b && exit 130', Meta())]),
            Menu('Sub Menu 1', [
                Command('Menu 1', [CommandLine('echo executing 1', Meta())]),
                Command('Menu 2', [CommandLine('echo executing 2', Meta())]),
                Command('Menu 3', [CommandLine('echo executing 3', Meta())]),
                Command('Menu 4', [CommandLine('echo executing 4', Meta())]),
                Command('Menu 5', [CommandLine('echo executing 5', Meta())]),
                Command('Menu 6', [CommandLine('echo executing 6', Meta())]),
                Command('Menu 7', [CommandLine('echo executing 7', Meta())]),
                Command('Menu 8', [CommandLine('echo executing 8', Meta())]),
                Command('Menu 9', [CommandLine('echo executing 9', Meta())]),
                Command('Menu 10', [CommandLine('echo executing 10', Meta())]),
            ], Meta())
        ], Meta())

        _in = FakeInput(''.join(['1n', '1N', '1\n', '1yx', '2Yx', '3n', '1yx', 'p', '9yx', '0', '-0']))

        # We use a temporary file due to capture the output of subprocess#call.
        path = os.path.join('tests', 'resources', 'expect', 'terminal_test_loop.txt')
        with self.withAssertOutputFile(path) as out:
            t = Terminal(
                root_menu, 'host', 'user', self.get_exec(encoding='utf-8', stdout=out, stderr=out),
                handler=TerminalHandler(stdin=_in, stdout=out, stderr=out, keep_input_clean=False),
                _input=_in, _output=out, encoding='utf-8', lang='en_US', width=80, timing=False)
            t.loop()

        self.assertEqual(t.executor.logger.buffer, [
            (6, '[INFO] Command started: echo executing a'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo executing b && exit 130'),
            (6, '[INFO] Command ended with return code: 130'),
            (6, '[INFO] Command started: echo executing 10'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo executing 9'),
            (6, '[INFO] Command ended with return code: 0'),
        ])
Пример #10
0
    def test_wait_input_menu(self):
        self.maxDiff = None

        _in = FakeInput('a\n9\n0\n')

        expected = '\n'.join([
            'Host: host                                                            User: user',
            '================================================================================',
            '  ',
            '--------------------------------------------------------------------------------',
            '------+-------------------------------------------------------------------------',
            '  [0] | Quit',
            '================================================================================',
            'Press menu number (0-0): ',
        ]) * 3
        with self.withAssertOutput(expected, '') as (out, err):
            t = Terminal(
                Menu('', [], Meta()), 'host', 'user', self.get_exec(encoding='utf-8', stdout=out, stderr=err),
                handler=TerminalHandler(stdin=_in, stdout=out, stderr=err, keep_input_clean=False, getch_enabled=False),
                _input=_in, _output=out, encoding='utf-8', lang='en_US', width=80, timing=False)
            t.loop()
Пример #11
0
    def test_loop_sjis(self):
        self.maxDiff = None

        root_menu = Menu('メインメニュー', [Command('メニュー 1', [CommandLine("echo 'あいうえお'", Meta())])], Meta())

        _in = FakeInput(''.join(['1yx', '0']))

        # We use a temporary file due to capture the output of subprocess#call.
        path = os.path.join('tests', 'resources', 'expect', 'terminal_test_loop_sjis.txt')
        with self.withAssertOutputFile(path, expect_file_encoding='sjis', output_encoding='sjis') as out:
            t = Terminal(
                root_menu, 'ホスト', 'ユーザ', self.get_exec(encoding='sjis', stdout=out, stderr=out),
                handler=TerminalHandler(stdin=_in, stdout=out, stderr=out, keep_input_clean=False),
                _input=_in, _output=out, encoding='sjis', lang='ja_JP', width=80, timing=False)
            t.loop()
            # out.seek(0)
            # print(out.read())

        self.assertEqual(t.executor.logger.buffer, [
            (6, "[INFO] Command started: echo 'あいうえお'"),
            (6, "[INFO] Command ended with return code: 0"),
        ])
Пример #12
0
    def test_loop_sjis(self):
        self.maxDiff = None

        root_menu = Menu(
            'メインメニュー',
            [Command('メニュー 1', [CommandLine("echo 'あいうえお'", Meta())])], Meta())

        _in = FakeInput(''.join(['1yx', '0']))

        # We use a temporary file due to capture the output of subprocess#call.
        path = os.path.join('tests', 'resources', 'expect',
                            'terminal_test_loop_sjis.txt')
        with self.withAssertOutputFile(path,
                                       expect_file_encoding='sjis',
                                       output_encoding='sjis') as out:
            t = Terminal(root_menu,
                         'ホスト',
                         'ユーザ',
                         self.get_exec(encoding='sjis', stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False),
                         _input=_in,
                         _output=out,
                         encoding='sjis',
                         lang='ja_JP',
                         width=80,
                         timing=False)
            t.loop()
            # out.seek(0)
            # print(out.read())

        self.assertEqual(t.executor.logger.buffer, [
            (6, "[INFO] Command started: echo 'あいうえお'"),
            (6, "[INFO] Command ended with return code: 0"),
        ])
Пример #13
0
    def test_loop_multiple_commands(self):
        self.maxDiff = None

        root_menu = Menu('Main Menu', [
            Menu('Sub Menu 1', [
                Command('Menu 1', [
                    CommandLine('echo 1', Meta()),
                    CommandLine('echo 2', Meta()),
                ])
            ], Meta()),
            Menu('Sub Menu 2', [
                Menu('Sub Menu 3', [
                    Command('Menu 3', [CommandLine('echo 3', Meta())]),
                    Command('Menu 4', [CommandLine('echo 4', Meta())]),
                ], Meta()),
                Command('Menu 5', [CommandLine('echo 5', Meta())])
            ], Meta()),
            Command('Menu 6', [
                CommandLine('echo 6', Meta()),
                CommandLine('echo 7', Meta()),
                CommandLine('false', Meta()),
                CommandLine('echo 8', Meta()),
            ])
        ], Meta())

        _in = FakeInput(''.join(
            ['1', '.1yx', '0', '21.1yx', '0.0', '3yx', '0']))

        # We use a temporary file due to capture the output of subprocess#call.
        with self.withAssertOutputFile(
                'tests/resources/expect/terminal_test_loop_multiple_commands.txt'
        ) as out:
            t = Terminal(root_menu,
                         'host',
                         'user',
                         self.get_exec(encoding='utf-8',
                                       stdout=out,
                                       stderr=out),
                         handler=TerminalHandler(stdin=_in,
                                                 stdout=out,
                                                 stderr=out,
                                                 keep_input_clean=False),
                         _input=_in,
                         _output=out,
                         encoding='utf-8',
                         lang='en_US',
                         width=80,
                         timing=False)
            t.loop()

        self.assertEqual(t.executor.logger.buffer, [
            (6, '[INFO] Command started: echo 1'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 2'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 3'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 6'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: echo 7'),
            (6, '[INFO] Command ended with return code: 0'),
            (6, '[INFO] Command started: false'),
            (6, '[INFO] Command ended with return code: 1'),
        ])