
    i                     p   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ  ee          Zed
ee	ddf         fd            Z	 	 ddeee         z  deez  dz  d
ej        fdZe	 ddeee         z  deez  dz  d
eeee         ee         f         ddf         fd            ZdS )zFContains utilities to easily handle subprocesses in `huggingface_hub`.    N)	Generator)contextmanager)StringIO)Path)IO   )
get_loggerreturnc               #      K   t                      } t          j        }| t          _        	 | V  |t          _        dS # |t          _        w xY w)a  Capture 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)r   sysstdout)outputprevious_outputs     b/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/huggingface_hub/utils/_subprocess.pycapture_outputr      sK       ZZFjOCJ%$


_
$$$$s	   < A
Tcommandfolderc           	          t          | t                    r|                                 } t          |t                    rt          |          }t	          j        | fd|dd|pt          j                    d|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.
    Tutf-8replace)r   checkencodingerrorscwd)
isinstancestrsplitr   
subprocessrunosgetcwd)r   r   r   kwargss       r   run_subprocessr#   5   s    4 '3 "--//&$ V>!bikk        c           
   +     K   t          | t                    r|                                 } t          j        | ft          j        t          j        t          j        dd|pt          j                    d|5 }|j	        
J d            |j
        
J d            |j	        |j
        fV  ddd           dS # 1 swxY w Y   dS )a  Run 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")
    ```
    r   r   )stdinr   stderrr   r   r   Nz'subprocess is opened as subprocess.PIPE)r   r   r   r   PopenPIPESTDOUTr    r!   r&   r   )r   r   r"   processs       r   run_interactive_subprocessr,   `   s     @ '3 "--//			
o !bikk	
 	
 	
 	
 , 
}((*S(((~))+T)))mW^++++, , , , , , , , , , , , , , , , , ,s   63B66B:=B:)NT)N)__doc__r    r   r   collections.abcr   
contextlibr   ior   pathlibr   typingr   loggingr	   __name__loggerr   r   listCompletedProcessr#   tupler,    r$   r   <module>r:      s   M L 				     



 % % % % % % % % % % % %                         
H		 %	(D$"67 % % % %. !%
( (49_($J(
  ( ( ( (V  !%., .,49_.,$J., uRWbg%&d23	., ., ., ., ., .,r$   