wL i TdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZmZddlmZeeZed e eddffd Z dd eee efd eeee fd ej.fd Ze dd eee efd eeee fd e ee ee efddffdZy)zFContains utilities to easily handle subprocesses in `huggingface_hub`.N)contextmanager)StringIO)Path)IO GeneratorListOptionalTupleUnion) get_loggerreturnc#Kt}tj}|t_ ||t_y#|t_wxYww)aCapture output that is printed to terminal. Taken from https://stackoverflow.com/a/34738440 Example: ```py >>> with capture_output() as output: ... print("hello world") >>> assert output.getvalue() == "hello world " ``` N)rsysstdout)outputprevious_outputs g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/huggingface_hub/utils/_subprocess.pycapture_outputrs7ZFjjOCJ% $ _ s&A 9 A  AA commandfolderc t|tr|j}t|tr t|}t j |ftj tj |dd|xstjd|S)aX Method to run subprocesses. Calling this will capture the `stderr` and `stdout`, please call `subprocess.run` manually in case you would like for them not to be captured. Args: command (`str` or `List[str]`): The command to execute as a string or list of strings. folder (`str`, *optional*): The folder in which to run the command. Defaults to current working directory (from `os.getcwd()`). check (`bool`, *optional*, defaults to `True`): Setting `check` to `True` will raise a `subprocess.CalledProcessError` when the subprocess has a non-zero exit code. kwargs (`Dict[str]`): Keyword arguments to be passed to the `subprocess.run` underlying command. Returns: `subprocess.CompletedProcess`: The completed process. utf-8replace)stderrrcheckencodingerrorscwd) isinstancestrsplitr subprocessrunPIPEosgetcwd)rrrkwargss rrun_subprocessr)5st4'3--/&$V >>   !biik    c +Kt|tr|j}tj|ftj tj tj dd|xstjd|5}|jJd|jJd|j|jfdddy#1swYyxYww)aRun a subprocess in an interactive mode in a context manager. Args: command (`str` or `List[str]`): The command to execute as a string or list of strings. folder (`str`, *optional*): The folder in which to run the command. Defaults to current working directory (from `os.getcwd()`). kwargs (`Dict[str]`): Keyword arguments to be passed to the `subprocess.run` underlying command. Returns: `Tuple[IO[str], IO[str]]`: A tuple with `stdin` and `stdout` to interact with the process (input and output are utf-8 encoded). Example: ```python with _interactive_subprocess("git credential-store get") as (stdin, stdout): # Write to stdin stdin.write("url=hf.co username=obama ".encode("utf-8")) stdin.flush() # Read from stdout output = stdout.read().decode("utf-8") ``` rr)stdinrrrrrNz'subprocess is opened as subprocess.PIPE) r r!r"r#Popenr%STDOUTr&r'r,r)rrr(processs rrun_interactive_subprocessr0as@'3--/    oo    !biik    , }}(S*SS(~~)T+TT)mmW^^++ , , ,sA>CAC  C CC)NT)N)__doc__r&r#r contextlibriorpathlibrtypingrrrr r r loggingr __name__loggerrr!CompletedProcessr)r0r*rr;sM  %>> H % (D$"67%%.*. ) 3S > ") U39% &)   )X*.., 3S > "., U39% &.,uRWbg%&d23 .,.,r*