Esempio n. 1
0
    def _loadSettings(self):
        """
        Loads the settings from the persistent settings file.
        """

        local_config = LocalConfig.instance()
        # restore the Dynamips settings from QSettings (for backward compatibility)
        legacy_settings = {}
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name in DYNAMIPS_SETTINGS.keys():
            if settings.contains(name):
                legacy_settings[name] = settings.value(name, type=DYNAMIPS_SETTING_TYPES[name])
        settings.remove("")
        settings.endGroup()

        if legacy_settings:
            local_config.saveSectionSettings(self.__class__.__name__, legacy_settings)
        self._settings = local_config.loadSectionSettings(self.__class__.__name__, DYNAMIPS_SETTINGS)

        if not os.path.exists(self._settings["dynamips_path"]):
            self._settings["dynamips_path"] = self._findDynamips(self)

        # keep the config file sync
        self._saveSettings()
Esempio n. 2
0
    def _loadIOUImages(self):
        """
        Load the IOU images from the persistent settings file.
        """

        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup("IOUImages")

        # load the IOU images
        size = settings.beginReadArray("iou_image")
        for index in range(0, size):
            settings.setArrayIndex(index)
            path = settings.value("path", "")
            image = settings.value("image", "")
            startup_config = settings.value("startup_config", "")
            use_default_iou_values = settings.value("use_default_iou_values", True, type=bool)
            ram = settings.value("ram", 256, type=int)
            nvram = settings.value("nvram", 128, type=int)
            server = settings.value("server", "local")
            key = "{server}:{image}".format(server=server, image=image)
            self._iou_images[key] = {"path": path,
                                     "image": image,
                                     "startup_config": startup_config,
                                     "use_default_iou_values": use_default_iou_values,
                                     "ram": ram,
                                     "nvram": nvram,
                                     "server": server}

        settings.endArray()
        settings.endGroup()
def get_default_base_config(base_config_template_path):
    """
    Copy the default base config template to settings directory (if not already present) and returns the path.

    :param base_config_template_path: path to the base config template

    :return: path to the base config
    """

    config_dir = os.path.join(os.path.dirname(QtCore.QSettings().fileName()),
                              "base_configs")
    if base_config_template_path:
        try:
            os.makedirs(config_dir)
        except FileExistsError:
            pass
        except OSError as e:
            log.error(
                "could not create the base configs directory {}: {}".format(
                    config_dir, e))
            return ""
        try:
            base_config_path = os.path.join(
                config_dir, os.path.basename(base_config_template_path))
            if not os.path.isfile(base_config_path):
                shutil.copyfile(base_config_template_path, base_config_path)
            return os.path.normpath(base_config_path)
        except OSError as e:
            log.error("could not copy {} to {}: {}".format(
                base_config_template_path, base_config_path, e))
    return ""
Esempio n. 4
0
    def _loadSettings(self):
        """
        Loads the settings from the persistent settings file.
        """

        local_config = LocalConfig.instance()

        # restore the Qemu settings from QSettings (for backward compatibility)
        legacy_settings = {}
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name in QEMU_SETTINGS.keys():
            if settings.contains(name):
                legacy_settings[name] = settings.value(
                    name, type=QEMU_SETTING_TYPES[name])
        settings.remove("")
        settings.endGroup()

        if legacy_settings:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             legacy_settings)
        self._settings = local_config.loadSectionSettings(
            self.__class__.__name__, QEMU_SETTINGS)

        # keep the config file sync
        self._saveSettings()
