PyInstallerInterface/PyInstallerExecDialog.py

Tue, 29 Oct 2024 17:39:59 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 29 Oct 2024 17:39:59 +0100
branch
eric7
changeset 56
02709629940d
parent 55
3794f1ca53af
child 58
8bfeb70bcdc3
permissions
-rw-r--r--

- changed to the new style header
- ensured proper parent relationship of modal dialogs
- included compiled form files

# -*- coding: utf-8 -*-

# Copyright (c) 2018 - 2024 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to show the output of the pyinstaller/pyi-makespec
process.
"""

import os

from PyQt6.QtCore import QProcess, QTimer, pyqtSlot
from PyQt6.QtWidgets import QAbstractButton, QDialog, QDialogButtonBox

from eric7 import Preferences
from eric7.EricWidgets import EricMessageBox

from .Ui_PyInstallerExecDialog import Ui_PyInstallerExecDialog


class PyInstallerExecDialog(QDialog, Ui_PyInstallerExecDialog):
    """
    Class implementing a dialog to show the output of the
    pyinstaller/pyi-makespec process.

    This class starts a QProcess and displays a dialog that
    shows the output of the packager command process.
    """

    def __init__(self, cmdname, parent=None):
        """
        Constructor

        @param cmdname name of the packager
        @type str
        @param parent reference to the parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)

        self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False)
        self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True)

        self.__process = None
        self.__cmdname = cmdname  # used for several outputs

    def start(self, args, project, script):
        """
        Public slot to start the packager command.

        @param args command line arguments for packager program
        @type list of str
        @param project reference to the project object
        @type Project
        @param script script or spec file name to be processed by by the
            packager
        @type str
        @return flag indicating the successful start of the process
        @rtype bool
        """
        self.__project = project

        if not os.path.isabs(script):
            # assume relative paths are relative to the project directory
            script = os.path.join(self.__project.getProjectPath(), script)
        dname = os.path.dirname(script)
        self.__script = os.path.basename(script)

        self.contents.clear()

        args.append(self.__script)

        self.__process = QProcess()
        self.__process.setWorkingDirectory(dname)

        self.__process.readyReadStandardOutput.connect(self.__readStdout)
        self.__process.readyReadStandardError.connect(self.__readStderr)
        self.__process.finished.connect(self.__finishedProcess)

        self.setWindowTitle(self.tr("{0} - {1}").format(self.__cmdname, script))
        self.contents.insertPlainText(
            "{0} {1}\n\n".format(" ".join(args), os.path.join(dname, self.__script))
        )
        self.contents.ensureCursorVisible()

        program = args.pop(0)
        self.__process.start(program, args)
        procStarted = self.__process.waitForStarted()
        if not procStarted:
            EricMessageBox.critical(
                self,
                self.tr("Process Generation Error"),
                self.tr(
                    "The process {0} could not be started. "
                    "Ensure, that it is in the search path."
                ).format(program),
            )
        return procStarted

    @pyqtSlot(QAbstractButton)
    def on_buttonBox_clicked(self, button):
        """
        Private slot called by a button of the button box clicked.

        @param button button that was clicked
        @type QAbstractButton
        """
        if button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close):
            self.accept()
        elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel):
            self.__finish()

    def __finish(self):
        """
        Private slot called when the process was canceled by the user.

        It is called when the process finished or
        the user pressed the cancel button.
        """
        if self.__process is not None:
            self.__process.disconnect(self.__finishedProcess)
            self.__process.terminate()
            QTimer.singleShot(2000, self.__process.kill)
            self.__process.waitForFinished(3000)
        self.__process = None

        self.contents.insertPlainText(
            self.tr("\n{0} aborted.\n").format(self.__cmdname)
        )

        self.__enableButtons()

    def __finishedProcess(self):
        """
        Private slot called when the process finished.

        It is called when the process finished or
        the user pressed the cancel button.
        """
        if self.__process.exitStatus() == QProcess.ExitStatus.NormalExit:
            # add the spec file to the project
            self.__project.addToOthers(os.path.splitext(self.__script)[0] + ".spec")

        self.__process = None

        self.contents.insertPlainText(
            self.tr("\n{0} finished.\n").format(self.__cmdname)
        )

        self.__enableButtons()

    def __enableButtons(self):
        """
        Private slot called when all processes finished.

        It is called when the process finished or
        the user pressed the cancel button.
        """
        self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
        self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
        self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
        self.contents.ensureCursorVisible()

    def __readStdout(self):
        """
        Private slot to handle the readyReadStandardOutput signal.

        It reads the output of the process, formats it and inserts it into
        the contents pane.
        """
        self.__process.setReadChannel(QProcess.ProcessChannel.StandardOutput)

        while self.__process.canReadLine():
            s = str(
                self.__process.readAllStandardOutput(),
                Preferences.getSystem("IOEncoding"),
                "replace",
            )
            self.contents.insertPlainText(s)
            self.contents.ensureCursorVisible()

    def __readStderr(self):
        """
        Private slot to handle the readyReadStandardError signal.

        It reads the error output of the process and inserts it into the
        error pane.
        """
        self.__process.setReadChannel(QProcess.ProcessChannel.StandardError)

        while self.__process.canReadLine():
            s = str(
                self.__process.readAllStandardError(),
                Preferences.getSystem("IOEncoding"),
                "replace",
            )
            self.contents.insertPlainText(s)
            self.contents.ensureCursorVisible()

eric ide

mercurial