55from shutil import rmtree
66from stat import S_IXGRP , S_IXOTH , S_IXUSR
77from subprocess import PIPE , Popen
8- from typing import Callable
8+ from typing import Callable , overload
99
1010import py7zr
1111import requests
@@ -162,9 +162,31 @@ def get_binary_path(self, command: str | None = None) -> Path:
162162 raise FileNotFoundError (f"Binary '{ binary_path } ' does not exist." )
163163 return binary_path
164164
165+ @overload
165166 def call (
166- self , command : str | None = None , arguments : list [str ] | None = None , working_directory : Path | str = Path ("." )
167- ) -> str :
167+ self ,
168+ command : str | None = ...,
169+ arguments : list [str ] | None = ...,
170+ working_directory : Path | str = ...,
171+ output_file_path : None = ...,
172+ ) -> str : ...
173+
174+ @overload
175+ def call (
176+ self ,
177+ command : str | None = ...,
178+ arguments : list [str ] | None = ...,
179+ working_directory : Path | str = ...,
180+ output_file_path : str | Path = ...,
181+ ) -> None : ...
182+
183+ def call (
184+ self ,
185+ command : str | None = None ,
186+ arguments : list [str ] | None = None ,
187+ working_directory : Path | str = Path .cwd (),
188+ output_file_path : str | Path | None = None ,
189+ ) -> str | None :
168190 """
169191 Calls the binary with the specified command and arguments.
170192
@@ -188,16 +210,29 @@ def call(
188210
189211 if arguments is None :
190212 arguments = []
213+
214+ if output_file_path is not None :
215+ output_file_path = Path (output_file_path )
216+ if not output_file_path .is_absolute ():
217+ output_file_path = working_directory / output_file_path
218+ output_stream = open (output_file_path , "w" )
191219 process = Popen (
192- [str (binary_path )] + arguments , stdout = PIPE , stderr = PIPE , creationflags = creation_flag , cwd = working_directory
220+ [str (binary_path )] + arguments ,
221+ stdout = PIPE if output_file_path is None else output_stream ,
222+ stderr = PIPE ,
223+ creationflags = creation_flag ,
224+ cwd = working_directory ,
193225 )
194226 stdout , stderr = process .communicate ()
195227 if process .returncode != 0 :
196228 raise RuntimeError (
197229 f"Command '{ binary_path } { ' ' .join (arguments )} ' failed with error:\n "
198230 f"{ stderr .decode ('utf-8' )} \n { stdout .decode ('utf-8' )} "
199231 )
200- return stdout .decode ("utf-8" )
232+ if output_file_path is None :
233+ return stdout .decode ("utf-8" )
234+ output_stream .close ()
235+ return None
201236
202237 def clear_cache (self ) -> None :
203238 """
0 commit comments