o
    [h.                  
   @   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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yg   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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e d e d&ej*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   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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   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#   abs_directory
abs_targetr   r   r   r   is_within_directoryM   s   r(   c                   C   s   dt   @ dB S )Ni  I   )r   r   r   r   r   !_get_default_mode_plus_executableX   s   r*   c                 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,   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_executabled   s   
r4   Tfilenamelocationflattenc              	   C   sJ  t | t| d}zt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|rt| q|  t|rt| 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infolistr5   r   r   r   joindirnamer(   r   formatendswithshutilcopyfileobjcloser4   r,   )r5   r6   r7   zipfpzipleadingr-   namefndirmessagefpdestfpr   r   r   
unzip_filek   sF   	




rP   c                    sr  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}zpt	dd |
 D }ztj W n tyd   t|| Y nIw t |r|
 D ]}t|j\}}||_| rt|j\}}	||kr|	|_qndtjdtdtjf 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   )rJ   ).0memberr   r   r   
<listcomp>   s    zuntar_file.<locals>.<listcomp>rT   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   >   )rV   	      )rV         )rV   
      z%Invalid member in the tar file {}: {}r)   )
r3   tarfileLinkOutsideDestinationErrorsysversion_info
tar_filterTarErrorr   rB   isfile)rT   r   Z	orig_modeexcrM   data_filterZdefault_mode_plus_executabler5   r6   r   r   
pip_filter   s4   

zuntar_file.<locals>.pip_filter)filterN)r   lowerrC   r   r	   loggerwarningr]   r;   r!   
getmembersrf   AttributeError_untar_without_filterr*   r   rJ   islnklinknameTarInfostr
extractallrF   )r5   r6   r3   tarrI   rT   Z	name_leadZ	name_restZlnk_leadZlnk_restrg   r   re   r   
untar_file   sJ   	
$':ru   rt   rI   c                 C   sz  |  D ]}|j}|rt|d }tj||}t||s(d}t|| |||	 r1t
| q| rYz||| W q tyX } 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sJ t|d}
t|	|
 W d   n1 sw   Y  |	  ||| |jd@ rt| 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)   )rl   rJ   r   r   r   r@   r(   r   rB   isdirr   issym_extract_member	Exceptionrj   rk   extractfileKeyErrorrm   rA   r;   rD   rE   rF   utimer3   r,   )r5   r6   rt   rI   rT   rK   r   rM   rd   rN   rO   r   r   r   rn      s^   



rn   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)r7   zapplication/x-gzipzZCannot unpack file %s (downloaded from %s, content-type: %s); cannot detect archive formatz#Cannot determine archive format of )r   r   realpathri   rC   r
   r<   
is_zipfilerP   r]   
is_tarfiler   r   r	   ru   rj   criticalr   )r5   r6   r}   r   r   r   unpack_file3  s(   r   )T)N)-__doc__loggingr   rD   r1   r_   r]   r<   typingr   r   r   r   pip._internal.exceptionsr   pip._internal.utils.filetypesr   r   r	   r
   pip._internal.utils.miscr   	getLogger__name__rj   SUPPORTED_EXTENSIONSbz2ImportErrordebuglzmaintr   rr   r   r0   r!   r(   r*   r,   r4   rP   ru   TarFilern   r   r   r   r   r   <module>   sr    
.`
=