Ejemplo n.º 1
0
    def askCopyUploadImage(self, parent, source_path, server, node_type):
        """
        Ask user for copying the image to the default directory or upload
        it to remote server.

        :param parent: Parent window
        :param path: File path on computer
        :param server: The server where the images should be located
        :param node_type: Remote upload endpoint
        :returns path: Final path
        """

        if (server and server != "local") or Controller.instance().isRemote():
            return self._uploadImageToRemoteServer(source_path, server, node_type)
        else:
            destination_directory = self.getDirectoryForType(node_type)
            destination_path = os.path.join(destination_directory, os.path.basename(source_path))
            source_filename = os.path.basename(source_path)
            destination_filename = os.path.basename(destination_path)
            if os.path.normpath(os.path.dirname(source_path)) != destination_directory:
                # the image is not in the default images directory
                if source_filename == destination_filename:
                    # the filename already exists in the default images directory
                    source_image = Image(node_type, source_path, filename=source_filename)
                    destination_image = Image(node_type, destination_path, filename=destination_filename)
                    try:
                        if source_image.md5sum == destination_image.md5sum:
                            # the source and destination images are identical
                            return source_path
                    except OSError as e:
                        QtWidgets.QMessageBox.critical(parent, 'Image', 'Cannot compare image file {} with {}: {}.'.format(source_path, destination_path, str(e)))
                        return source_path
                    # find a new unique path to avoid overwriting existing destination file
                    destination_path = self._getUniqueDestinationPath(source_image, node_type, destination_path)

                reply = QtWidgets.QMessageBox.question(parent,
                                                       'Image',
                                                       'Would you like to copy {} to the default images directory'.format(source_filename),
                                                       QtWidgets.QMessageBox.Yes,
                                                       QtWidgets.QMessageBox.No)
                if reply == QtWidgets.QMessageBox.Yes:
                    try:
                        os.makedirs(destination_directory, exist_ok=True)
                    except OSError as e:
                        QtWidgets.QMessageBox.critical(parent, 'Image', 'Could not create destination directory {}: {}'.format(destination_directory, str(e)))
                        return source_path

                    worker = FileCopyWorker(source_path, destination_path)
                    progress_dialog = ProgressDialog(worker, 'Image', 'Copying {}'.format(source_filename), 'Cancel', busy=True, parent=parent)
                    progress_dialog.show()
                    progress_dialog.exec_()
                    errors = progress_dialog.errors()
                    if errors:
                        QtWidgets.QMessageBox.critical(parent, 'Image', '{}'.format(''.join(errors)))
                        return source_path
                    else:
                        source_path = destination_path
            return source_path
Ejemplo n.º 2
0
    def askCopyUploadImage(self, parent, source_path, server, node_type):
        """
        Ask user for copying the image to the default directory or upload
        it to remote server.

        :param parent: Parent window
        :param path: File path on computer
        :param server: The server where the images should be located
        :param node_type: Remote upload endpoint
        :returns path: Final path
        """

        if (server and server != "local") or Controller.instance().isRemote():
            return self._uploadImageToRemoteServer(source_path, server, node_type)
        else:
            destination_directory = self.getDirectoryForType(node_type)
            destination_path = os.path.join(destination_directory, os.path.basename(source_path))
            source_filename = os.path.basename(source_path)
            destination_filename = os.path.basename(destination_path)
            if os.path.normpath(os.path.dirname(source_path)) != destination_directory:
                # the image is not in the default images directory
                if source_filename == destination_filename:
                    # the filename already exists in the default images directory
                    source_image = Image(node_type, source_path, filename=source_filename)
                    destination_image = Image(node_type, destination_path, filename=destination_filename)
                    try:
                        if source_image.md5sum == destination_image.md5sum:
                            # the source and destination images are identical
                            return source_path
                    except OSError as e:
                        QtWidgets.QMessageBox.critical(parent, 'Image', 'Cannot compare image file {} with {}: {}.'.format(source_path, destination_path, str(e)))
                        return source_path
                    # find a new unique path to avoid overwriting existing destination file
                    destination_path = self._getUniqueDestinationPath(source_image, node_type, destination_path)

                reply = QtWidgets.QMessageBox.question(parent,
                                                       'Image',
                                                       'Would you like to copy {} to the default images directory'.format(source_filename),
                                                       QtWidgets.QMessageBox.Yes,
                                                       QtWidgets.QMessageBox.No)
                if reply == QtWidgets.QMessageBox.Yes:
                    try:
                        os.makedirs(destination_directory, exist_ok=True)
                    except OSError as e:
                        QtWidgets.QMessageBox.critical(parent, 'Image', 'Could not create destination directory {}: {}'.format(destination_directory, str(e)))
                        return source_path

                    worker = FileCopyWorker(source_path, destination_path)
                    progress_dialog = ProgressDialog(worker, 'Image', 'Copying {}'.format(source_filename), 'Cancel', busy=True, parent=parent)
                    progress_dialog.show()
                    progress_dialog.exec_()
                    errors = progress_dialog.errors()
                    if errors:
                        QtWidgets.QMessageBox.critical(parent, 'Image', '{}'.format(''.join(errors)))
                        return source_path
                    else:
                        source_path = destination_path
            return source_path
