a
    ñ»j¬2  ã                   @  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yÒ   e d	¡ Y n0 zddlZee7 ZW n e�y   e d
¡ Y n0 ddœdd„Zdddœdd„Zdddœdd„Z ddddœdd„Z!ddœdd„Z"dddœdd „Z#d!dd"œd#d$„Z$d8ddddd&œd'd(„Z%dddd)œd*d+„Z&d,d-dd.œd/d0„Z'ddd,ddd1œd2d3„Z(d9ddd4dd5œd6d7„Z)dS ):z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Úint)Úreturnc                  C  s   t  d¡} t  | ¡ | S )zBGet the current umask which involves having to set it temporarily.r   )ÚosÚumask)Úmask© r   úCE:\soccer\ml_env\Lib\site-packages\pip/_internal/utils/unpacking.pyÚcurrent_umask-   s    

r   Ústrz	list[str])Úpathr   c                 C  sh   |   d¡  d¡} d| v rHd| v r4|  d¡|  d¡k s<d| vrH|  dd¡S d| v r\|  dd¡S | dgS d S )Nú/ú\é   Ú )ÚlstripÚfindÚsplit©r   r   r   r   Úsplit_leading_dir4   s    ÿÿÿr   zIterable[str]Úbool)Úpathsr   c                 C  sD   d}| D ]6}t |ƒ\}}|s" dS |du r0|}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Útargetr   c                 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    r/   r   )Úinfor   c                 C  s$   | j d? }t|o t |¡o |d@ ƒS )Né   r+   )Úexternal_attrr   ÚstatÚS_ISREG)r0   Úmoder   r   r   Úzip_item_is_executablef   s    
r6   T)ÚfilenameÚlocationÚflattenr   c              	   C  sH  t |ƒ t| dƒ}�z$tj|dd�}t| ¡ ƒo2|}| ¡ D ]ð}|j}|}|rZt|ƒd }t	j
 ||¡}t	j
 |¡}	t||ƒs”d}
t|
 | ||¡ƒ‚| d¡r¨t |ƒ q<t |	ƒ | |¡}zTt|dƒ�}t ||¡ W d  ƒ n1 sè0    Y  W | ¡  t|ƒ�r,t|ƒ q<| ¡  t|ƒ�r*t|ƒ 0 q<W | ¡  n
| ¡  0 dS )	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Úinfolistr7   r   r   r   ÚjoinÚdirnamer*   r   ÚformatÚendswithÚshutilÚcopyfileobjÚcloser6   r/   )r7   r8   r9   ÚzipfpÚzipÚleadingr0   ÚnameÚfnÚdirÚmessageÚfpÚdestfpr   r   r   Ú
unzip_filem   s<    	

ÿ


,

þ
rR   )r7   r8   r   c                   s`  t ˆƒ ˆ ¡  d¡s$ˆ ¡  d¡r*d}nLˆ ¡  t¡r>d}n8ˆ ¡  t¡rRd}n$ˆ ¡  d¡rfd}nt dˆ¡ d	}tjˆ|d
d�}zÊt	dd„ | 
¡ D ƒƒ}z
tj‰ W n  tyÈ   tˆˆ||ƒ Y n~0 tƒ ‰|�r| 
¡ D ]<}t|jƒ\}}||_| ¡ rÞt|jƒ\}}	||krÞ|	|_qÞddddœ‡ ‡‡‡fdd„}
|jˆ|
d� W | ¡  n
| ¡  0 dS )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   )rL   )Ú.0Úmemberr   r   r   Ú
<listcomp>¶   ó    zuntar_file.<locals>.<listcomp>útarfile.TarInfor   )rV   r   r   c              
     s®   | j }zJzˆ | ˆƒ} W n6 tjyL   tjd d… dv rFt | ˆ¡} n‚ Y n0 W n: tjyŠ } z d}t| ˆ|¡ƒ‚W Y d }~n
d }~0 0 |  	¡ r¤|d@ r¤ˆ| _ nd | _ | S )Né   >   )rZ   é	   é   )rZ   é   é   )rZ   é
   é   z%Invalid member in the tar file {}: {}r+   )
r5   ÚtarfileZLinkOutsideDestinationErrorÚsysÚversion_infoZ
tar_filterÚTarErrorr   rD   Úisfile)rV   r   Z	orig_modeÚexcrO   ©Údata_filterZdefault_mode_plus_executabler7   r8   r   r   Ú
pip_filterÎ   s(    
þÿzuntar_file.<locals>.pip_filter)ÚfilterN)r
   ÚlowerrE   r   r   ÚloggerÚwarningra   r=   r#   Ú
getmembersrh   ÚAttributeErrorÚ_untar_without_filterr,   r   rL   ÚislnkÚlinknameÚ
extractallrH   )r7   r8   r5   ÚtarrK   rV   Z	name_leadZ	name_restZlnk_leadZlnk_restri   r   rg   r   Ú
untar_file›   sB    	þ
'ru   ztarfile.TarFilerY   )rt   Útarinfor   c                 C  s\   t j t j |j¡|j¡}t j |¡}| dd¡}z|  |¡ W dS  t	yV   Y dS 0 dS )zGCheck if the file pointed to by the symbolic link is in the tar archiver   r   TFN)
r   r   rB   rC   rL   rr   ÚnormpathÚreplaceÚ	getmemberÚKeyError)rt   rv   rr   r   r   r   Úis_symlink_target_in_tarû   s    
r{   )r7   r8   rt   rK   r   c                 C  sÀ  |  ¡ D �]°}|j}|r$t|ƒd }tj ||¡}t||ƒsRd}t| | ||¡ƒ‚| 	¡ rdt
|ƒ q| ¡ ræt||ƒs�d}t| | |j|j¡ƒ‚z| ||¡ W nB tyâ } z*t d| |j|¡ W Y d}~qW Y d}~n
d}~0 0 qz| |¡}	W nH ttf�y< } z*t d| |j|¡ W Y d}~qW Y d}~n
d}~0 0 t
tj |¡ƒ |	du�s\J ‚t|dƒ�}
t |	|
¡ W d  ƒ n1 �sŠ0    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+   )rn   rL   r   r   r   rB   r*   r   rD   Úisdirr
   Úissymr{   rr   Ú_extract_memberÚ	Exceptionrl   rm   Úextractfilerz   ro   rC   r=   rF   rG   rH   Úutimer5   r/   )r7   r8   rt   rK   rV   rM   r   rO   rf   rP   rQ   r   r   r   rp   	  s\    

ÿ

ÿÿü$ü",
rp   z
str | None)r7   r8   Úcontent_typer   c                 C  sš   t j | ¡} |dks,|  ¡  t¡s,t | ¡rDt| ||  d¡ d� nR|dkslt	 
| ¡sl|  ¡  tt t ¡rxt| |ƒ nt d| ||¡ td|› �ƒ‚d S )Nzapplication/zipz.whl)r9   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   Úrealpathrk   rE   r	   r>   Ú
is_zipfilerR   ra   Ú
is_tarfiler   r   r   ru   rl   Úcriticalr   )r7   r8   r‚   r   r   r   Úunpack_fileN  s,    ÿþýÿþýûr‡   )T)N)*Ú__doc__Ú
__future__r   Úloggingr   rF   r3   rb   ra   r>   Úcollections.abcr   r   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r   r	   Úpip._internal.utils.miscr
   Ú	getLoggerÚ__name__rl   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmar   r   r#   r*   r,   r/   r6   rR   ru   r{   rp   r‡   r   r   r   r   Ú<module>   sL   
.`H ý