a
    ’·DfYF  ã                   @   sô   d Z ddlZddlZddlmZmZmZ deeeddœZddlm	Z	 e
dƒZd	d
„ Zdd„ Zdd„ Zdd„ Zd)dd„Zd*dd„Zd+dd„Zd,dd„Zd-dd„Zd.dd„Zd/d d!„Zd0d"d#„Zd1d$d%„Zd&d'„ ZeZeZeZeZeZeZe d(k rðdS )2zA
shell utilities for user environment and filesystem exploration
é    N)ÚPopenÚPIPEÚSTDOUTT)ÚshellÚstdinÚstdoutÚstderrÚ	close_fdsé   )ÚrmtreeZ0o775c                  C   s8   t dddpt ddd} | dv r$d} | r4tj | ¡S dS )z­get the name (e.g. ``bash``) of the current command shell

    Args:
        None

    Returns:
        string name of the shell, or None if name can not be determined.
    ÚSHELLF©ÚallZSESSIONNAME)ZConsoleÚcmdN)ÚenvÚosÚpathÚbasename)r   © r   úX/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/pox/shutils.pyÚ	shelltype   s    	r   c                  C   st   z*t dddptj d¡} d| v r&t‚| W S    d} tj}| sfttƒ tj |¡ƒ} tj 	tj
|¡}q<|  Y S 0 dS )z¦get the full path of the user's home directory

    Args:
        None

    Returns:
        string path of the directory, or None if home can not be determined.
    ÚUSERPROFILEFr   z$HOMEú$N)r   r   r   Ú
expandvarsÚ
ValueErrorÚcurdirÚwhereÚusernameÚabspathÚjoinÚpardir)ÚhomedirÚ	directoryr   r   r   r!   $   s    	r!   c                   C   s   t j t  ¡ ¡d t j S )zŒget the path corresponding to the root of the current drive

    Args:
        None

    Returns:
        string path of the directory.
    r   )r   r   Ú
splitdriveÚgetcwdÚsepr   r   r   r   Úrootdir9   s    	r&   c                  C   s@   z
t  ¡ W S    t j d¡} d| v r2tddd} |  Y S 0 dS )zrget the login name of the current user

    Args:
        None

    Returns:
        string name of the user.
    z$USERr   ÚUSERNAMEFr   N)r   Úgetloginr   r   r   )Úunamer   r   r   r   D   s    	
r   Ú c                 C   sh   | dv rt jS | dv rt jS | dv r*t jS | dv r8t jS | dvrb|  d¡sR| d7 } td|  ƒ t‚t jS )z´get the separator string for the given type of separator

    Args:
        type (str, default=''): one of ``{sep,line,path,ext,alt}``.

    Returns:
        separator string.
    )r   Úpathsep)ÚextÚextsep)ÚlineÚlinesep)ZaltÚaltsep)r*   r%   r%   zError: 'os.%s' not found)	r   r+   r-   r/   r0   ÚendswithÚprintÚ	TypeErrorr%   )Útyper   r   r   r%   T   s    	r%   c                 C   s>   |s
t j}|  |¡}g }|D ]}||vr| |¡ q| |¡S )a«  remove duplicate paths from given set of paths

    Args:
        path (str): path string (e.g. '/Users/foo/bin:/bin:/sbin:/usr/bin').
        pathsep (str, default=None): path separator (e.g. ``:``).

    Returns:
        string composed of one or more paths, with duplicates removed.

    Examples:
        >>> minpath('.:/Users/foo/bin:.:/Users/foo/bar/bin:/Users/foo/bin')
        '.:/Users/foo/bin:/Users/foo/bar/bin'
    )r   r+   ÚsplitÚappendr   )r   r+   ÚpathlistZ	shortlistÚitemr   r   r   Úminpathh   s    

