a
    Sicu"                     @   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y   ed Y n0 zddlZee7 ZW n ey   ed	 Y n0 ed
ddZee	e dddZ ee e!dddZ"eee!dddZ#eddddZ$ee!dddZ%d%eee!ddddZ&eeddd d!Z'd&eee
e dd"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   Y/var/www/html/django/DPS/env/lib/python3.9/site-packages/pip/_internal/utils/unpacking.pycurrent_umask+   s    

r   )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_dir2   s    r   )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   Z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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(   )infor   c                 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flattenr   c              	   C   sR  t | t| d}z.tj|dd}t| o2|}| D ]}|j}|}|rZt|d }t	j
||}t	j
|}	t||sd}
t|
| |||ds|drt | q<t |	 ||}zTt|d}t|| W d	   n1 s0    Y  W |  t|r6t| q<|  t|r4t| 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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   s<    	



,


rK   )r1   r2   r   c                 C   sX  t | |  ds$|  dr*d}nL|  tr>d}n8|  trRd}n$|  drfd}ntd|  d	}tj| |d
d}zt	dd |
 D }|
 D ]}|j}|rt|d }tj||}t||sd}t|| ||| rt | q| rhz||| W nD tyd }	 z*td| |j|	 W Y d}	~	qW Y d}	~	n
d}	~	0 0 qz||}
W nH ttfy }	 z*td| |j|	 W Y d}	~	qW Y d}	~	n
d}	~	0 0 t tj| |
dusJ t|d}t|
| W d   n1 s0    Y  |
  ||| |jd@ rt | qW |  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 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>       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   st    	



$",
ra   )r1   r2   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)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   realpathrR   r?   r
   r8   
is_zipfilerK   rU   
is_tarfiler   r   r	   ra   rS   criticalr   )r1   r2   rb   r   r   r   unpack_file   s,    rg   )T)N))__doc__loggingr   r@   r-   rU   r8   typingr   r   r   r   pip._internal.exceptionsr   pip._internal.utils.filetypesr   r   r	   r
   pip._internal.utils.miscr   	getLogger__name__rS   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmaintr   strr   r,   r    r%   r(   r0   rK   ra   rg   r   r   r   r   <module>   sL   
.S 