# Path: pysecure/adapters/channela.py
# class RemoteShellProcessor(object):
#     def __init__(self, ssh_session, block_size=DEFAULT_SHELL_READ_BLOCK_SIZE):
#         self.__log = logging.getLogger('RSP')
#         self.__log.debug("Initializing RSP.")
# 
#         self.__ssh_session = ssh_session
#         self.__block_size = block_size
# 
#     def __wait_on_output(self, data_cb):
#         self.__log.debug("Reading chunked output.")
# 
#         start_at = time()
#         while self.__sc.is_open() and self.__sc.is_eof() is False:
#             buffer_ = self.__sc.read_nonblocking(self.__block_size)
#             if buffer_ == b'':
#                 delta = time() - start_at
#                 if delta * 1000 > NONBLOCK_READ_TIMEOUT_MS:
#                     break
# 
#                 continue
# 
#             data_cb(buffer_)
#             start_at = time()
# 
#     def __wait_on_output_all(self, whole_data_cb):
#         self.__log.debug("Reading complete output.")
# 
#         received = bytearray()
#         def data_cb(buffer_):
#             received.extend(buffer_)
# 
#         self.__wait_on_output(data_cb)
#         whole_data_cb(bytes(received))
# 
#     def do_command(self, command, block_cb=None, add_nl=True, 
#                    drop_last_line=True, drop_first_line=True):
#         self.__log.debug("Sending shell command: %s" % (command.rstrip()))
# 
#         if add_nl is True:
#             command += '\n'
# 
#         self.__sc.write(bytify(command))
#         
#         if block_cb is not None:
#             self.__wait_on_output(block_cb)
#         else:
#             received = bytearray()
#             def data_cb(buffer_):
#                 received.extend(bytify(buffer_))
#             
#             self.__wait_on_output_all(data_cb)
# 
#             if drop_first_line is True:
#                 received = received[received.index(b'\n') + 1:]
# 
#             # In all likelihood, the last line is probably the prompt.
#             if drop_last_line is True:
#                 received = received[:received.rindex(b'\n')]
# 
#             return bytes(received)
# 
#     def shell(self, ready_cb, cols=80, rows=24):
#         self.__log.debug("Starting RSP shell.")
# 
#         with SshChannel(self.__ssh_session) as sc:
#             sc.open_session()
#             sc.request_env('aa', 'bb')
# #        sc.request_env('LANG', 'en_US.UTF-8')
# 
#             sc.request_pty()
#             sc.change_pty_size(cols, rows)
#             sc.request_shell()
# 
#             self.__log.debug("Waiting for shell welcome message.")
# 
#             welcome = bytearray()
#             def welcome_received_cb(data):
#                 welcome.extend(bytify(data))
#             
#             self.__sc = sc
#             self.__wait_on_output_all(welcome_received_cb)
# 
#             self.__log.debug("RSP shell is ready.")
#             ready_cb(sc, stringify(welcome))
#             self.__sc = None
# 
# Path: pysecure/test/test_base.py
# def connect_ssh_test(ssh_cb):
#     print("Connecting SSH with key: %s" % (key_filepath))
#     auth_cb = get_key_auth_cb(key_filepath)
#     connect_ssh_with_cb(ssh_cb, user, host, auth_cb, verbosity=verbosity)


from unittest import TestCase
from pysecure.adapters.channela import RemoteShellProcessor
from pysecure.test.test_base import connect_ssh_test


class RemoteShellTest(TestCase):
    def __ssh_cb(self, ssh):
        rsp = RemoteShellProcessor(ssh)
        
        def shell_context_cb(sc, welcome):
#            print('-' * 50 + '\n' + 
#                  welcome + '\n' + 
#                  '-' * 50)

            output = rsp.do_command('whoami')
#            print(output)

#            output = rsp.do_command('cat /proc/uptime')
#            print(output)

# Doesn't work. See bug report at libssh.
#        print("Setting environment.")
#        sc.request_env('aa', 'bb')
#        sc.request_env('LANG', 'en_US.UTF-8')

        rsp.shell(shell_context_cb)

    def test_remote_shell(self):

