@@ -67,8 +67,13 @@ def _process_output(encoding, temp_file_path):
6767 output = output .decode (encoding )
6868 return output , None # In Windows stderr writing in stdout
6969
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+ ):
7175 assert exec_env is None or type (exec_env ) == dict # noqa: E721
76+ assert cwd is None or type (cwd ) == str # noqa: E721
7277
7378 # TODO: why don't we use the data from input?
7479
@@ -104,6 +109,7 @@ def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process
104109 stdin = stdin or subprocess .PIPE if input is not None else None ,
105110 stdout = stdout ,
106111 stderr = stderr ,
112+ cwd = cwd ,
107113 ** extParams ,
108114 )
109115 if get_process :
@@ -116,8 +122,13 @@ def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process
116122 output , error = self ._process_output (encoding , temp_file_path )
117123 return process , output , error
118124
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+ ):
120130 assert exec_env is None or type (exec_env ) == dict # noqa: E721
131+ assert cwd is None or type (cwd ) == str # noqa: E721
121132
122133 input_prepared = None
123134 if not get_process :
@@ -154,6 +165,7 @@ def _run_command__generic(self, cmd, shell, input, stdin, stdout, stderr, get_pr
154165 stdin = stdin or subprocess .PIPE if input is not None else None ,
155166 stdout = stdout or subprocess .PIPE ,
156167 stderr = stderr or subprocess .PIPE ,
168+ cwd = cwd ,
157169 ** extParams
158170 )
159171 assert not (process is None )
@@ -173,26 +185,44 @@ def _run_command__generic(self, cmd, shell, input, stdin, stdout, stderr, get_pr
173185 error = error .decode (encoding )
174186 return process , output , error
175187
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+ ):
177193 """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+
178198 if os .name == 'nt' and stdout is None : # Windows
179199 method = __class__ ._run_command__nt
180200 else : # Other OS
181201 method = __class__ ._run_command__generic
182202
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 )
184204
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+ ):
188212 """
189213 Execute a command in a subprocess and handle the output based on the provided parameters.
190214 """
191215 assert type (expect_error ) == bool # noqa: E721
192216 assert type (ignore_errors ) == bool # noqa: E721
193217 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+ )
194225
195- process , output , error = self ._run_command (cmd , shell , input , stdin , stdout , stderr , get_process , timeout , encoding , exec_env = exec_env )
196226 if get_process :
197227 return process
198228
0 commit comments