|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a function to patch QProcess to support debugging of the |
|
8 process. |
|
9 """ |
|
10 |
|
11 import os |
|
12 |
|
13 from DebugUtilities import isPythonProgram, startsWithShebang, patchArguments |
|
14 |
|
15 _debugClient = None |
|
16 |
|
17 |
|
18 def patchQProcess(module, debugClient): |
|
19 """ |
|
20 Function to patch the QtCore module's QProcess. |
|
21 |
|
22 @param module reference to the imported module to be patched |
|
23 @type module |
|
24 @param debugClient reference to the debug client object |
|
25 @type DebugClient |
|
26 """ # __IGNORE_WARNING_D234__ |
|
27 global _debugClient |
|
28 |
|
29 class QProcessWrapper(module.QProcess): |
|
30 """ |
|
31 Wrapper class for *.QProcess. |
|
32 """ |
|
33 _origQProcessStartDetached = module.QProcess.startDetached |
|
34 |
|
35 def __init__(self, parent=None): |
|
36 """ |
|
37 Constructor |
|
38 """ |
|
39 super(QProcessWrapper, self).__init__(parent) |
|
40 |
|
41 ################################################################### |
|
42 ## Handling of 'start(...)' below |
|
43 ################################################################### |
|
44 |
|
45 def start(self, *args, **kwargs): |
|
46 """ |
|
47 Public method to start the process. |
|
48 |
|
49 This method patches the arguments such, that a debug client is |
|
50 started for the Python script. A Python script is assumed, if the |
|
51 program to be started contains the string 'python'. |
|
52 |
|
53 @param args arguments of the start call |
|
54 @type list |
|
55 @param kwargs keyword arguments of the start call |
|
56 @type dict |
|
57 """ |
|
58 if ( |
|
59 _debugClient.debugging and |
|
60 _debugClient.multiprocessSupport and |
|
61 ((len(args) >= 2 and isinstance(args[1], list)) or |
|
62 (len(args) == 1 and not isinstance(args[0], str)) or |
|
63 len(args) == 0) |
|
64 ): |
|
65 if len(args) >= 2: |
|
66 program = args[0] |
|
67 arguments = args[1] |
|
68 if len(args) > 2: |
|
69 mode = args[2] |
|
70 else: |
|
71 mode = module.QIODevice.ReadWrite |
|
72 else: |
|
73 program = self.program() |
|
74 arguments = self.arguments() |
|
75 if len(args) == 1: |
|
76 mode = args[0] |
|
77 else: |
|
78 mode = module.QIODevice.ReadWrite |
|
79 ok = isPythonProgram(program) |
|
80 if ok: |
|
81 if startsWithShebang(program): |
|
82 scriptName = os.path.basename(program) |
|
83 else: |
|
84 scriptName = os.path.basename(arguments[0]) |
|
85 if not _debugClient.skipMultiProcessDebugging(scriptName): |
|
86 newArgs = patchArguments( |
|
87 _debugClient, |
|
88 [program] + arguments, |
|
89 ) |
|
90 super(QProcessWrapper, self).start( |
|
91 newArgs[0], newArgs[1:], mode) |
|
92 return |
|
93 |
|
94 super(QProcessWrapper, self).start(*args, **kwargs) |
|
95 |
|
96 ################################################################### |
|
97 ## Handling of 'startDetached(...)' below |
|
98 ################################################################### |
|
99 |
|
100 def startDetached(self, *args, **kwargs): |
|
101 """ |
|
102 Public method to start the detached process. |
|
103 |
|
104 This method patches the arguments such, that a debug client is |
|
105 started for the Python script. A Python script is assumed, if the |
|
106 program to be started contains the string 'python'. |
|
107 |
|
108 @param args arguments of the start call |
|
109 @type list |
|
110 @param kwargs keyword arguments of the start call |
|
111 @type dict |
|
112 @return flag indicating a successful start |
|
113 @rtype bool |
|
114 """ |
|
115 if isinstance(self, str): |
|
116 return QProcessWrapper.startDetachedStatic( |
|
117 self, *args) |
|
118 else: |
|
119 return self.__startDetached(*args, **kwargs) |
|
120 |
|
121 def __startDetached(self, *args, **kwargs): |
|
122 """ |
|
123 Private method to start the detached process. |
|
124 |
|
125 This method patches the arguments such, that a debug client is |
|
126 started for the Python script. A Python script is assumed, if the |
|
127 program to be started contains the string 'python'. |
|
128 |
|
129 @param args arguments of the start call |
|
130 @type list |
|
131 @param kwargs keyword arguments of the start call |
|
132 @type dict |
|
133 @return flag indicating a successful start |
|
134 @rtype bool |
|
135 """ |
|
136 if ( |
|
137 _debugClient.debugging and |
|
138 _debugClient.multiprocessSupport and |
|
139 len(args) == 0 |
|
140 ): |
|
141 program = self.program() |
|
142 arguments = self.arguments() |
|
143 wd = self.workingDirectory() |
|
144 |
|
145 ok = isPythonProgram(program) |
|
146 if ok: |
|
147 return QProcessWrapper.startDetachedStatic( |
|
148 program, arguments, wd) |
|
149 |
|
150 return super(QProcessWrapper, self).startDetached(*args, **kwargs) |
|
151 |
|
152 @staticmethod |
|
153 def startDetachedStatic(*args, **kwargs): |
|
154 """ |
|
155 Static method to start the detached process. |
|
156 |
|
157 This method patches the arguments such, that a debug client is |
|
158 started for the Python script. A Python script is assumed, if the |
|
159 program to be started contains the string 'python'. |
|
160 |
|
161 @param args arguments of the start call |
|
162 @type list |
|
163 @param kwargs keyword arguments of the start call |
|
164 @type dict |
|
165 @return flag indicating a successful start |
|
166 @rtype bool |
|
167 """ |
|
168 if ( |
|
169 _debugClient.debugging and |
|
170 _debugClient.multiprocessSupport and |
|
171 (len(args) >= 2 and isinstance(args[1], list)) |
|
172 ): |
|
173 program = args[0] |
|
174 arguments = args[1] |
|
175 if len(args) >= 3: |
|
176 wd = args[2] |
|
177 else: |
|
178 wd = "" |
|
179 ok = isPythonProgram(program) |
|
180 if ok: |
|
181 if startsWithShebang(program): |
|
182 scriptName = os.path.basename(program) |
|
183 else: |
|
184 scriptName = os.path.basename(arguments[0]) |
|
185 if not _debugClient.skipMultiProcessDebugging(scriptName): |
|
186 newArgs = patchArguments( |
|
187 _debugClient, |
|
188 [program] + arguments, |
|
189 ) |
|
190 return QProcessWrapper._origQProcessStartDetached( |
|
191 newArgs[0], newArgs[1:], wd) |
|
192 |
|
193 return QProcessWrapper._origQProcessStartDetached( |
|
194 *args, **kwargs) |
|
195 |
|
196 _debugClient = debugClient |
|
197 module.QProcess = QProcessWrapper |