o
    ÿé!^ƒ  ã                   @   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y(   ejZY nw ej	d  dkr4e
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¡r-t  	¡ dkr-t
|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   ÚfpathÚmode© r   ú//usr/lib/python3/dist-packages/pexpect/utils.pyÚis_executable_file   s   
ÿÿþr   c                 C   sv   t j | ¡dkrt| ƒr| S |du rt j}| d¡}|st j}| t j¡}|D ]}t j 	|| ¡}t|ƒr8|  S q'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ÚpÚpathlistr   Úffr   r   r   Úwhich0   s   
ÿr'   c           
      C   s  g }d}d}d}d}d}d}|}| D ]c}	||ks||krK|	dkr#|}q|	dkr*|}q|	d	kr1|}q|	  ¡ rD||kr:q| |¡ d}|}q||	 }|}q||krV||	 }|}q||krf|	dkra|}q||	 }q||kru|	d	krq|}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)
Úcommand_lineÚarg_listÚargÚstate_basicÚ	state_escÚstate_singlequoteÚstate_doublequoteÚstate_whitespaceÚstateÚcr   r   r   Úsplit_command_lineE   sN   

€
r:   c                 C   sˆ   |dur
t   ¡ | }	 z	t | |||¡W S  tyB   t ¡ d }|jd tjkr?|dur>|t   ¡  }|dk r>g g g f Y S n‚ Y nw q)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). NTr(   r   )ÚtimeÚselectÚInterruptedErrorr
   Úexc_infoÚargsÚerrnoÚEINTR)ÚiwtdÚowtdÚewtdÚtimeoutÚend_timeÚerrr   r   r   Úselect_ignore_interrupts‚   s"   	€€ôýrH   c                 C   sÐ   |dur
t   ¡ | }t ¡ }| D ]}| |tjtjB tjB tjB ¡ q	 z|du r+dn|d }| |¡}dd„ |D ƒW S  tyf   t	 
¡ d }|jd tjkrc|durb|t   ¡  }|dk rbg  Y S n‚ Y nw q$)zOSimple wrapper around poll to register file descriptors and
    ignore signals.NTiè  c                 S   s   g | ]\}}|‘qS r   r   )Ú.0ÚafdÚ_r   r   r   Ú
<listcomp>®   s    z*poll_ignore_interrupts.<locals>.<listcomp>r(   r   )r;   r<   ÚpollÚregisterÚPOLLINÚPOLLPRIÚPOLLHUPÚPOLLERRr=   r
   r>   r?   r@   rA   )ÚfdsrE   rF   ÚpollerÚfdÚ
timeout_msÚresultsrG   r   r   r   Úpoll_ignore_interruptsŸ   s,   "
€€ôûrX   )N)r   r
   r   r<   r;   r@   r=   Ú	NameErrorÚerrorÚversion_infoÚstrÚstring_typesÚunicoder   r'   r:   rH   rX   r   r   r   r   Ú<module>   s&    
þ

=