o
    6‘ðiá4  ã                   @  sˆ  d Z ddlm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 ddlmZ ddlmZmZmZmZ ddlmZ e e¡Zee Zz
ddlZee7 ZW n eyi   e d	¡ Y nw z
ddlZee7 ZW n ey‚   e d
¡ Y nw 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$dAdBd*d+„Z%dCd,d-„Z&dDd2d3„Z'dEd5d6„Z(	dFdGd9d:„Z)dS )HzUtilities related archives.é    )ÚannotationsN)ÚIterable)ÚZipInfo)ÚInstallationError)ÚBZ2_EXTENSIONSÚTAR_EXTENSIONSÚXZ_EXTENSIONSÚZIP_EXTENSIONS)Ú
ensure_dirzbz2 module is not availablezlzma module is not availableÚreturnÚintc                  C  s   t  d¡} t  | ¡ | S )zBGet the current umask which involves having to set it temporarily.r   )ÚosÚumask)Úmask© r   ú\/root/parts/websockify/install/lib/python3.10/site-packages/pip/_internal/utils/unpacking.pyÚcurrent_umask-   s   

r   ÚpathÚstrú	list[str]c                 C  sd   |   d¡  d¡} d| v r$d| v r|  d¡|  d¡k sd| vr$|  dd¡S d| v r.|  dd¡S | dgS )Nú/ú\é   Ú )ÚlstripÚfindÚsplit©r   r   r   r   Úsplit_leading_dir4   s   $r   ÚpathsúIterable[str]Úboolc                 C  sD   d}| D ]}t |ƒ\}}|s dS |du r|}q||kr dS qdS )zyReturns true if all the paths have the same leading path name
    (i.e., everything is in one subdirectory in an archive)NFT)r   )r   Úcommon_prefixr   ÚprefixÚrestr   r   r   Úhas_leading_dir@   s   ÿr%   Ú	directoryÚtargetc                 C  s0   t j | ¡}t j |¡}t j ||g¡}||kS )zL
    Return true if the absolute path of target is within the directory
    )r   r   ÚabspathÚ
commonpath)r&   r'   Úabs_directoryÚ
abs_targetr#   r   r   r   Úis_within_directoryO   s   r,   c                   C  s   dt ƒ  @ dB S )Niÿ  éI   )r   r   r   r   r   Ú!_get_default_mode_plus_executableZ   s   r.   ÚNonec                 C  s   t  | tƒ ¡ dS )zx
    Make file present at path have execute for user/group/world
    (chmod +x) is no-op on windows per python docs
    N)r   Úchmodr.   r   r   r   r   Ú2set_extracted_file_to_default_mode_plus_executable^   s   r1   Úinfor   c                 C  s$   | j d? }t|ot |¡o|d@ ƒS )Né   r-   )Úexternal_attrr!   ÚstatÚS_ISREG)r2   Úmoder   r   r   Úzip_item_is_executablef   s   
r8   TÚfilenameÚlocationÚflattenc              	   C  s@  t |ƒ t| dƒ}z‘tj|dd�}t| ¡ ƒo|}| ¡ D ]v}|j}|}|r,t|ƒd }t	j
 ||¡}t	j
 |¡}	t||ƒsId}
t|
 | ||¡ƒ‚| d¡rSt |ƒ qt |	ƒ | |¡}z)t|dƒ�}t ||¡ W d  ƒ n1 ssw   Y  W | ¡  t|ƒr…t|ƒ q| ¡  t|ƒr“t|ƒ w w W | ¡  dS | ¡  w )	aŸ  
    Unzip the file (with path `filename`) to the destination `location`.  All
    files are written based on system defaults and umask (i.e. permissions are
    not preserved), except that regular file members with any execute
    permissions (user, group, or world) have "chmod +x" applied after being
    written. Note that for windows, any execute changes using os.chmod are
    no-ops per the python docs.
    ÚrbT)Ú
allowZip64r   zQThe zip file ({}) has a file ({}) trying to install outside target directory ({}))r   r   ÚwbN)r
   ÚopenÚzipfileÚZipFiler%   ÚnamelistÚinfolistr9   r   r   r   ÚjoinÚdirnamer,   r   ÚformatÚendswithÚshutilÚcopyfileobjÚcloser8   r1   )r9   r:   r;   ÚzipfpÚzipÚleadingr2   ÚnameÚfnÚdirÚmessageÚfpÚdestfpr   r   r   Ú
unzip_filem   sF   	

ÿ


ÿ€€þ
ÿærT   c                   sb  t ˆƒ ˆ ¡  d¡sˆ ¡  d¡rd}n&ˆ ¡  t¡rd}nˆ ¡  t¡r)d}nˆ ¡  d¡r3d}nt dˆ¡ d	}tjˆ|d
d�}zht	dd„ | 
¡ D ƒƒ}ztj‰ W n tyd   tˆˆ||ƒ Y nAw tƒ ‰|r�| 
¡ D ]}t|jƒ\}}||_| ¡ rŒt|jƒ\}}	||krŒ|	|_qnd‡ ‡‡‡fdd„}
|jˆ|
d� W | ¡  dS W | ¡  dS | ¡  w )a¡  
    Untar the file (with path `filename`) to the destination `location`.
    All files are written based on system defaults and umask (i.e. permissions
    are not preserved), except that regular file members with any execute
    permissions (user, group, or world) have "chmod +x" applied on top of the
    default.  Note that for windows, any execute changes using os.chmod are
    no-ops per the python docs.
    z.gzz.tgzzr:gzzr:bz2zr:xzz.tarÚrz-Cannot determine compression type for file %szr:*zutf-8)Úencodingc                 S  s   g | ]}|j ‘qS r   )rN   )Ú.0Úmemberr   r   r   Ú