r9   Fc                 C   sŠ   ddl }i }tj ¡ D ]\}}|  || ¡r|||< q|rb| ¡ D ] \}}|  |d¡r@t|ƒ||< q@|s†t|ƒdkrvdS t| ¡ ƒd S |S )a5  get dict of environment variables of the form ``{variable:value}``

    Args:
        variable (str): name or partial name for environment variable.
        all (bool, default=True): if False, only return the first match.
        minimal (bool, default=False): if True, remove all duplicate paths.

    Returns:
        dict of strings of environment variables.

    Warning:
        selecting all=False can lead to unexpected matches of *variable*.

    Examples:
        >>> env('*PATH')
        {'PYTHONPATH': '.', 'PATH': '.:/usr/bin:/bin:/usr/sbin:/sbin'}
    r   Nz*PATH)Úfnmatchr   ÚenvironÚitemsr9   ÚlenÚlistÚvalues)Úvariabler   Zminimalr:   ÚvalsÚkeyÚvaluer   r   r   r      s    
r   c                 C   sš   t jdd… dkrt| |dS d}t||  fi t¤Ž}|j ¡  |j ¡ }|j ¡  | 	¡ }| 
¡  d¡d }| 
¡  ¡ }|sŠ|s†d}|S |s–|d	 S |S )
a[  get path to the given program

    search the standard binary install locations for the given executable.

    Args:
        prog (str): name of an executable to search for (e.g. ``python``).
        all (bool, default=True): if True, return a list of paths found.

    Returns:
        string path of the executable, or list of path strings.
    Né   Úwinr   zwhereis ú:éÿÿÿÿr*   r   )ÚsysÚplatformÚwhichr   Úpopen4r   Úcloser   ÚreadÚdecodeÚstripr5   )Úprogr   ÚwhcomÚpÚpathstrÚpathsr7   r   r   r   Úwhereis¤   s    


rU   c                 C   s  t jdd… dkrª|  d¡s2| |  d¡r,dnd7 } tddd	pJtj tj¡}g }|rXdnd
}| 	tj
¡D ]$}|rx|rx qŽ|t| |d|d7 }qh|s¦t|ƒr¢|d S dS |S d}|rº|d7 }t||  fi t¤Ž}	|	j ¡  |	j ¡ }
|	j ¡  d|  d }|
 ¡ }
||
v r|rdS t|
 ¡ tjƒ}
|
 	tj¡}tt|ƒƒD ]2}|s<tj || ¡r<tj || ¡||< q<|sŒt|ƒrˆ|d S dS |S )a÷  get the path of the given program

    search the user's paths for the given executable.

    Args:
        prog (str): name of an executable to search for (e.g. ``python``).
        allow_links (bool, default=True): if False, replace link with fullpath.
        ignore_errors (bool, default=True): if True, ignore search errors.
        all (bool, default=False): if True, get list of paths for executable.

    Returns:
        if all=True, get a list of string paths, else return a string path. 
    NrD   rE   z.exeÚ.r*   ÚPATHFr   Úfile)ÚrootÚrecurser4   r   zwhich z-a zno z in)rH   rI   r1   Úcountr   r   r   r   r   r5   r+   Úfindr=   r   rK   r   rL   r   rM   rN   r9   rO   r/   ÚrangeÚislinkÚrealpath)rP   Zallow_linksÚignore_errorsr   ÚdirsrT   Ú_typeZ_dirrQ   rR   rS   ÚerrindÚir   r   r   rJ   Ã   s6     


rJ   c              
   C   s  |s
t j}|du rnh|dv r"d}nZ|dv r0d}nL|dv r>d}n>|dv rLd	}n0|d
v rZd}n"|dv rhd}n|rxtd| ƒ d}ztjdd… dkr–t‚g }|  d¡}|D ]ð}d||f }|rÈ|d| 7 }|sÖ|d7 }n|durò|dt|ƒd  7 }|rþt|ƒ t|fi t	¤Ž}	|	j
 ¡  |	j ¡ }