Esempio n. 5
0
    def _loadSettings(self):
        """
        Loads the settings from the persistent settings file.
        """

        local_config = LocalConfig.instance()

        # restore the VPCS settings from QSettings (for backward compatibility)
        legacy_settings = {}
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name in VPCS_SETTINGS.keys():
            if settings.contains(name):
                legacy_settings[name] = settings.value(
                    name, type=VPCS_SETTING_TYPES[name])
        settings.remove("")
        settings.endGroup()

        if legacy_settings:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             legacy_settings)
        self._settings = local_config.loadSectionSettings(
            self.__class__.__name__, VPCS_SETTINGS)

        if not self._settings["base_script_file"]:
            self._settings["base_script_file"] = get_default_base_config(
                get_resource(os.path.join("configs", "vpcs_base_config.txt")))

        if not os.path.exists(self._settings["vpcs_path"]):
            self._settings["vpcs_path"] = self._findVPCS(self)

        # keep the config file sync
        self._saveSettings()
Esempio n. 6
0
    def __init__(self):

        QtGui.QWidget.__init__(self)
        self.setupUi(self)
        self._remote_servers = {}

        # Load the pre-configured console commands
        for name, cmd in sorted(PRECONFIGURED_TELNET_CONSOLE_COMMANDS.items()):
            self.uiTelnetConsolePreconfiguredCommandComboBox.addItem(name, cmd)
        for name, cmd in sorted(PRECONFIGURED_SERIAL_CONSOLE_COMMANDS.items()):
            self.uiSerialConsolePreconfiguredCommandComboBox.addItem(name, cmd)

        # Display the path of the settings file
        settings = QtCore.QSettings()
        self.uiConfigurationFileLabel.setText(settings.fileName())

        self.uiProjectsPathToolButton.clicked.connect(self._projectsPathSlot)
        self.uiImagesPathToolButton.clicked.connect(self._imagesPathSlot)
        self.uiTemporaryFilesPathToolButton.clicked.connect(
            self._temporaryFilesPathSlot)
        self.uiImportConfigurationFilePushButton.clicked.connect(
            self._importConfigurationFileSlot)
        self.uiExportConfigurationFilePushButton.clicked.connect(
            self._exportConfigurationFileSlot)
        self.uiTelnetConsolePreconfiguredCommandPushButton.clicked.connect(
            self._telnetConsolePreconfiguredCommandSlot)
        self.uiSerialConsolePreconfiguredCommandPushButton.clicked.connect(
            self._serialConsolePreconfiguredCommandSlot)
Esempio n. 7
0
    def _loadSettings(self):
        """
        Loads the settings from the server settings file.
        """

        local_config = LocalConfig.instance()

        # restore the VirtualBox settings from QSettings (for backward compatibility)
        legacy_settings = {}
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name in VBOX_SETTINGS.keys():
            if settings.contains(name):
                legacy_settings[name] = settings.value(
                    name, type=VBOX_SETTING_TYPES[name])
        settings.remove("")
        settings.endGroup()

        if legacy_settings:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             legacy_settings)
        self._settings = local_config.loadSectionSettings(
            self.__class__.__name__, VBOX_SETTINGS)

        if not os.path.exists(self._settings["vboxmanage_path"]):
            self._settings["vboxmanage_path"] = self._findVBoxManage(self)

        # keep the config file sync
        self._saveSettings()
Esempio n. 8
0
    def _importConfigurationFileSlot(self):
        """
        Slot to import a configuration file.
        """

        settings = QtCore.QSettings()
        configuration_file_path = settings.fileName()
        directory = os.path.dirname(configuration_file_path)

        path = QtGui.QFileDialog.getOpenFileName(
            self, "Import configuration file", directory,
            "Configuration file (*.conf);;All files (*.*)")
        if not path:
            return

        try:
            shutil.copyfile(path, configuration_file_path)
        except (shutil.Error, IOError) as e:
            QtGui.QMessageBox.critical(
                self, "Import configuration file",
                "Cannot import configuration file: {}".format(e))
            return

        QtGui.QMessageBox.information(
            self, "Configuration file",
            "Configuration file imported, default settings will be applied after a restart"
        )

        # restart the application
        from ..main_window import MainWindow
        main_window = MainWindow.instance()
        main_window.reboot_signal.emit()