# Path: pysecure/adapters/channela.py
# class RemoteShellProcessor(object):
#     def __init__(self, ssh_session, block_size=DEFAULT_SHELL_READ_BLOCK_SIZE):
#         self.__log = logging.getLogger('RSP')
#         self.__log.debug("Initializing RSP.")
# 
#         self.__ssh_session = ssh_session
#         self.__block_size = block_size
# 
#     def __wait_on_output(self, data_cb):
#         self.__log.debug("Reading chunked output.")
# 
#         start_at = time()
#         while self.__sc.is_open() and self.__sc.is_eof() is False:
#             buffer_ = self.__sc.read_nonblocking(self.__block_size)
#             if buffer_ == b'':
#                 delta = time() - start_at
#                 if delta * 1000 > NONBLOCK_READ_TIMEOUT_MS:
#                     break
# 
#                 continue
# 
#             data_cb(buffer_)
#             start_at = time()
# 
#     def __wait_on_output_all(self, whole_data_cb):
#         self.__log.debug("Reading complete output.")
# 
#         received = bytearray()
#         def data_cb(buffer_):
#             received.extend(buffer_)
# 
#         self.__wait_on_output(data_cb)
#         whole_data_cb(bytes(received))
# 
#     def do_command(self, command, block_cb=None, add_nl=True, 
#                    drop_last_line=True, drop_first_line=True):
#         self.__log.debug("Sending shell command: %s" % (command.rstrip()))
# 
#         if add_nl is True:
#             command += '\n'
# 
#         self.__sc.write(bytify(command))
#         
#         if block_cb is not None:
#             self.__wait_on_output(block_cb)
#         else:
#             received = bytearray()
#             def data_cb(buffer_):
#                 received.extend(bytify(buffer_))
#             
#             self.__wait_on_output_all(data_cb)
# 
#             if drop_first_line is True:
#                 received = received[received.index(b'\n') + 1:]
# 
#             # In all likelihood, the last line is probably the prompt.
#             if drop_last_line is True:
#                 received = received[:received.rindex(b'\n')]
# 
#             return bytes(received)
# 
#     def shell(self, ready_cb, cols=80, rows=24):
#         self.__log.debug("Starting RSP shell.")
# 
#         with SshChannel(self.__ssh_session) as sc:
#             sc.open_session()
#             sc.request_env('aa', 'bb')
# #        sc.request_env('LANG', 'en_US.UTF-8')
# 
#             sc.request_pty()
#             sc.change_pty_size(cols, rows)
#             sc.request_shell()
# 
#             self.__log.debug("Waiting for shell welcome message.")
# 
#             welcome = bytearray()
#             def welcome_received_cb(data):
#                 welcome.extend(bytify(data))
#             
#             self.__sc = sc
#             self.__wait_on_output_all(welcome_received_cb)
# 
#             self.__log.debug("RSP shell is ready.")
#             ready_cb(sc, stringify(welcome))
#             self.__sc = None
# 
# Path: pysecure/test/test_base.py
# def connect_ssh_test(ssh_cb):
#     print("Connecting SSH with key: %s" % (key_filepath))
#     auth_cb = get_key_auth_cb(key_filepath)
#     connect_ssh_with_cb(ssh_cb, user, host, auth_cb, verbosity=verbosity)


# Path: pysecure/test/remote_shell.py
from unittest import TestCase
from pysecure.adapters.channela import RemoteShellProcessor
from pysecure.test.test_base import connect_ssh_test



class RemoteShellTest(TestCase):
    def __ssh_cb(self, ssh):
        rsp = RemoteShellProcessor(ssh)
        
        def shell_context_cb(sc, welcome):
#            print('-' * 50 + '\n' + 
#                  welcome + '\n' + 
#                  '-' * 50)

            output = rsp.do_command('whoami')
#            print(output)

#            output = rsp.do_command('cat /proc/uptime')
#            print(output)

# Doesn't work. See bug report at libssh.
#        print("Setting environment.")
#        sc.request_env('aa', 'bb')
#        sc.request_env('LANG', 'en_US.UTF-8')

        rsp.shell(shell_context_cb)

    def test_remote_shell(self):

        connect_ssh_test(self.__ssh_cb)