|	j ¡  ddg}|d |
v r\|rTtd| ƒ  W dS |
D ]6}| ¡ }|d |vr`| ¡ }| t j |¡¡ q`q¨W nf   d}d}d}|dv rÀd}n,|dv rÐd}n|dv ràd}nd}d}d}t|| ||||ƒ}Y n0 |S ) aö  get the path to a file or directory

    Args:
        patterns (str): name or partial name of items to search for.
        root (str, default=None): path of top-level directory to search.
        recurse (bool, default=True): if True, recurse downward from *root*.
        type (str, default=None): a search filter.
        verbose (bool, default=False): if True, be verbose about the search.

    Returns:
        a list of string paths.

    Notes:
        on some OS, *recursion* can be specified by recursion depth (*int*),
        and *patterns* can be specified with basic pattern matching. Also,
        multiple patterns can be specified by splitting patterns with a ``;``.
        The *type* can be one of ``{file, dir, link, socket, block, char}``.

    Examples:
        >>> find('pox*', root='..')
        ['/Users/foo/pox/pox', '/Users/foo/pox/scripts/pox_launcher.py']
        >>> 
        >>> find('*shutils*;*init*')
        ['/Users/foo/pox/pox/shutils.py', '/Users/foo/pox/pox/__init__.py']
    N)ÚfrX   re   )ÚdÚdirrf   )ÚlÚlinkrh   )ÚsÚsocketrj   )ÚbÚblockrl   )ÚcÚcharrn   z)type '%s' not understood, will be ignoredrD   rE   ú;zfind %s -name %rz -type z -maxdepth 1Tz -maxdepth %dr
   zfind:zUsage:zError: incorrect usage '%s'r   F)re   )rh   )rf   )r   r   r2   rH   rI   ÚNotImplementedErrorr5   Úintr   rK   r   rL   r   Ú	readlinesrN   rO   r6   r   r   Úwalk)ÚpatternsrY   rZ   r4   Úverboser7   Zsearch_listr8   ÚcommandrR   rS   rc   r   ÚfoldersÚfilesÚlinksr   r   r   r\   ï   sX    






r\   Ú*c                    sª  ddl ‰ | d¡}z
tj}W n ty2   d}Y n0 |rfg }t | ¡D ]\}	}
}|s\|rÌ|
D ]j}tj tj |	|¡¡}|r–tj |¡r–tj 	|¡r¦|r`tj 	|¡r`|D ]}ˆ   ||¡rª| 
|¡  q`qªq`|sÖ|rP|D ]t}tj tj |	|¡¡}|rtj |¡rtj 	|¡r&|rÚtj 	|¡rÚ|D ]"}ˆ   ||¡r*| 
|¡  qÚq*qÚ|sHg |
dd…< qH|S G dd„ dƒ}||||||g d}‡ fdd„}tj | ||¡ |jS )	aè  walk directory tree and return a list matching the requested pattern

    Args:
        root (str): path of top-level directory to search.
        patterns (str, default='*'): (partial) name of items to search for.
        recurse (bool, default=True): if True, recurse downward from *root*.
        folders (bool, default=False): if True, include folders in the results.
        files (bool, default=True): if True, include files in results.
        links (bool, default=True): if True, include links in results.

    Returns:
        a list of string paths.

    Notes:
        patterns can be specified with basic pattern matching. Additionally,
        multiple patterns can be specified by splitting patterns with a ``;``.

    Examples:
        >>> walk('..', patterns='pox*')
        ['/Users/foo/pox/pox', '/Users/foo/pox/scripts/pox_launcher.py']
        >>> 
        >>> walk('.', patterns='*shutils*;*init*')
        ['/Users/foo/pox/pox/shutils.py', '/Users/foo/pox/pox/__init__.py']
    r   Nrp   c                   @   s   e Zd Zdd„ ZdS )zwalk.<locals>.Bunchc                 [   s   | j  |¡ d S ©N)Ú__dict__Úupdate)ÚselfÚkwdsr   r   r   Ú__init__v  s    zwalk.<locals>.Bunch.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   ÚBunchu  s   r…   )rZ   Úpattern_listrx   ry   rz   Úresultsc                    s¬   |D ]}t j t j ||¡¡}| jr<t j |¡r<t j |¡rl| jrZt j |¡rZt j |¡rl| j	rt j |¡r| j
