o
    ú§ÝjÅ9  ã                   @  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œdAdd„Z!d>d d!„Z"dBd#d$„Z#dCd'd(„Z$dDdEd-d.„Z%dFd/d0„Z&dGd5d6„Z'dHd8d9„Z(	dIdJd<d=„Z)dS )Kz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   úX/var/www/html/chatgpt/venv/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   Zcommon_prefixr   ÚprefixÚrestr   r   r   Úhas_leading_dir@   s   ÿr$   F©Úresolve_symlinksÚ	directoryÚtargetr&   c                C  sN   |rt j | ¡}t j |¡}nt j | ¡}t j |¡}||kp&| |t j ¡S )a  
    Return true if the absolute path of target is within the directory
    (including when target is equal to the directory).

    When ``resolve_symlinks`` is true, resolve symlinks before comparing so
    traversal through a symlink (e.g. "link/../file") is also caught.
    )r   r   ÚrealpathÚabspathÚ
startswithÚsep)r'   r(   r&   Zabs_directoryZ
abs_targetr   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_executablec   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_executableg   s   r2   Úinfor   c                 C  s$   | j d? }t|ot |¡o|d@ ƒS )Né   r.   )Úexternal_attrr!   ÚstatÚS_ISREG)r3   Úmoder   r   r   Úzip_item_is_executableo   s   
r9   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Úinfolistr:   r   r   r   ÚjoinÚdirnamer-   r   ÚformatÚendswithÚshutilÚcopyfileobjÚcloser9   r2   )r:   r;   r<   ZzipfpÚzipÚleadingr3   ÚnameÚfnÚdirÚmessageÚfpÚdestfpr   r   r   Ú
unzip_filev   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.   )
r8   ÚtarfileZLinkOutsideDestinationErrorÚsysÚversion_infoZ
tar_filterÚTarErrorr   rG   Úisfile)rX   r   Z	orig_modeÚexcrQ   ©Údata_filterZdefault_mode_plus_executabler:   r;   r   r   Ú
pip_filter×   s4   
þõ€þÿ€ü
ÿzuntar_file.<locals>.pip_filter)ÚfilterN)rX   rZ   r   r   r   rZ   )r
   ÚlowerrH   r   r   ÚloggerÚwarningrb   r@   r$   Ú
getmembersri   ÚAttributeErrorÚ_untar_without_filterr/   r   rN   ZislnkÚlinknameÚ
extractallrK   )r:   r;   r8   ÚtarrM   rX   Z	name_leadZ	name_restZlnk_leadZlnk_restrj   r   rh   r   Ú
untar_file¤   sJ   	þ
ÿ€'Æ:ru   rt   ú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   rE   rF   rN   rr   ÚnormpathÚreplaceZ	getmemberÚKeyError)rt   rw   rr   r   r   r   Úis_symlink_target_in_tar  s   
ÿr{   rM   c                 C  sì  |  ¡ D ]ï}|j}|rt|ƒd }tj ||¡}t||ƒr$t||dd�s/d}t| | ||¡ƒ‚| 	¡ r8t
|ƒ q| ¡ r’tj tj |¡|j¡}t||dd�s\d}t| | |j|j¡ƒ‚t||ƒsnd}t| | |j|j¡ƒ‚z| ||¡ W q ty‘ }	 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   Tr%   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.   )ro   rN   r   r   r   rE   r-   r   rG   Úisdirr
   ZissymrF   rr   r{   Ú_extract_memberÚ	Exceptionrm   rn   Zextractfilerz   rp   r@   rI   rJ   rK   Úutimer8   r2   )r:   r;   rt   rM   rX   rO   r   rQ   r(   rg   rR   rS   r   r   r   rq     s|   
ÿÿ
ÿÿ
ÿÿü€÷ü€÷
ÿ
€·rq   Ú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   r0   c                     s   t ˆ ˆˆd� d S )N)r<   )rT   r   ©r:   r;   Zzip_flattenr   r   Ú_unzipv  s   zunpack_file.<locals>._unzipc                     s   t ˆ ˆƒ d S ©N)ru   r   )r:   r;   r   r   Ú_untary  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   r0   )r   r   r)   rH   rl   r	   r   r   r   rA   Ú
is_zipfilerb   Ú
is_tarfilerm   ÚerrorÚcriticalr   )r:   r;   r€   rƒ   r…   r†   r‡   r   r‚   r   Úunpack_fileh  s6   

ûrŠ   )r   r   )r   r   r   r   )r   r    r   r!   )r'   r   r(   r   r&   r!   r   r!   )r   r   r   r0   )r3   r   r   r!   )T)r:   r   r;   r   r<   r!   r   r0   )r:   r   r;   r   r   r0   )rt   rv   rw   rZ   r   r!   )
r:   r   r;   r   rt   rv   rM   r!   r   r0   r„   )r:   r   r;   r   r€   r�   r   r0   )*Ú__doc__Ú
__future__r   Úloggingr   rI   r6   rc   rb   rA   Úcollections.abcr   r   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r   r	   Úpip._internal.utils.miscr
   Ú	getLoggerÚ__name__rm   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmar   r   r$   r-   r/   r2   r9   rT   ru   r{   rq   rŠ   r   r   r   r   Ú<module>   sT    
ÿÿ


ÿ



.
`
Yý