o
    $i                  	   @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ G dd dejZ	e
dddee dejd	ejd
efddZdS )    N)Listc                       sP   e Zd ZdZdZdd fdd
Zededed	efd
dZ	 fddZ
  ZS )SubprocessCalledProcessErrorz7The subprocess.CalledProcessError with stripped stdout.2   N)	cmd_indexc                   s   || _ t j|i | d S )N)r   super__init__)selfr   argskwargs	__class__ [/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_private/runtime_env/utils.pyr      s   z%SubprocessCalledProcessError.__init__str_datalast_n_linesreturnc                 C   s.   |dk r| S |   d}d|| d  S )Nr   
)stripsplitjoin)r   r   linesr   r   r   _get_last_n_line   s   z-SubprocessCalledProcessError._get_last_n_linec              	      s   | j d u rg nd| j  dg}|t   | j| jd}| D ]'\}}|rId| j d| d}| || j	 }|| dt
|d  q"d|S )	NRun cmd[z$] failed with the following details.stdoutstderrzLast z
 lines of :r   z    )r   appendr   __str__r   r   itemsLAST_N_LINESr   r   textwrapindentr   )r   str_listoutnamessubtitlelast_n_line_strr   r   r   r      s"   

z$SubprocessCalledProcessError.__str__)__name__
__module____qualname____doc__r    r   staticmethodstrintr   r   __classcell__r   r   r   r   r   
   s    r      )cmd_index_gencmdloggerr2   r   c          	         sd  t |}|d|t|  d}zztj| tjjtjjd|I dH }| I dH \}}W n$ tj	j
y> } z|d}~w tyR } z	td| d|d}~ww |d}|rb|d|| n|d| |jd	krvt|j| ||d
|W |durz|  W n	 ty   Y nw | I dH  S S |durz|  W n	 ty   Y nw | I dH  w w )a  Run command with arguments and return its output.

    If the return code was non-zero it raises a CalledProcessError. The
    CalledProcessError object will have the return code in the returncode
    attribute and any output in the output attribute.

    Args:
        cmd: The cmdline should be a sequence of program arguments or else
            a single string or path-like object. The program to execute is
            the first item in cmd.
        logger: The logger instance.
        cmd_index_gen: The cmd index generator, default is itertools.count(1).
        kwargs: All arguments are passed to the create_subprocess_exec.

    Returns:
        The stdout of cmd.

    Raises:
        CalledProcessError: If the return code of cmd is not 0.
    zRun cmd[%s] %sNr   r   z] got exception.zutf-8zOutput of cmd[%s]: %szNo output for cmd[%s]r   )outputr   )nextinforeprasynciocreate_subprocess_exec
subprocessPIPESTDOUTcommunicate
exceptionsCancelledErrorBaseExceptionRuntimeErrordecode
returncoder   killProcessLookupErrorwait)	r3   r4   r2   r
   r   procr   _er   r   r   check_output_cmd/   sX   



rK   )r9   	itertoolsloggingr;   r!   typestypingr   CalledProcessErrorr   countr.   LoggerGeneratorTyperK   r   r   r   r   <module>   s$    )