D ] }ˆ  ||¡rr| j |¡  qqrq| js¨g |d d …< d S r|   )r   r   Únormpathr   ry   Úisfiler^   rx   Úisdirrz   r†   r:   r‡   r6   rZ   )ÚargÚdirnamer<   ÚnameÚfullnameÚpattern©r:   r   r   Úvisitz  s*    
ÿþ
þ
ýü
ü
zwalk.<locals>.visit)r:   r5   r   rt   ÚAttributeErrorr   rˆ   r   rŠ   r^   r6   r‰   r‡   )rY   ru   rZ   rx   ry   rz   r†   Ú_walkr‡   rŒ   ra   r<   r   rŽ   r   r…   r‹   r‘   r   r   r   rt   8  s^    



ÿþ
þ


ÿþ
þ

ÿrt   c                 C   sH   |s
t j}| |¡D ].}t j || ¡}t j |¡rt j |¡  S qdS )a“  get the full path for the given name string on the given search path.

    Args:
        name (str): name of file, folder, etc to find.
        path (str): path string (e.g. '/Users/foo/bin:/bin:/sbin:/usr/bin').
        pathsep (str, default=None): path separator (e.g. ``:``)

    Returns:
        the full path string.

    Notes:
        if pathsep is not provided, the OS default will be used.
    N)r   r+   r5   r   r   Úexistsr   )r   r   r+   Ú_pathÚ	candidater   r   r   r     s    
r   c                 C   sˆ   |du rt }|stj}tj || ¡}tj |¡}ddl}zt ||¡ |W S  ty‚   t	 
¡ d }|j|jks|tj |¡s~‚ Y n0 dS )a  create a new directory in the root directory

    create a directory at *path* and any necessary parents (i.e. ``mkdir -p``).
    Default mode is read/write/execute for 'user' and 'group', and then
    read/execute otherwise.

    Args:
        path (str): string name of the new directory.
        root (str, default=None): path at which to build the new directory.
        mode (str, default=None): octal read/write permission [default: 0o775].

    Returns:
        string absolute path for new directory.
    Nr   )ÚMODEr   r   r   r   r   ÚerrnoÚmakedirsÚOSErrorrH   Úexc_infoÚEEXISTrŠ   )r   rY   ÚmodeZnewdirZabsdirr˜   Úerrr   r   r   Úmkdir¢  s    
rŸ   c                 C   sf   ddl }| d¡ d| ¡} | d¡ d| ¡} | d¡ d| ¡} | d	¡ d
| ¡} | d¡ d| ¡} | S )a'  parse the given command to be formatted for remote shell invocation

    secure shell (``ssh``) can be used to send and execute commands to remote
    machines (using ``ssh <hostname> <command>``). Additional escape characters
    are needed to enable the command to be correctly formed and executed
    remotely. *shellsub* attemps to parse the given command string correctly
    so that it can be executed remotely with ssh.

    Args:
        command (str): the command to be executed remotely.

    Returns:
        the parsed command string.
    r   Nz\'z\\'z\"z\\"z\$z\\$z\(z\\(z\)z\\))ÚreÚcompileÚsub)rw   r    r   r   r   Úshellsub¾  s    r£   Ú__main__)r*   )N)TF)F)TTF)NTNF)r{   TFTT)N)NN)!Ú__doc__r   rH   Ú
subprocessr   r   r   rK   Z_diskr   Úevalr—   r   r!   r&   r   r%   r9   r   rU   rJ   r\   rt   r   rŸ   r£   ZgetSHELLZgetHOMEZgetROOTZgetUSERZgetSEPZ	stripDupsr‚   r   r   r   r   Ú<module>
   s<   ÿ


#

,
I
U

-