Ejemplo n.º 3
0
    def getDiskImage(parent):

        destination_directory = os.path.join(
            MainWindow.instance().imagesDirPath(), "QEMU")
        path, _ = QtGui.QFileDialog.getOpenFileNameAndFilter(
            parent, "Select a QEMU disk image", destination_directory)
        if not path:
            return

        if not os.access(path, os.R_OK):
            QtGui.QMessageBox.critical(parent, "QEMU disk image",
                                       "Cannot read {}".format(path))
            return

        try:
            os.makedirs(destination_directory)
        except FileExistsError:
            pass
        except OSError as e:
            QtGui.QMessageBox.critical(
                parent, "QEMU disk images directory",
                "Could not create the QEMU disk images directory {}: {}".
                format(destination_directory, e))
            return

        if os.path.normpath(os.path.dirname(path)) != destination_directory:
            # the QEMU disk image is not in the default images directory
            reply = QtGui.QMessageBox.question(
                parent, "QEMU disk image",
                "Would you like to copy {} to the default images directory".
                format(os.path.basename(path)), QtGui.QMessageBox.Yes,
                QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.Yes:
                destination_path = os.path.join(destination_directory,
                                                os.path.basename(path))
                thread = FileCopyThread(path, destination_path)
                progress_dialog = ProgressDialog(thread,
                                                 "QEMU disk image",
                                                 "Copying {}".format(
                                                     os.path.basename(path)),
                                                 "Cancel",
                                                 busy=True,
                                                 parent=parent)
                thread.deleteLater()
                progress_dialog.show()
                progress_dialog.exec_()
                errors = progress_dialog.errors()
                if errors:
                    QtGui.QMessageBox.critical(parent, "QEMU disk image",
                                               "{}".format("".join(errors)))
                else:
                    path = destination_path

        return path
Ejemplo n.º 4
0
    def askCopyUploadImage(self, parent, path, server, node_type):
        """
        Ask user for copying the image to the default directory or upload
        it to remote server.

        :param parent: Parent window
        :param path: File path on computer
        :param server: The server where the images should be located
        :param node_type: Remote upload endpoint
        :returns path: Final path
        """

        if (server and server != "local") or Controller.instance().isRemote():
            return self._uploadImageToRemoteServer(path, server, node_type)
        else:
            destination_directory = self.getDirectoryForType(node_type)
            if os.path.normpath(
                    os.path.dirname(path)) != destination_directory:
                # the IOS image is not in the default images directory
                reply = QtWidgets.QMessageBox.question(
                    parent, 'Image',
                    'Would you like to copy {} to the default images directory'
                    .format(os.path.basename(path)), QtWidgets.QMessageBox.Yes,
                    QtWidgets.QMessageBox.No)
                if reply == QtWidgets.QMessageBox.Yes:
                    destination_path = os.path.join(destination_directory,
                                                    os.path.basename(path))
                    try:
                        os.makedirs(destination_directory, exist_ok=True)
                    except OSError as e:
                        QtWidgets.QMessageBox.critical(
                            parent, 'Image',
                            'Could not create destination directory {}: {}'.
                            format(destination_directory, str(e)))
                        return path
                    worker = FileCopyWorker(path, destination_path)
                    progress_dialog = ProgressDialog(
                        worker,
                        'Image',
                        'Copying {}'.format(os.path.basename(path)),
                        'Cancel',
                        busy=True,
                        parent=parent)
                    progress_dialog.show()
                    progress_dialog.exec_()
                    errors = progress_dialog.errors()
                    if errors:
                        QtWidgets.QMessageBox.critical(
                            parent, 'Image', '{}'.format(''.join(errors)))
                        return path
                    else:
                        path = destination_path
            return path
Ejemplo n.º 5
0
    def askCopyUploadImage(self, parent, path, server, vm_type):
        """
        Ask user for copying the image to the default directory or upload
        it to remote server.

        :param parent: Parent window
        :param path: File path on computer
        :param server: The server where the images should be located
        :param vm_type: Remote upload endpoint
        :returns path: Final path
        """

        if server and not server.isLocal():
            return self._uploadImageToRemoteServer(path, server, vm_type)
        else:
            destination_directory = self.getDirectoryForType(vm_type)
            if os.path.normpath(os.path.dirname(path)) != destination_directory:
                # the IOS image is not in the default images directory
                reply = QtWidgets.QMessageBox.question(parent,
                                                       'Image',
                                                       'Would you like to copy {} to the default images directory'.format(os.path.basename(path)),
                                                       QtWidgets.QMessageBox.Yes,
                                                       QtWidgets.QMessageBox.No)
                if reply == QtWidgets.QMessageBox.Yes:
                    destination_path = os.path.join(destination_directory, os.path.basename(path))
                    try:
                        os.makedirs(destination_directory, exist_ok=True)
                    except OSError as e:
                        QtWidgets.QMessageBox.critical(parent, 'Image', 'Could not create destination directory {}: {}'.format(destination_directory, str(e)))
                        return path
                    worker = FileCopyWorker(path, destination_path)
                    progress_dialog = ProgressDialog(worker, 'Image', 'Copying {}'.format(os.path.basename(path)), 'Cancel', busy=True, parent=parent)
                    progress_dialog.show()
                    progress_dialog.exec_()
                    errors = progress_dialog.errors()
                    if errors:
                        QtWidgets.QMessageBox.critical(parent, 'Image', '{}'.format(''.join(errors)))
                        return path
                    else:
                        path = destination_path
            return path
Ejemplo n.º 6
0
    def getIOUImage(parent):
        """

        :param parent: parent widget

        :return: path to the IOU image or None
        """

        destination_directory = os.path.join(
            MainWindow.instance().imagesDirPath(), "IOU")
        path, _ = QtGui.QFileDialog.getOpenFileNameAndFilter(
            parent, "Select an IOU image", destination_directory,
            "All files (*)", "IOU image (*.bin *.image)")

        if not path:
            return

        if not os.access(path, os.R_OK):
            QtGui.QMessageBox.critical(parent, "IOU image",
                                       "Cannot read {}".format(path))
            return

        try:
            with open(path, "rb") as f:
                # read the first 7 bytes of the file.
                elf_header_start = f.read(7)
        except OSError as e:
            QtGui.QMessageBox.critical(
                parent, "IOU image",
                "Cannot read ELF magic number: {}".format(e))
            return

        # file must start with the ELF magic number, be 32-bit, little endian and have an ELF version of 1
        # normal IOS image are big endian!
        if elf_header_start != b'\x7fELF\x01\x01\x01':
            QtGui.QMessageBox.critical(
                parent, "IOU image", "Sorry, this is not a valid IOU image!")
            return

        try:
            os.makedirs(destination_directory)
        except FileExistsError:
            pass
        except OSError as e:
            QtGui.QMessageBox.critical(
                parent, "IOU images directory",
                "Could not create the IOU images directory {}: {}".format(
                    destination_directory, e))
            return

        if os.path.normpath(os.path.dirname(path)) != destination_directory:
            # the IOU image is not in the default images directory
            reply = QtGui.QMessageBox.question(
                parent, "IOU image",
                "Would you like to copy {} to the default images directory".
                format(os.path.basename(path)), QtGui.QMessageBox.Yes,
                QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.Yes:
                destination_path = os.path.join(destination_directory,
                                                os.path.basename(path))
                thread = FileCopyThread(path, destination_path)
                progress_dialog = ProgressDialog(thread,
                                                 "Project",
                                                 "Copying {}".format(
                                                     os.path.basename(path)),
                                                 "Cancel",
                                                 busy=True,
                                                 parent=parent)
                thread.deleteLater()
                progress_dialog.show()
                progress_dialog.exec_()
                errors = progress_dialog.errors()
                if errors:
                    QtGui.QMessageBox.critical(parent, "IOS image",
                                               "{}".format("".join(errors)))
                else:
                    path = destination_path
                    mode = os.stat(path).st_mode
                    os.chmod(path, mode | stat.S_IXUSR)

        if not os.access(path, os.X_OK):
            QtGui.QMessageBox.warning(parent, "IOU image",
                                      "{} is not executable".format(path))

        return path
    def getIOSImage(parent):
        """

        :param parent: parent widget

        :return: path to the IOS image or None
        """

        destination_directory = os.path.join(
            MainWindow.instance().imagesDirPath(), "IOS")
        path, _ = QtGui.QFileDialog.getOpenFileNameAndFilter(
            parent, "Select an IOS image", destination_directory,
            "All files (*.*);;IOS image (*.bin *.image)",
            "IOS image (*.bin *.image)")
        if not path:
            return

        if not os.access(path, os.R_OK):
            QtGui.QMessageBox.critical(parent, "IOS image",
                                       "Cannot read {}".format(path))
            return

        if sys.platform.startswith('win'):
            # Dynamips (Cygwin acutally) doesn't like non ascii paths on Windows
            try:
                path.encode('ascii')
            except UnicodeEncodeError:
                QtGui.QMessageBox.warning(
                    parent, "IOS image",
                    "The IOS image filename should contains only ascii (English) characters."
                )

        try:
            with open(path, "rb") as f:
                # read the first 7 bytes of the file.
                elf_header_start = f.read(7)
        except OSError as e:
            QtGui.QMessageBox.critical(
                parent, "IOS image",
                "Cannot read ELF magic number: {}".format(e))
            return

        # file must start with the ELF magic number, be 32-bit, big endian and have an ELF version of 1
        if elf_header_start != b'\x7fELF\x01\x02\x01':
            QtGui.QMessageBox.critical(
                parent, "IOS image", "Sorry, this is not a valid IOS image!")
            return

        try:
            os.makedirs(destination_directory)
        except FileExistsError:
            pass
        except OSError as e:
            QtGui.QMessageBox.critical(
                parent, "IOS images directory",
                "Could not create the IOS images directory {}: {}".format(
                    destination_directory, e))
            return

        if isIOSCompressed(path):
            reply = QtGui.QMessageBox.question(
                parent, "IOS image",
                "Would you like to decompress this IOS image?",
                QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.Yes:
                decompressed_image_path = os.path.join(
                    destination_directory,
                    os.path.basename(os.path.splitext(path)[0] + ".image"))
                thread = DecompressIOSThread(path, decompressed_image_path)
                progress_dialog = ProgressDialog(
                    thread,
                    "IOS image",
                    "Decompressing IOS image {}...".format(
                        os.path.basename(path)),
                    "Cancel",
                    busy=True,
                    parent=parent)
                progress_dialog.show()
                if progress_dialog.exec_() is not False:
                    path = decompressed_image_path
                thread.wait()

        if os.path.normpath(os.path.dirname(path)) != destination_directory:
            # the IOS image is not in the default images directory
            reply = QtGui.QMessageBox.question(
                parent, "IOS image",
                "Would you like to copy {} to the default images directory".
                format(os.path.basename(path)), QtGui.QMessageBox.Yes,
                QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.Yes:
                destination_path = os.path.join(destination_directory,
                                                os.path.basename(path))
                thread = FileCopyThread(path, destination_path)
                progress_dialog = ProgressDialog(thread,
                                                 "IOS image",
                                                 "Copying {}".format(
                                                     os.path.basename(path)),
                                                 "Cancel",
                                                 busy=True,
                                                 parent=parent)
                thread.deleteLater()
                progress_dialog.show()
                progress_dialog.exec_()
                errors = progress_dialog.errors()
                if errors:
                    QtGui.QMessageBox.critical(parent, "IOS image",
                                               "{}".format("".join(errors)))
                else:
                    path = destination_path

        return path