o
    f}”eÊ"  ã                	   @   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mZm	Z	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yc   e d¡ Y nw z
ddlZee7 ZW n ey|   e d	¡ Y nw d
efdd„Zded
e	e fdd„Z dee d
e!fdd„Z"deded
e!fdd„Z#ded
dfdd„Z$ded
e!fdd„Z%d'dedede!d
dfd d!„Z&deded
dfd"d#„Z'	d(deded$e
e d
dfd%d&„Z(dS ))zUtilities related archives.
é    N)ÚIterableÚListÚOptional)ÚZipInfo)ÚInstallationError)ÚBZ2_EXTENSIONSÚTAR_EXTENSIONSÚXZ_EXTENSIONSÚZIP_EXTENSIONS)Ú
ensure_dirzbz2 module is not availablezlzma module is not availableÚreturnc                  C   s   t  d¡} t  | ¡ | S )zBGet the current umask which involves having to set it temporarily.r   )ÚosÚumask)Úmask© r   úf/Users/abhinavbhinder/Documents/map/venv/lib/python3.10/site-packages/pip/_internal/utils/unpacking.pyÚcurrent_umask+   s   

r   Úpathc                 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_dir2   s   ÿÿÿr   Úpathsc                 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    Ú	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Úcommonprefix)r!   r"   Zabs_directoryZ
abs_targetr   r   r   r   Úis_within_directoryM   s   r%   c                 C   s   t  | dtƒ  @ dB ¡ dS )zx
    Make file present at path have execute for user/group/world
    (chmod +x) is no-op on windows per python docs
    iÿ  éI   N)r   Úchmodr   r   r   r   r   Ú2set_extracted_file_to_default_mode_plus_executableX   s   r(   Úinfoc                 C   s$   | j d? }t|ot |¡o|d@ ƒS )Né   r&   )Úexternal_attrÚboolÚstatÚS_ISREG)r)   Úmoder   r   r   Úzip_item_is_executable`   s   
r0   TÚfilenameÚlocationÚflattenc              	   C   sJ  t |ƒ t| dƒ}z–tj|dd�}t| ¡ ƒo|}| ¡ D ]{}|j}|}|r,t|ƒd }t	j
 ||¡}t	j
 |¡}	t||ƒsId}
t|
 | ||¡ƒ‚| d¡sS| d¡rXt |ƒ qt |	ƒ | |¡}z)t|dƒ�}t ||¡ W d	  ƒ n1 sxw   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Úinfolistr1   r   r   r   ÚjoinÚdirnamer%   r   ÚformatÚendswithÚshutilÚcopyfileobjÚcloser0   r(   )r1   r2   r3   ZzipfpÚzipÚleadingr)   ÚnameÚfnÚdirÚmessageÚfpÚdestfpr   r   r   Ú
unzip_fileg   sF   	

ÿ

ÿ€€þ
ÿærK   c                 C   s.  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�}zÎt	dd„ | 
¡ D ƒƒ}| 
¡ D ]·}|j}|r`t|ƒd }tj ||¡}t||ƒswd}t| | ||¡ƒ‚| ¡ r€t |ƒ qS| ¡ r¨z| ||¡ W qS ty§ }	 zt d| |j|	¡ W Y d}	~	qSd}	~	ww z| |¡}
W n ttfyÌ }	 zt d| |j|	¡ W Y d}	~	qSd}	~	ww t tj |¡ƒ |
dusÛJ ‚t|dƒ�}t |
|¡ W d  ƒ n1 sñw   Y  |
 ¡  | ||¡ |jd@ �r
t |ƒ qSW | ¡  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 after being
    written.  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   )rE   )Ú.0Úmemberr   r   r   Ú
<listcomp>¯   s    zuntar_file.<locals>.<listcomp>r   zQThe tar file ({}) has a file ({}) trying to install outside target directory ({})z/In the tar file %s the member %s is invalid: %sNr6   r&   )!r   Úlowerr?   r   r	   ÚloggerÚwarningÚtarfiler7   r    Ú
getmembersrE   r   r   r   r<   r%   r   r>   ÚisdirÚissymÚ_extract_memberÚ	ExceptionÚextractfileÚKeyErrorÚAttributeErrorr=   r@   rA   rB   Úutimer/   r(   )r1   r2   r/   ÚtarrD   rO   rF   r   rH   ÚexcrI   rJ   r   r   r   Ú
untar_file•   s‚   	þ
ÿ
ü€÷ü€÷
ÿ€Ï3r`   Úcontent_typec                 C   sš   t j | ¡} |dks|  ¡  t¡st | ¡r#t| ||  d¡ d� d S |dks7t	 
| ¡s7|  ¡  tt t ¡r>t| |ƒ d S t d| ||¡ td|› �ƒ‚)Nzapplication/zipz.whl)r3   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   ÚrealpathrQ   r?   r
   r8   Ú
is_zipfilerK   rT   Ú
is_tarfiler   r   r	   r`   rR   Úcriticalr   )r1   r2   ra   r   r   r   Úunpack_fileæ   s,   ÿþýÿþýûrf   )T)N))Ú__doc__Úloggingr   r@   r-   rT   r8   Útypingr   r   r   r   Úpip._internal.exceptionsr   Úpip._internal.utils.filetypesr   r   r	   r
   Úpip._internal.utils.miscr   Ú	getLoggerÚ__name__rR   ÚSUPPORTED_EXTENSIONSÚbz2ÚImportErrorÚdebugÚlzmaÚintr   Ústrr   r,   r    r%   r(   r0   rK   r`   rf   r   r   r   r   Ú<module>   sX    
ÿÿ.Týÿþýü