Esempio n. 9
0
    def _loadSettings(self):
        """
        Loads the settings from the persistent settings file.
        """

        local_config = LocalConfig.instance()

        # restore the IOU settings from QSettings (for backward compatibility)
        legacy_settings = {}
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name in IOU_SETTINGS.keys():
            if settings.contains(name):
                legacy_settings[name] = settings.value(
                    name, type=IOU_SETTING_TYPES[name])
        if "iourc" in legacy_settings:
            legacy_settings["iourc_path"] = legacy_settings["iourc"]
            del legacy_settings["iourc"]
        settings.remove("")
        settings.endGroup()

        if legacy_settings:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             legacy_settings)
        self._settings = local_config.loadSectionSettings(
            self.__class__.__name__, IOU_SETTINGS)

        if sys.platform.startswith("linux") and not os.path.exists(
                self._settings["iouyap_path"]):
            iouyap_path = shutil.which("iouyap")
            if iouyap_path:
                self._settings["iouyap_path"] = iouyap_path

        # keep the config file sync
        self._saveSettings()
Esempio n. 10
0
    def _loadSettings(self):
        """
        Loads the settings from the persistent settings file.
        """

        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name, value in VPCS_SETTINGS.items():
            self._settings[name] = settings.value(name, value, type=VPCS_SETTING_TYPES[name])
        settings.endGroup()
Esempio n. 11
0
    def _saveSettings(self):
        """
        Saves the settings to the persistent settings file.
        """

        # save the settings
        settings = QtCore.QSettings()
        settings.beginGroup(self.__class__.__name__)
        for name, value in self._settings.items():
            settings.setValue(name, value)
        settings.endGroup()
Esempio n. 12
0
    def _scriptFileBrowserSlot(self):
        """
        Slot to open a file browser and select a base script file for VPCS
        """

        config_dir = os.path.join(os.path.dirname(QtCore.QSettings().fileName()), "base_configs")
        path = QtGui.QFileDialog.getOpenFileName(self, "Select a script file", config_dir)
        if not path:
            return

        if not os.access(path, os.R_OK):
            QtGui.QMessageBox.critical(self, "Script file", "{} cannot be read".format(os.path.basename(path)))
            return

        self.uiScriptFileEdit.setText(os.path.normpath(path))
Esempio n. 13
0
    def __init__(self):

        self._config = configparser.ConfigParser()
        if sys.platform.startswith("win"):
            filename = "gns3_server.ini"
        else:
            filename = "gns3_server.conf"
        self._config_file = os.path.join(
            os.path.dirname(QtCore.QSettings().fileName()), filename)
        try:
            # create the config file if it doesn't exist
            open(self._config_file, 'a').close()
        except OSError as e:
            log.error("Could not create the local server configuration {}: {}".
                      format(self._config_file, e))
        self.readConfig()
Esempio n. 14
0
    def _privateConfigBrowserSlot(self):
        """
        Slot to open a file browser and select a private-config file.
        """

        config_dir = os.path.join(os.path.dirname(QtCore.QSettings().fileName()), "base_configs")
        path = QtGui.QFileDialog.getOpenFileName(self, "Select a private configuration", config_dir)
        if not path:
            return

        if not os.access(path, os.R_OK):
            QtGui.QMessageBox.critical(self, "Private configuration", "Cannot read {}".format(path))
            return

        self.uiPrivateConfigLineEdit.clear()
        self.uiPrivateConfigLineEdit.setText(path)