<listcomp>¶   s    zuntar_file.<locals>.<listcomp>rX   útarfile.TarInfor   r   r   c              
     s¤   | j }z%zˆ | ˆƒ} W n tjy&   tjd d… dv r#t | ˆ¡} n‚ Y nw W n tjy? } z
d}t| ˆ|¡ƒ‚d }~ww |  	¡ rM|d@ rMˆ| _ | S d | _ | S )Né   >   ©r[   é	   é   ©r[   é
   é   ©r[   é   é   z%Invalid member in the tar file {}: {}r-   )
r7   ÚtarfileÚLinkOutsideDestinationErrorÚsysÚversion_infoÚ
tar_filterÚTarErrorr   rF   Úisfile)rX   r   Ú	orig_modeÚexcrQ   ©Údata_filterÚdefault_mode_plus_executabler9   r:   r   r   Ú
pip_filterÎ   s4   
þõ€þÿ€ü
ÿzuntar_file.<locals>.pip_filter)ÚfilterN)rX   rZ   r   r   r   rZ   )r
   ÚlowerrG   r   r   ÚloggerÚwarningre   r?   r%   Ú
getmembersro   ÚAttributeErrorÚ_untar_without_filterr.   r   rN   ÚislnkÚlinknameÚ
extractallrJ   )r9   r:   r7   ÚtarrM   rX   Ú	name_leadÚ	name_restÚlnk_leadÚlnk_restrq   r   rn   r   Ú
untar_file›   sJ   	þ
ÿ€'Æ:r�   r|   útarfile.TarFileÚtarinforZ   c                 C  sX   t j t j |j¡|j¡}t j |¡}| dd¡}z|  |¡ W dS  t	y+   Y dS w )zGCheck if the file pointed to by the symbolic link is in the tar archiver   r   TF)
r   r   rD   rE   rN   rz   ÚnormpathÚreplaceÚ	getmemberÚKeyError)r|   rƒ   rz   r   r   r   Úis_symlink_target_in_tarû   s   
ÿrˆ   rM   c                 C  sž  |  ¡ D ]È}|j}|rt|ƒd }tj ||¡}t||ƒs(d}t| | ||¡ƒ‚| 	¡ r1t
|ƒ q| ¡ rkt||ƒsGd}t| | |j|j¡ƒ‚z| ||¡ W q tyj } zt d| |j|¡ W Y d}~qd}~ww z| |¡}	W n ttfy� } zt d| |j|¡ W Y d}~qd}~ww t
tj |¡ƒ |	dusžJ ‚t|dƒ�}
t |	|
¡ W d  ƒ n1 s´w   Y  |	 ¡  | ||¡ |jd@ rÌt|ƒ qdS )z/Fallback for Python without tarfile.data_filterr   zQThe tar file ({}) has a file ({}) trying to install outside target directory ({})z/In the tar file %s the member %s is invalid: %sNr>   r-   )rv   rN   r   r   r   rD   r,   r   rF   Úisdirr
   Úissymrˆ   rz   Ú_extract_memberÚ	Exceptionrt   ru   Úextractfiler‡   rw   rE   r?   rH   rI   rJ   Úutimer7   r1   )r9   r:   r|   rM   rX   rO   r   rQ   rm   rR   rS   r   r   r   rx   	  sj   

ÿ

ÿÿü€÷ü€÷
ÿ
€Èrx   Úcontent_typeú
str | Nonec                   sè   t j ˆ ¡‰ ˆ  d¡ ‰d‡ ‡‡fdd„}d‡ ‡fdd„}|dkr$|ƒ S |d	kr+|ƒ S ˆ  ¡  t¡r5|ƒ S ˆ  ¡  tt t ¡rC|ƒ S t	 
ˆ ¡}t ˆ ¡}|rT|sT|ƒ S |r[|s[|ƒ S |re|ret d
ˆ ¡ t dˆ ˆ|¡ tdˆ› �ƒ‚)zÑUnpack ``filename`` into ``location``.

    Archive format is chosen in order of decreasing reliability:
    ``content_type``, then filename extension, then magic signature
    (unambiguous matches only).
    z.whlr   r/   c                     s   t ˆ ˆˆd� d S )N)r;   )rT   r   ©r9   r:   Úzip_flattenr   r   Ú_unzip\  s   zunpack_file.<locals>._unzipc                     s   t ˆ ˆƒ d S ©N)r�   r   )r9   r:   r   r   Ú_untar_  s   zunpack_file.<locals>._untarzapplication/zipzapplication/x-gzipzAmbiguous file signature in %s.zZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of N)r   r/   )r   r   ÚrealpathrG   rs   r	   r   r   r   r@   Ú
is_zipfilere   Ú
is_tarfilert   ÚerrorÚcriticalr   )r9   r:   r�   r“   r•   r—   r˜   r   r‘   r   Úunpack_fileN  s6   

ûr›   )r   r   )r   r   r   r   )r   r    r   r!   )r&   r   r'   r   r   r!   )r   r   r   r/   )r2   r   r   r!   )T)r9   r   r:   r   r;   r!   r   r/   )r9   r   r:   r   r   r/   )r|   r‚   rƒ   rZ   r   r!   )
r9   r   r:   r   r|   r‚   rM   r!   r   r/   r”   )r9   r   r:   r   r�   r�   r   r/   )*Ú__doc__Ú
__future__r   Úloggingr   rH   r5   rg   re   r@   Úcollections.abcr   r   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r   r	   Úpip._internal.utils.miscr
   Ú	getLoggerÚ__name__rt   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmar   r   r%   r,   r.   r1   r8   rT   r�   rˆ   rx   r›   r   r   r   r   Ú<module>   sR    
ÿÿ







.
`
Hý