U
    pufƒ  ã                   @   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ze W n ek
rR   ejZY nX ej	d  dkrje
fZnee
fZdd„ Zddd„Zdd„ Zdd	d
„Zddd„ZdS )é    Né   c                 C   sh   t j | ¡}t j |¡sdS t  |¡j}tj d¡rZt  	¡ dkrZt
|tjtjB tjB @ ƒS t  |t j¡S )zšChecks that path is an executable regular file, or a symlink towards one.

    This is roughly ``os.path isfile(path) and os.access(path, os.X_OK)``.
    FÚsunosr   )ÚosÚpathÚrealpathÚisfileÚstatÚst_modeÚsysÚplatformÚ
startswithÚgetuidÚboolÚS_IXUSRÚS_IXGRPÚS_IXOTHÚaccessÚX_OK)r   ZfpathÚmode© r   ú1/tmp/pip-unpacked-wheel-v5brguv_/pexpect/utils.pyÚis_executable_file   s    
ÿÿþr   c                 C   sv   t j | ¡dkrt| ƒr| S |dkr*t j}| d¡}|s>t j}| t j¡}|D ]"}t j 	|| ¡}t|ƒrN|  S qNdS )zÐThis takes a given filename; tries to find it in the environment path;
    then checks if it is executable. This returns the full path to the filename
    if found and executable. Otherwise this returns None.Ú NÚPATH)
r   r   Údirnamer   ÚenvironÚgetÚdefpathÚsplitÚpathsepÚjoin)ÚfilenameÚenvÚpÚpathlistr   Úffr   r   r   Úwhich0   s    

r&   c           
      C   s  g }d}d}d}d}d}d}|}| D ]Æ}	||ks8||kr–|	dkrF|}qê|	dkrT|}qê|	d	krb|}qê|	  ¡ rˆ||krtq”| |¡ d}|}qê||	 }|}q$||kr¬||	 }|}q$||krÌ|	dkrÂ|}qê||	 }q$||kr$|	d	krâ|}q$||	 }q$|dkrþ| |¡ |S )
a  This splits a command line into a list of arguments. It splits arguments
    on spaces, but handles embedded quotes, doublequotes, and escaped
    characters. It's impossible to do this with a regular expression, so I
    wrote a little state machine to parse the command line. r   r   é   é   r   é   ú\ú'ú")ÚisspaceÚappend)
Zcommand_lineÚarg_listÚargZstate_basicZ	state_escZstate_singlequoteZstate_doublequoteZstate_whitespaceÚstateÚcr   r   r   Úsplit_command_lineE   sL    



r3   c                 C   sŒ   |dk	rt   ¡ | }zt | |||¡W S  tk
r„   t ¡ d }|jd tjkr~|dk	r€|t   ¡  }|dk r€g g g f Y S n‚ Y qX qdS )zæThis is a wrapper around select.select() that ignores signals. If
    select.select raises a select.error exception and errno is an EINTR
    error then it is ignored. Mainly this is used to ignore sigwinch
    (terminal resize). Nr'   r   )ÚtimeÚselectÚInterruptedErrorr
   Úexc_infoÚargsÚerrnoÚEINTR)ZiwtdZowtdZewtdÚtimeoutÚend_timeÚerrr   r   r   Úselect_ignore_interrupts‚   s    	r>   c                 C   sÔ   |dk	rt   ¡ | }t ¡ }| D ]$}| |tjtjB tjB tjB ¡ q z.|dkrTdn|d }| |¡}dd„ |D ƒW S  tk
rÌ   t	 
¡ d }|jd tjkrÆ|dk	rÈ|t   ¡  }|dk rÈg  Y S n‚ Y qFX qFdS )zOSimple wrapper around poll to register file descriptors and
    ignore signals.Niè  c                 S   s   g | ]\}}|‘qS r   r   )Ú.0ZafdÚ_r   r   r   Ú
<listcomp>®   s     z*poll_ignore_interrupts.<locals>.<listcomp>r'   r   )r4   r5   ÚpollÚregisterÚPOLLINÚPOLLPRIÚPOLLHUPÚPOLLERRr6   r
   r7   r8   r9   r:   )Zfdsr;   r<   ZpollerÚfdZ
timeout_msÚresultsr=   r   r   r   Úpoll_ignore_interruptsŸ   s"    "

rJ   )N)N)N)r   r
   r   r5   r4   r9   r6   Ú	NameErrorÚerrorÚversion_infoÚstrÚstring_typesÚunicoder   r&   r3   r>   rJ   r   r   r   r   Ú<module>   s"   
=
