@@ -67,8 +67,13 @@ def _process_output(encoding, temp_file_path):
67
67
output = output .decode (encoding )
68
68
return output ,None # In Windows stderr writing in stdout
69
69
70
- def _run_command__nt (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,exec_env = None ):
70
+ def _run_command__nt (
71
+ self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,
72
+ exec_env :typing .Optional [dict ],
73
+ cwd :typing .Optional [str ],
74
+ ):
71
75
assert exec_env is None or type (exec_env )== dict # noqa: E721
76
+ assert cwd is None or type (cwd )== str # noqa: E721
72
77
73
78
# TODO: why don't we use the data from input?
74
79
@@ -104,6 +109,7 @@ def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process
104
109
stdin = stdin or subprocess .PIPE if input is not None else None ,
105
110
stdout = stdout ,
106
111
stderr = stderr ,
112
+ cwd = cwd ,
107
113
** extParams ,
108
114
)
109
115
if get_process :
@@ -116,8 +122,13 @@ def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process
116
122
output ,error = self ._process_output (encoding ,temp_file_path )
117
123
return process ,output ,error
118
124
119
- def _run_command__generic (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,exec_env = None ):
125
+ def _run_command__generic (
126
+ self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,
127
+ exec_env :typing .Optional [dict ],
128
+ cwd :typing .Optional [str ],
129
+ ):
120
130
assert exec_env is None or type (exec_env )== dict # noqa: E721
131
+ assert cwd is None or type (cwd )== str # noqa: E721
121
132
122
133
input_prepared = None
123
134
if not get_process :
@@ -154,6 +165,7 @@ def _run_command__generic(self, cmd, shell, input, stdin, stdout, stderr, get_pr
154
165
stdin = stdin or subprocess .PIPE if input is not None else None ,
155
166
stdout = stdout or subprocess .PIPE ,
156
167
stderr = stderr or subprocess .PIPE ,
168
+ cwd = cwd ,
157
169
** extParams
158
170
)
159
171
assert not (process is None )
@@ -173,26 +185,44 @@ def _run_command__generic(self, cmd, shell, input, stdin, stdout, stderr, get_pr
173
185
error = error .decode (encoding )
174
186
return process ,output ,error
175
187
176
- def _run_command (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,exec_env = None ):
188
+ def _run_command (
189
+ self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,
190
+ exec_env :typing .Optional [dict ],
191
+ cwd :typing .Optional [str ],
192
+ ):
177
193
"""Execute a command and return the process and its output."""
194
+
195
+ assert exec_env is None or type (exec_env )== dict # noqa: E721
196
+ assert cwd is None or type (cwd )== str # noqa: E721
197
+
178
198
if os .name == 'nt' and stdout is None :# Windows
179
199
method = __class__ ._run_command__nt
180
200
else :# Other OS
181
201
method = __class__ ._run_command__generic
182
202
183
- return method (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,exec_env = exec_env )
203
+ return method (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,exec_env , cwd )
184
204
185
- def exec_command (self ,cmd ,wait_exit = False ,verbose = False ,expect_error = False ,encoding = None ,shell = False ,
186
- text = False ,input = None ,stdin = None ,stdout = None ,stderr = None ,get_process = False ,timeout = None ,
187
- ignore_errors = False ,exec_env = None ):
205
+ def exec_command (
206
+ self ,cmd ,wait_exit = False ,verbose = False ,expect_error = False ,encoding = None ,shell = False ,
207
+ text = False ,input = None ,stdin = None ,stdout = None ,stderr = None ,get_process = False ,timeout = None ,
208
+ ignore_errors = False ,
209
+ exec_env :typing .Optional [dict ]= None ,
210
+ cwd :typing .Optional [str ]= None
211
+ ):
188
212
"""
189
213
Execute a command in a subprocess and handle the output based on the provided parameters.
190
214
"""
191
215
assert type (expect_error )== bool # noqa: E721
192
216
assert type (ignore_errors )== bool # noqa: E721
193
217
assert exec_env is None or type (exec_env )== dict # noqa: E721
218
+ assert cwd is None or type (cwd )== str # noqa: E721
219
+
220
+ process ,output ,error = self ._run_command (
221
+ cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,
222
+ exec_env ,
223
+ cwd
224
+ )
194
225
195
- process ,output ,error = self ._run_command (cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ,exec_env = exec_env )
196
226
if get_process :
197
227
return process
198
228