B
    +‹dƒ  ã               @   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y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   úZ/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/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¡}x&|D ]}t j 	|| ¡}t|ƒrP|S qPW d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ÚpZpathlistr   Úffr   r   r   Úwhich0   s    

r%   c       
      C   s  g }d}d}d}d}d}d}|}xÎ| D ]Æ}	||ks:||kr˜|	dkrH|}qì|	dkrV|}qì|	d	krd|}qì|	  ¡ rŠ||krvq–| |¡ d}|}qì||	 }|}q&||kr®||	 }|}q&||krÎ|	dkrÄ|}qì||	 }q&||kr&|	d	krä|}q&||	 }q&W |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    




r2   c             C   sŠ   |dk	rt   ¡ | }xpyt | |||¡S  tk
r€   t ¡ d }|jd tjkrz|dk	r||t   ¡  }|dk r|g g g fS n‚ Y qX qW 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 ¡ }x,| D ]$}| |tjtjB tjB tjB ¡ q"W x†y,|dkrZdn|d }| |¡}dd„ |D ƒS  tk
rÌ   t	 
¡ d }|jd tjkrÆ|dk	rÈ|t   ¡  }|dk rÈg S n‚ Y qLX qLW d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   )r3   r4   ÚpollÚregisterÚPOLLINÚPOLLPRIÚPOLLHUPÚPOLLERRr5   r
   r6   r7   r8   r9   )Zfdsr:   r;   ZpollerÚfdZ
timeout_msÚresultsr<   r   r   r   Úpoll_ignore_interruptsŸ   s$    
$
rI   )N)N)N)r   r
   r   r4   r3   r8   r5   Ú	NameErrorÚerrorÚversion_infoÚstrÚstring_typesÚunicoder   r%   r2   r=   rI   r   r   r   r   Ú<module>   s"   
=
