Exemple #1
0
class REDTabConsole(REDTab, Ui_REDTabConsole):
    append_text_signal = pyqtSignal(str)

    def __init__(self):
        REDTab.__init__(self)

        self.setupUi(self)

        self.console = TerminalWidget()
        self.console_layout.insertWidget(1, self.console)

        self.refresh_button.clicked.connect(self.refresh_ports)
        self.connect_button.clicked.connect(self.connect_clicked)
        self.copy_button.clicked.connect(
            self.console.copy_selection_to_clipboard)

        # make all elements on this tab non-focusable so the focus has
        # to stay on the console widget
        self.combo_serial_port.setFocusPolicy(Qt.NoFocus)
        self.connect_button.setFocusPolicy(Qt.NoFocus)
        self.refresh_button.setFocusPolicy(Qt.NoFocus)
        self.copy_button.setFocusPolicy(Qt.NoFocus)
        self.setFocusPolicy(Qt.NoFocus)

        self.combo_serial_port.currentIndexChanged.connect(
            self.selected_serial_port_changed)

        self.refresh_ports()

    def selected_serial_port_changed(self, index):
        if index > 0:
            self.connect_button.setEnabled(True)
        else:
            self.connect_button.setEnabled(False)

    def refresh_ports(self):
        current_text = self.combo_serial_port.currentText()
        self.combo_serial_port.clear()
        try:
            ports = get_serial_ports(vid=0x16d0,
                                     pid=0x09e5)  # RED Brick serial console
        except:
            ports = []

        for port in ports:
            self.combo_serial_port.addItem(port.description, port.path)

        self.combo_serial_port.setEnabled(self.combo_serial_port.count() > 0)

        self.connect_button.setEnabled(False)
        if self.combo_serial_port.count() == 0:
            self.combo_serial_port.addItem('No serial port found')
        else:
            self.combo_serial_port.insertItem(0, 'Select...')
            self.combo_serial_port.insertSeparator(1)
            self.combo_serial_port.setCurrentIndex(0)
            if current_text != '':
                index = self.combo_serial_port.findText(current_text)
                if index >= 0:
                    self.combo_serial_port.setCurrentIndex(index)

    def connect_clicked(self):
        text = self.connect_button.text().replace('&', '')

        if text == 'Connect':

            def open_console():
                self.combo_serial_port.setEnabled(False)
                self.refresh_button.setEnabled(False)
                self.console.setEnabled(True)
                self.connect_button.setText("Disconnect")

                port = self.combo_serial_port.itemData(
                    self.combo_serial_port.currentIndex())

                if self.console._session == None:
                    try:
                        self.console.execute(command=port)
                        self.console.setFocus()
                    except Exception as e:
                        self.destroy_session()
                        if isinstance(e, OSError) and e.errno == 16:
                            QMessageBox.critical(
                                get_main_window(), 'Serial Connect Error',
                                'Could not connect to serial console:\n\n{0}\n\nMaybe some other process is currently probing the serial port. Please try again later.'
                                .format(e))
                        else:
                            QMessageBox.critical(
                                get_main_window(), 'Serial Connect Error',
                                'Could not connect to serial console:\n\n{0}'.
                                format(e))

            def cb_pkill_ttyGS0(result):
                if result == None or result.exit_code not in [
                        0, 1
                ]:  # 0 == no error, 1 == nothing killed
                    # FIXME: report error
                    return

                open_console()

            # FIXME: disable this for now as it doesn't work reliable. init
            #        seems to stop spawning getty for a while after some kills
            # kill everything running on ttyGS0 with force, this ensures that no hanging
            # process blocks ttyGS0 and that we get a clean shell instance
            #self.script_manager.execute_script('pkill_ttyGS0', cb_pkill_ttyGS0, ['SIGKILL'])
            open_console()
        else:
            self.destroy_session()

    def destroy_session(self):
        if self.console._session != None:
            self.console.stop()
            self.console._session = None

            def cb_pkill_ttyGS0(result):
                pass

            # FIXME: disable this for now as it doesn't work reliable. init
            #        seems to stop spawning getty for a while after some kills
            # ask everything running on ttyGS0 to exit
            #self.script_manager.execute_script('pkill_ttyGS0', cb_pkill_ttyGS0, ['SIGTERM'])

        self.combo_serial_port.setEnabled(True)
        self.refresh_button.setEnabled(True)
        self.console.setEnabled(False)
        self.connect_button.setText("Connect")

    def tab_on_focus(self):
        self.console._reset()

    def tab_off_focus(self):
        pass

    def tab_destroy(self):
        self.destroy_session()
