Sun, 15 Sep 2024 16:56:58 +0200
Added a QProcess derived class with timeout and changed the pip interface to use that class.
10919
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
3 | # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de> |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing a QProcess derived class with a timeout and convenience signals. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | from PyQt6.QtCore import QProcess, QTimer, pyqtSignal, pyqtSlot |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | class EricProcess(QProcess): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
15 | Class implementing a QProcess derived class with a timeout and convenience signals |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
16 | succeeded and failed. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
17 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
18 | @signal failed() emitted to indicate a process failure |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | @signal succeeded() emitted to indicate that the process finished successfully |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
20 | @signal timeout() emitted to indicate the expiry of the configured timeout value |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
22 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | failed = pyqtSignal() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24 | succeeded = pyqtSignal() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | timeout = pyqtSignal() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
27 | def __init__(self, timeout=30000, parent=None): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
28 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | Constructor |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | @param timeout timeout value in milliseconds. If the process does not finish |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | within this interval, it is killed. (defaults to 30000) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | @type int (optional) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | @param parent reference to the parent object (defaults to None) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | @type QObject (optional) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | super().__init__(parent=parent) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | self.started.connect(self.__started) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | self.finished.connect(self.__finished) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | self.__timeoutTimer = QTimer(self) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | self.__timeoutTimer.setInterval(timeout) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | self.__timeoutTimer.timeout.connect(self.__timeout) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | self.__timedOut = False |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | def timedOut(self): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
49 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
50 | Public method to test, if the process timed out. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
51 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | @return flag indicating a timeout |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | @rtype bool |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
55 | return self.__timedOut |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | def timeoutInterval(self): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | Public method to get the process timeout interval. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | @return process timeout interval in milliseconds |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | @rtype int |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | return self.__timeoutTimer.interval() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | @pyqtSlot() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
67 | def __timeout(self): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
68 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | Private slot to handle the timer interval exoiration. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | self.__timeoutTimer.stop() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | self.__timedOut = True |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
73 | self.kill() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75 | self.timeout.emit() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
76 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77 | @pyqtSlot() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78 | def __started(self): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
80 | Private slot handling the process start. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | self.__timedOut = False |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | self.__timeoutTimer.start() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | @pyqtSlot(int, QProcess.ExitStatus) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | def __finished(self, exitCode, exitStatus): |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
87 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | Private slot handling the end of the process. |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
89 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
90 | @param exitCode exit code of the process (0 = success) |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
91 | @type int |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92 | @param exitStatus exit status of the process |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
93 | @type QProcess.ExitStatus |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94 | """ |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
95 | self.__timeoutTimer.stop() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
96 | |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | if exitStatus == QProcess.ExitStatus.CrashExit or exitCode != 0: |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
98 | self.failed.emit() |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
99 | else: |
4e4c8ee38c45
Added a QProcess derived class with timeout and changed the pip interface to use that class.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100 | self.succeeded.emit() |