o
    :n0j                     @   s  d Z 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Zddlm	Z	 ddl
mZ G dd deZd8d	e	e d
edefddZdefddZdd	e	e dedefddZd9d	e	e de	e defddZdedefddZdedefddZdedefddZdedefdd Zdedefd!d"Zdefd#d$Zd%ed&edefd'd(ZejdededB fd)d*Zdefd+d,Zdefd-d.Zdefd/d0Z defd1d2Z!defd3d4Z"d5e	e defd6d7Z#dS ):zUtility functions.    N)Sequence   )log_infoc                   @   s   e Zd ZdZdS )PatoolErrorzRaised when errors occur.N)__name__
__module____qualname____doc__ r
   r
   T/home/wildlama/miniconda3/envs/lam_a2e/lib/python3.10/site-packages/patoolib/util.pyr      s    r   utf-8cmdencodingreturnc                 C   s   t j| t jd|ddjS )z#Return decoded output from command.Treplace)stdoutcheckr   errors)
subprocessrunPIPEr   )r   r   r
   r
   r   backtick"   s
   r   c                   C   s$   t jdkotjduotj dS )z?Return True iff this script is run with pythonw.exe on Windows.ntNzpythonw.exe)osnamesys
executablelowerendswithr
   r
   r
   r   run_under_pythonw)   s
   
r   	verbosityc                 K   s   |dkrd tt| }td|  t rtj|d< d|d< |dk r(tj|d< |d	k r1tj|d
< |rT|dkrJd dd | D }td|  |	drTd | } tj
| fi |}|jS )zIRun command without error checking.
    @return: command return code
    r    zrunning creationflags inputr   r   stderr, c                 s   s*    | ]\}}| d t t| V  qdS )=N)shell_quotestr).0kvr
   r
   r   	<genexpr>J   s   ( zrun.<locals>.<genexpr>z	    with shell)joinmapshell_quote_ntr   r   r   ZCREATE_NO_WINDOWDEVNULLitemsgetr   
returncode)r   r    kwargsinforesr
   r
   r   r   2   s&   



r   r   ret_okc                 K   s4   t | fi |}||vrd|  d| }t||S )z+Run command and raise PatoolError on error.z	Command `z ' returned non-zero exit status )r   r   )r   r;   r7   retcodemsgr
   r
   r   run_checkedS   s
   r>   valuec                 C   s   t jdkr	t| S t| S )zQuote all shell metacharacters in given string value with strong
    (i.e. single) quotes, handling the single quote especially.
    r   )r   r   r2   shell_quote_unixr?   r
   r
   r   r)   \   s   
r)   c                 C   s   |  dd} d|  dS )zQuote argument for Unix system.'z'\'')r   rA   r
   r
   r   r@   e   s   r@   c                 C   s   d| v r
d|  dS | S )z^Quote argument for Windows system. Modeled after distutils
    _nt_quote_args() function.
    r!   "r
   rA   r
   r
   r   r2   k   s   r2   programc                 C   0   t | }|rt|dg}ttd|tjS dS )zDetermine if the RAR codec is installed for 7z program.
    If installed, `7z i` will print something like
    ...
    Codecs:
    1   D    40301 Rar1
    1   D    40302 Rar2
    1   D    40303 Rar3
    1   D    40305 Rar5
    ...
    iz Rar\d$Ffind_programr   boolresearch	MULTILINErD   Z_7zformatsr
   r
   r   p7zip_supports_rart   s
   rO   c                 C   rE   )zDetermine if COMPRESS (.Z) archives are supported for 7z program.
    If installed, `7z i` will print something like
    ...
    Formats:
    0  ......................  Z        z taz (.tar)  1F 9D
    ...
    rF   zZ\s+z\s+tazFrG   rM   r
   r
   r   p7zip_supports_compress   s
   rP   c                  C   sx   t jdt j} t jdkrt| t } t| t } t| t } t	 }t j
|r:t |D ]}t| t j
||} q-| S )z>Get the list of directories to search for executable programs.PATHr   )r   environr5   defpathr   append_to_pathget_nt_7z_dirget_nt_mac_dirget_nt_winrar_dirget_peazip_addon_dirpathisdirlistdirr0   )rY   Z
peazip_dirsubdirr
   r
   r   system_search_path   s   
r]   rY   	directoryc                 C   s6   t j|r
|| v r| S | t js| t j7 } | | S )zVAdd a directory to the PATH environment variable, if it is a valid
    directory.
    )r   rY   rZ   r   pathsep)rY   r^   r
   r
   r   rT      s
   
rT   c                 C   s   t j| t dS )zLook for given program.)rY   )shutilwhichr]   )rD   r
   r
   r   rH      s   rH   c               	   C   s   ddl } ddl}| d }d}z6|dkr*| dr*| | j|d| j| jB }n| | j|}z| 	|dd W | 
| W S | 
| w  tyP   Y dS w )z9Return 7-Zip directory from registry, or an empty string.r   NzSOFTWARE\7-Zip32bitZ64Pathr#   )winregplatformarchitecturemachiner   OpenKeyHKEY_LOCAL_MACHINEZKEY_READZKEY_WOW64_64KEYQueryValueExZCloseKeyOSError)rd   re   Zpython_bitsZkeynamekeyr
   r
   r   rU      s&   
rU   c                  C   s   d} t j| S )z+Return the Windows program files directory.z%ProgramFiles%)r   rY   
expandvars)Zprogvarr
   r
   r   get_nt_program_dir   s   rn   c                   C      t jt dS )z/Return Monkey Audio Compressor (MAC) directory.zMonkey's Audior   rY   r0   rn   r
   r
   r
   r   rV         rV   c                   C   ro   )zReturn WinRAR directory.ZWinRARrp   r
   r
   r
   r   rW      rq   rW   c                   C   s4   t  dkrdS t  dkrdS t  dkrdS dS )z3Get platform-dependen directory for PeaZip add-ons.Windowsz C:\Program Files\PeaZip\res\bin\Linuxz/usr/lib/peazip/res/bin/Darwinz+/Applications/peazip.app/Content/MacOS/bin/r#   )re   systemr
   r
   r
   r   rX      s   rX   alistc                 C   s>   t | dkrd| dd }| d }| d| S d| S )zCReturn comma separated string, and last entry appended with ' or '.r   r'   Nr%   z or )lenr0   )rv   headtailr
   r
   r   strlist_with_or   s
   
rz   )r   )r:   )$r	   	functoolsr   rJ   re   r   r`   r   collections.abcr   logr   	Exceptionr   r*   r   rI   r   intr   r>   r)   r@   r2   rO   rP   r]   rT   cacherH   rU   rn   rV   rW   rX   rz   r
   r
   r
   r   <module>   s<   	 !			