class REDTabConsole(REDTab, Ui_REDTabConsole):
    append_text_signal = QtCore.pyqtSignal(str)

    def __init__(self):
        REDTab.__init__(self)

        self.setupUi(self)

        self.console = TerminalWidget()
        self.console_layout.insertWidget(1, self.console)

        self.refresh_button.clicked.connect(self.refresh_ports)
        self.connect_button.clicked.connect(self.connect_clicked)
        self.copy_button.clicked.connect(self.console.copy_selection_to_clipboard)

        # make all elements on this tab non-focusable so the focus has
        # to stay on the console widget
        self.combo_serial_port.setFocusPolicy(QtCore.Qt.NoFocus)
        self.connect_button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.refresh_button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.copy_button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.setFocusPolicy(QtCore.Qt.NoFocus)

        self.refresh_ports()

    def refresh_ports(self):
        current_text = self.combo_serial_port.currentText()
        self.combo_serial_port.clear()
        try:
            ports = get_serial_ports()
        except:
            ports = []

        preferred_index = None

        for port in ports:
            if preferred_index is None:
                if 'ttyACM' in port[0] or \
                   'ttyUSB' in port[0] or \
                   'RED Brick' in port[1] or \
                   'usbmodem' in port[0]:
                    preferred_index = self.combo_serial_port.count()

            if len(port[1]) > 0 and port[0] != port[1]:
                self.combo_serial_port.addItem(u'{0} - {1}'.format(port[0], port[1]), port[0])
            else:
                self.combo_serial_port.addItem(port[0], port[0])

        self.combo_serial_port.setEnabled(self.combo_serial_port.count() > 0)

        if self.combo_serial_port.count() == 0:
            self.combo_serial_port.addItem('No serial port found')
            self.connect_button.setEnabled(False)
        elif preferred_index is not None:
            self.combo_serial_port.setCurrentIndex(preferred_index)
            self.connect_button.setEnabled(True)
        else:
            self.connect_button.setEnabled(True)
            index = self.combo_serial_port.findText(current_text)
            if index >= 0:
                self.combo_serial_port.setCurrentIndex(index)

    def connect_clicked(self):
        text = self.connect_button.text()

        if text == 'Connect':
            def open_console():
                self.combo_serial_port.setEnabled(False)
                self.refresh_button.setEnabled(False)
                self.console.setEnabled(True)
                self.connect_button.setText("Disconnect")

                port = self.combo_serial_port.itemData(self.combo_serial_port.currentIndex())

                if self.console._session == None:
                    try:
                        self.console.execute(command=port)
                        self.console.setFocus()
                    except Exception as e:
                        self.destroy_session()
                        QtGui.QMessageBox.critical(get_main_window(), 'Serial Connect Error',
                                                   u'Could not connect to serial console:\n\n{0}'.format(e))

            def cb_pkill_ttyGS0(result):
                if result == None or result.exit_code not in [0, 1]: # 0 == no error, 1 == nothing killed
                    # FIXME: report error
                    return

                open_console()

            # FIXME: disable this for now as it doesn't work reliable. init
            #        seems to stop spawning getty for a while after some kills
            # kill everything running on ttyGS0 with force, this ensures that no hanging
            # process blocks ttyGS0 and that we get a clean shell instance
            #self.script_manager.execute_script('pkill_ttyGS0', cb_pkill_ttyGS0, ['SIGKILL'])
            open_console()
        else:
            self.destroy_session()

    def destroy_session(self):
        if self.console._session != None:
            self.console.stop()
            self.console._session = None

            def cb_pkill_ttyGS0(result):
                pass

            # FIXME: disable this for now as it doesn't work reliable. init
            #        seems to stop spawning getty for a while after some kills
            # ask everything running on ttyGS0 to exit
            #self.script_manager.execute_script('pkill_ttyGS0', cb_pkill_ttyGS0, ['SIGTERM'])

        self.combo_serial_port.setEnabled(True)
        self.refresh_button.setEnabled(True)
        self.console.setEnabled(False)
        self.connect_button.setText("Connect")

    def tab_on_focus(self):
        self.console._reset()

    def tab_off_focus(self):
        pass

    def tab_destroy(self):
        self.destroy_session()