Esempio n. 15
0
    def _loadIOUDevices(self):
        """
        Load the IOU devices from the persistent settings file.
        """

        local_config = LocalConfig.instance()

        # restore the VirtualBox settings from QSettings (for backward compatibility)
        iou_devices = []
        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup("IOUDevices")
        # load the IOU devices
        size = settings.beginReadArray("iou_device")
        for index in range(0, size):
            settings.setArrayIndex(index)
            device = {}
            for setting_name, default_value in IOU_DEVICE_SETTINGS.items():
                device[setting_name] = settings.value(
                    setting_name, default_value,
                    IOU_DEVICE_SETTING_TYPES[setting_name])
            iou_devices.append(device)
        settings.endArray()
        settings.remove("")
        settings.endGroup()

        if iou_devices:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             {"devices": iou_devices})

        settings = local_config.settings()
        if "devices" in settings.get(self.__class__.__name__, {}):
            for device in settings[self.__class__.__name__]["devices"]:
                name = device.get("name")
                server = device.get("server")
                key = "{server}:{name}".format(server=server, name=name)
                if key in self._iou_devices or not name or not server:
                    continue
                device_settings = IOU_DEVICE_SETTINGS.copy()
                device_settings.update(device)
                self._iou_devices[key] = device_settings

        # keep things sync
        self._saveIOUDevices()
Esempio n. 16
0
    def _loadVirtualBoxVMs(self):
        """
        Load the VirtualBox VMs from the client settings file.
        """

        local_config = LocalConfig.instance()

        # restore the VirtualBox settings from QSettings (for backward compatibility)
        virtualbox_vms = []
        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup("VirtualBoxVMs")
        # load the VMs
        size = settings.beginReadArray("VM")
        for index in range(0, size):
            settings.setArrayIndex(index)
            vm = {}
            for setting_name, default_value in VBOX_VM_SETTINGS.items():
                vm[setting_name] = settings.value(
                    setting_name, default_value,
                    VBOX_VM_SETTING_TYPES[setting_name])
            virtualbox_vms.append(vm)
        settings.endArray()
        settings.remove("")
        settings.endGroup()

        if virtualbox_vms:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             {"vms": virtualbox_vms})

        settings = local_config.settings()
        if "vms" in settings.get(self.__class__.__name__, {}):
            for vm in settings[self.__class__.__name__]["vms"]:
                vmname = vm.get("vmname")
                server = vm.get("server")
                key = "{server}:{vmname}".format(server=server, vmname=vmname)
                if key in self._virtualbox_vms or not vmname or not server:
                    continue
                vm_settings = VBOX_VM_SETTINGS.copy()
                vm_settings.update(vm)
                self._virtualbox_vms[key] = vm_settings

        # keep things sync
        self._saveVirtualBoxVMs()
Esempio n. 17
0
    def _loadQemuVMs(self):
        """
        Load the QEMU VMs from the persistent settings file.
        """

        local_config = LocalConfig.instance()

        # restore the Qemu settings from QSettings (for backward compatibility)
        qemu_vms = []
        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup("QemuVMs")
        # load the QEMU VMs
        size = settings.beginReadArray("vm")
        for index in range(0, size):
            settings.setArrayIndex(index)
            vm = {}
            for setting_name, default_value in QEMU_VM_SETTINGS.items():
                vm[setting_name] = settings.value(
                    setting_name, default_value,
                    QEMU_VM_SETTING_TYPES[setting_name])
            qemu_vms.append(vm)
        settings.endArray()
        settings.remove("")
        settings.endGroup()

        if qemu_vms:
            local_config.saveSectionSettings(self.__class__.__name__,
                                             {"vms": qemu_vms})

        settings = local_config.settings()
        if "vms" in settings.get(self.__class__.__name__, {}):
            for vm in settings[self.__class__.__name__]["vms"]:
                name = vm.get("name")
                server = vm.get("server")
                key = "{server}:{name}".format(server=server, name=name)
                if key in self._qemu_vms or not name or not server:
                    continue
                vm_settings = QEMU_VM_SETTINGS.copy()
                vm_settings.update(vm)
                self._qemu_vms[key] = vm_settings

        # keep things sync
        self._saveQemuVMs()
Esempio n. 18
0
    def _saveIOUImages(self):
        """
        Saves the IOU images to the persistent settings file.
        """

        # save the settings
        settings = QtCore.QSettings()
        settings.beginGroup("IOUImages")
        settings.remove("")

        # save the IOU images
        settings.beginWriteArray("iou_image", len(self._iou_images))
        index = 0
        for ios_image in self._iou_images.values():
            settings.setArrayIndex(index)
            for name, value in ios_image.items():
                settings.setValue(name, value)
            index += 1
        settings.endArray()
        settings.endGroup()
Esempio n. 19
0
    def _exportConfigurationFileSlot(self):
        """
        Slot to export a configuration file.
        """

        settings = QtCore.QSettings()
        configuration_file_path = settings.fileName()
        directory = os.path.dirname(configuration_file_path)

        path = QtGui.QFileDialog.getSaveFileName(
            self, "Import configuration file", directory,
            "Configuration file (*.conf);;All files (*.*)")
        if not path:
            return

        try:
            shutil.copyfile(configuration_file_path, path)
        except (shutil.Error, IOError) as e:
            QtGui.QMessageBox.critical(
                self, "Export configuration file",
                "Cannot export configuration file: {}".format(e))
            return
Esempio n. 20
0
    def _loadIOSImages(self):
        """
        Load the IOS images from the persistent settings file.
        """

        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup("IOSImages")

        # load the IOS images
        size = settings.beginReadArray("ios_image")
        for index in range(0, size):
            settings.setArrayIndex(index)
            path = settings.value("path", "")
            image = settings.value("image", "")
            startup_config = settings.value("startup_config", "")
            private_config = settings.value("private_config", "")
            platform = settings.value("platform", "")
            chassis = settings.value("chassis", "")
            idlepc = settings.value("idlepc", "")
            ram = settings.value("ram", 128, type=int)
            server = settings.value("server", "local")  # TODO: remote servers

            key = "{server}:{image}".format(server=server, image=image)
            self._ios_images[key] = {
                "path": path,
                "image": image,
                "startup_config": startup_config,
                "private_config": private_config,
                "platform": platform,
                "chassis": chassis,
                "idlepc": idlepc,
                "ram": ram,
                "server": server
            }

        settings.endArray()
        settings.endGroup()
Esempio n. 21
0
    def _loadIOSRouters(self):
        """
        Load the IOS routers from the persistent settings file.
        """

        local_config = LocalConfig.instance()

        # restore the Dynamips VM settings from QSettings (for backward compatibility)
        ios_routers = []
        # load the settings
        settings = QtCore.QSettings()
        settings.beginGroup("IOSRouters")
        # load the VMs
        size = settings.beginReadArray("ios_router")
        for index in range(0, size):
            settings.setArrayIndex(index)
            router = {}
            for setting_name, default_value in IOS_ROUTER_SETTINGS.items():
                router[setting_name] = settings.value(setting_name, default_value, IOS_ROUTER_SETTING_TYPES[setting_name])

            for slot_id in range(0, 7):
                slot = "slot{}".format(slot_id)
                if settings.contains(slot):
                    router[slot] = settings.value(slot, "")

            for wic_id in range(0, 3):
                wic = "wic{}".format(wic_id)
                if settings.contains(wic):
                    router[wic] = settings.value(wic, "")

            platform = router["platform"]
            chassis = router["chassis"]

            if platform == "c7200":
                router["midplane"] = settings.value("midplane", "vxr")
                router["npe"] = settings.value("npe", "npe-400")
                router["slot0"] = settings.value("slot0", "C7200-IO-FE")
            else:
                router["iomem"] = 5

            if platform in ("c3725", "c3725", "c2691"):
                router["slot0"] = settings.value("slot0", "GT96100-FE")
            elif platform == "c3600" and chassis == "3660":
                router["slot0"] = settings.value("slot0", "Leopard-2FE")
            elif platform == "c2600" and chassis == "2610":
                router["slot0"] = settings.value("slot0", "C2600-MB-1E")
            elif platform == "c2600" and chassis == "2611":
                router["slot0"] = settings.value("slot0", "C2600-MB-2E")
            elif platform == "c2600" and chassis in ("2620", "2610XM", "2620XM", "2650XM"):
                router["slot0"] = settings.value("slot0", "C2600-MB-1FE")
            elif platform == "c2600" and chassis in ("2621", "2611XM", "2621XM", "2651XM"):
                router["slot0"] = settings.value("slot0", "C2600-MB-2FE")
            elif platform == "c1700" and chassis in ("1720", "1721", "1750", "1751", "1760"):
                router["slot0"] = settings.value("slot0", "C1700-MB-1FE")
            elif platform == "c1700" and chassis in ("1751", "1760"):
                router["slot0"] = settings.value("slot0", "C1700-MB-WIC1")

            ios_routers.append(router)

        settings.endArray()
        settings.remove("")
        settings.endGroup()

        if ios_routers:
            local_config.saveSectionSettings(self.__class__.__name__, {"routers": ios_routers})

        settings = local_config.settings()
        if "routers" in settings.get(self.__class__.__name__, {}):
            for router in settings[self.__class__.__name__]["routers"]:
                name = router.get("name")
                server = router.get("server")
                key = "{server}:{name}".format(server=server, name=name)
                if key in self._ios_routers or not name or not server:
                    continue
                self._ios_routers[key] = router

        # keep things sync
        self._saveIOSRouters()