Exemple #3
0
class REDTabConsole(REDTab, Ui_REDTabConsole):
    append_text_signal = QtCore.pyqtSignal(str)

    def __init__(self):
        REDTab.__init__(self)

        self.setupUi(self)

        self.console = TerminalWidget()
        self.console_layout.insertWidget(1, self.console)

        self.refresh_button.clicked.connect(self.refresh_ports)
        self.connect_button.clicked.connect(self.connect_clicked)
        self.copy_button.clicked.connect(
            self.console.copy_selection_to_clipboard)

        # make all elements on this tab non-focusable so the focus has
        # to stay on the console widget
        self.combo_serial_port.setFocusPolicy(QtCore.Qt.NoFocus)
        self.connect_button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.refresh_button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.copy_button.setFocusPolicy(QtCore.Qt.NoFocus)
        self.setFocusPolicy(QtCore.Qt.NoFocus)

        self.refresh_ports()

    def refresh_ports(self):
        current_text = self.combo_serial_port.currentText()
        self.combo_serial_port.clear()
        try:
            ports = get_serial_ports()
        except:
            ports = []

        preferred_index = None

        for port in ports:
            if preferred_index is None:
                if 'ttyACM' in port[0] or \
                   'ttyUSB' in port[0] or \
                   'RED Brick' in port[1] or \
                   'usbmodem' in port[0]:
                    preferred_index = self.combo_serial_port.count()

            if len(port[1]) > 0 and port[0] != port[1]:
                self.combo_serial_port.addItem(
                    u'{0} - {1}'.format(port[0], port[1]), port[0])
            else:
                self.combo_serial_port.addItem(port[0], port[0])

        self.combo_serial_port.setEnabled(self.combo_serial_port.count() > 0)

        if self.combo_serial_port.count() == 0:
            self.combo_serial_port.addItem('No serial port found')
            self.connect_button.setEnabled(False)
        elif preferred_index is not None:
            self.combo_serial_port.setCurrentIndex(preferred_index)
            self.connect_button.setEnabled(True)
        else:
            self.connect_button.setEnabled(True)
            index = self.combo_serial_port.findText(current_text)
            if index >= 0:
                self.combo_serial_port.setCurrentIndex(index)

    def connect_clicked(self):
        text = self.connect_button.text()

        if text == 'Connect':

            def open_console():
                self.combo_serial_port.setEnabled(False)
                self.refresh_button.setEnabled(False)
                self.console.setEnabled(True)
                self.connect_button.setText("Disconnect")

                port = self.combo_serial_port.itemData(
                    self.combo_serial_port.currentIndex())

                if self.console._session == None:
                    try:
                        self.console.execute(command=port)
                        self.console.setFocus()
                    except Exception as e:
                        self.destroy_session()
                        QtGui.QMessageBox.critical(
                            get_main_window(), 'Serial Connect Error',
                            u'Could not connect to serial console:\n\n{0}'.
                            format(e))

            def cb_pkill_ttyGS0(result):
                if result == None or result.exit_code not in [
                        0, 1
                ]:  # 0 == no error, 1 == nothing killed
                    # FIXME: report error
                    return

                open_console()

            # FIXME: disable this for now as it doesn't work reliable. init
            #        seems to stop spawning getty for a while after some kills
            # kill everything running on ttyGS0 with force, this ensures that no hanging
            # process blocks ttyGS0 and that we get a clean shell instance
            #self.script_manager.execute_script('pkill_ttyGS0', cb_pkill_ttyGS0, ['SIGKILL'])
            open_console()
        else:
            self.destroy_session()

    def destroy_session(self):
        if self.console._session != None:
            self.console.stop()
            self.console._session = None

            def cb_pkill_ttyGS0(result):
                pass

            # FIXME: disable this for now as it doesn't work reliable. init
            #        seems to stop spawning getty for a while after some kills
            # ask everything running on ttyGS0 to exit
            #self.script_manager.execute_script('pkill_ttyGS0', cb_pkill_ttyGS0, ['SIGTERM'])

        self.combo_serial_port.setEnabled(True)
        self.refresh_button.setEnabled(True)
        self.console.setEnabled(False)
        self.connect_button.setText("Connect")

    def tab_on_focus(self):
        self.console._reset()

    def tab_off_focus(self):
        pass

    def tab_destroy(self):
        self.destroy_session()