Esempio n. 22
0
def main():
    """
    Entry point for GNS3 GUI.
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("project",
                        help="load a GNS3 project (.gns3)",
                        metavar="path",
                        nargs="?")
    parser.add_argument("--version",
                        help="show the version",
                        action="version",
                        version=__version__)
    parser.add_argument("--debug",
                        help="print out debug messages",
                        action="store_true",
                        default=False)
    options = parser.parse_args()
    exception_file_path = "exceptions.log"

    if options.project and hasattr(sys, "frozen"):
        os.chdir(os.path.dirname(os.path.abspath(sys.executable)))

    def exceptionHook(exception, value, tb):

        if exception == KeyboardInterrupt:
            sys.exit(0)

        lines = traceback.format_exception(exception, value, tb)
        print(
            "****** Exception detected, traceback information saved in {} ******"
            .format(exception_file_path))
        print(
            "\nPLEASE REPORT ON https://community.gns3.com/community/support/bug\n"
        )
        print("".join(lines))
        try:
            curdate = time.strftime("%d %b %Y %H:%M:%S")
            logfile = open(exception_file_path, "a")
            logfile.write("=== GNS3 {} traceback on {} ===\n".format(
                __version__, curdate))
            logfile.write("".join(lines))
            logfile.close()
        except OSError as e:
            print("Could not save traceback to {}: {}".format(
                os.path.normpath(exception_file_path), e))

        if not sys.stdout.isatty():
            # if stdout is not a tty (redirected to the console view),
            # then print the exception on stderr too.
            print("".join(lines), file=sys.stderr)
        CrashReport.instance().captureException(exception, value, tb)

    # catch exceptions to write them in a file
    sys.excepthook = exceptionHook

    current_year = datetime.date.today().year
    print("GNS3 GUI version {}".format(__version__))
    print("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year))

    # we only support Python 2 version >= 2.7 and Python 3 version >= 3.3
    if sys.version_info < (2, 7):
        raise RuntimeError("Python 2.7 or higher is required")
    elif sys.version_info[0] == 3 and sys.version_info < (3, 3):
        raise RuntimeError("Python 3.3 or higher is required")

    def version(version_string):
        return [int(i) for i in version_string.split('.')]

    if version(QtCore.QT_VERSION_STR) < version("4.6"):
        raise RuntimeError(
            "Requirement is Qt version 4.6 or higher, got version {}".format(
                QtCore.QT_VERSION_STR))

    # 4.8.3 because of QSettings (http://pyqt.sourceforge.net/Docs/PyQt4/pyqt_qsettings.html)
    if DEFAULT_BINDING == "PyQt" and version(
            QtCore.BINDING_VERSION_STR) < version("4.8.3"):
        raise RuntimeError(
            "Requirement is PyQt version 4.8.3 or higher, got version {}".
            format(QtCore.BINDING_VERSION_STR))

    if DEFAULT_BINDING == "PySide" and version(
            QtCore.BINDING_VERSION_STR) < version("1.0"):
        raise RuntimeError(
            "Requirement is PySide version 1.0 or higher, got version {}".
            format(QtCore.BINDING_VERSION_STR))

    # check for the correct locale
    # (UNIX/Linux only)
    locale_check()

    try:
        os.getcwd()
    except FileNotFoundError:
        log.critical("the current working directory doesn't exist")
        return

    # always use the INI format on Windows and OSX (because we don't like the registry and plist files)
    if sys.platform.startswith('win') or sys.platform.startswith('darwin'):
        QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)

    if sys.platform.startswith('win') and hasattr(sys, "frozen"):
        try:
            import win32console
            import win32con
            import win32gui
        except ImportError:
            raise RuntimeError(
                "Python for Windows extensions must be installed.")

        if not options.debug:
            try:
                # hide the console
                console_window = win32console.GetConsoleWindow()
                win32gui.ShowWindow(console_window, win32con.SW_HIDE)
            except win32console.error as e:
                print("warning: could not allocate console: {}".format(e))

    app = QtGui.QApplication(sys.argv)

    # this info is necessary for QSettings
    app.setOrganizationName("GNS3")
    app.setOrganizationDomain("gns3.net")
    app.setApplicationName("GNS3")
    app.setApplicationVersion(__version__)

    formatter = logging.Formatter(
        "[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] %(message)s",
        datefmt="%y%m%d %H:%M:%S")

    # on debug enable logging to stdout
    if options.debug:
        root_logger = init_logger(logging.DEBUG)
    else:
        root_logger = init_logger(logging.INFO)

    # save client logging info to a file
    logfile = os.path.join(os.path.dirname(QtCore.QSettings().fileName()),
                           "gns3_gui.log")
    try:
        try:
            os.makedirs(os.path.dirname(QtCore.QSettings().fileName()))
        except FileExistsError:
            pass
        handler = logging.FileHandler(logfile, "w")
        root_logger.addHandler(handler)
    except OSError as e:
        log.warn("could not log to {}: {}".format(logfile, e))

    log.info('Log level: {}'.format(
        logging.getLevelName(log.getEffectiveLevel())))

    # update the exception file path to have it in the same directory as the settings file.
    exception_file_path = os.path.join(
        os.path.dirname(QtCore.QSettings().fileName()), exception_file_path)

    mainwindow = MainWindow(options.project)
    mainwindow.show()
    exit_code = app.exec_()
    delattr(MainWindow, "_instance")
    app.deleteLater()

    sys.exit(exit_code)
Esempio n. 23
0
def main():
    """
    Entry point for GNS3 GUI.
    """

    parser = argparse.ArgumentParser()
    parser.add_argument('--version', help="show the version", action='version', version=__version__)
    parser.parse_args()
    exception_file_path = "exception.log"

    def exceptionHook(exception, value, tb):

        if exception == KeyboardInterrupt:
            sys.exit(0)

        lines = traceback.format_exception(exception, value, tb)
        print("****** Exception detected, traceback information saved in {} ******".format(exception_file_path))
        print("\nPLEASE REPORT ON http://forum.gns3.net/development-f14.html OR http://github.com/GNS3/gns3-gui/issues\n")
        print("".join(lines))
        try:
            curdate = time.strftime("%d %b %Y %H:%M:%S")
            logfile = open(exception_file_path, "a")
            logfile.write("=== GNS3 {} traceback on {} ===\n".format(__version__, curdate))
            logfile.write("".join(lines))
            logfile.close()
        except OSError as e:
            print("Could not save traceback to {}: {}".format(exception_file_path, e))

        if not sys.stdout.isatty():
            # if stdout is not a tty (redirected to the console view),
            # then print the exception on stderr too.
            print("".join(lines), file=sys.stderr)

    # catch exceptions to write them in a file
    sys.excepthook = exceptionHook

    current_year = datetime.date.today().year
    print("GNS3 GUI version {}".format(__version__))
    print("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year))

    # we only support Python 2 version >= 2.7 and Python 3 version >= 3.3
    if sys.version_info < (2, 7):
        raise RuntimeError("Python 2.7 or higher is required")
    elif sys.version_info[0] == 3 and sys.version_info < (3, 3):
        raise RuntimeError("Python 3.3 or higher is required")

    version = lambda version_string: [int(i) for i in version_string.split('.')]

    if version(QtCore.QT_VERSION_STR) < version("4.6"):
        raise RuntimeError("Requirement is Qt version 4.6 or higher, got version {}".format(QtCore.QT_VERSION_STR))

    # 4.8.3 because of QSettings (http://pyqt.sourceforge.net/Docs/PyQt4/pyqt_qsettings.html)
    if DEFAULT_BINDING == "PyQt" and version(QtCore.BINDING_VERSION_STR) < version("4.8.3"):
        raise RuntimeError("Requirement is PyQt version 4.8.3 or higher, got version {}".format(QtCore.BINDING_VERSION_STR))

    if DEFAULT_BINDING == "PySide" and version(QtCore.BINDING_VERSION_STR) < version("1.0"):
        raise RuntimeError("Requirement is PySide version 1.0 or higher, got version {}".format(QtCore.BINDING_VERSION_STR))

    try:
        # if tornado is present then enable pretty logging.
        import tornado.log
        tornado.log.enable_pretty_logging()
    except ImportError:
        pass

    # check for the correct locale
    # (UNIX/Linux only)
    locale_check()

    try:
        os.getcwd()
    except FileNotFoundError:
        log.critical("the current working directory doesn't exist")
        return

    # always use the INI format on Windows and OSX (because we don't like the registry and plist files)
    if sys.platform.startswith('win') or sys.platform.startswith('darwin'):
        QtCore.QSettings.setDefaultFormat(QtCore.QSettings.IniFormat)

    if sys.platform.startswith('win'):
        try:
            import win32console
            import win32con
            import win32gui
        except ImportError:
            raise RuntimeError("Python for Windows extensions must be installed.")

        try:
            win32console.AllocConsole()
            #FIXME: do not hide the console for alpha releases to help with debugging
            #console_window = win32console.GetConsoleWindow()
            #win32gui.ShowWindow(console_window, win32con.SW_HIDE)
        except win32console.error as e:
            print("warning: could not allocate console: {}".format(e))

    exit_code = MainWindow.exit_code_reboot
    while exit_code == MainWindow.exit_code_reboot:

        exit_code = 0
        app = QtGui.QApplication(sys.argv)

        # this info is necessary for QSettings
        app.setOrganizationName("GNS3")
        app.setOrganizationDomain("gns3.net")
        app.setApplicationName("GNS3")
        app.setApplicationVersion(__version__)

        # save client logging info to a file
        logfile = os.path.join(os.path.dirname(QtCore.QSettings().fileName()), "GNS3_client.log")
        try:
            logger = logging.getLogger("gns3")
            try:
                os.makedirs(os.path.dirname(QtCore.QSettings().fileName()))
            except FileExistsError:
                pass
            handler = logging.FileHandler(logfile, "w")
            handler.setLevel(logging.INFO)
            formatter = logging.Formatter("[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] %(message)s",
                                          datefmt="%y%m%d %H:%M:%S")
            handler.setFormatter(formatter)
            logger.addHandler(handler)
        except OSError as e:
            log.warn("could not log to {}: {}".format(logfile, e))

        # update the exception file path to have it in the same directory as the settings file.
        exception_file_path = os.path.join(os.path.dirname(QtCore.QSettings().fileName()), exception_file_path)

        mainwindow = MainWindow.instance()
        mainwindow.show()
        exit_code = app.exec_()
        delattr(MainWindow, "_instance")
        app.deleteLater()

    sys.exit(exit_code)