U
    E¸Öh‡  ã                   @   s  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 d dlmZ d dlmZmZ er¨d dlmZmZ G d	d
„ d
eƒZdd„ Zdd„ Zdd„ Zedd„ ƒZedddZerêedd„ ƒZn
eejƒZdd„ Zdd„ ZdS )é    N)Úcontextmanager)ÚNamedTemporaryFile)Úretry)ÚPY2)Úget_path_uid)ÚMYPY_CHECK_RUNNINGÚcast)ÚBinaryIOÚIteratorc                   @   s   e Zd Zedd„ ƒZdS )ÚNamedTemporaryFileResultc                 C   s   d S ©N© )Úselfr   r   úD./venv/lib/python3.8/site-packages/pip/_internal/utils/filesystem.pyÚfile   s    zNamedTemporaryFileResult.fileN)Ú__name__Ú
__module__Ú__qualname__Úpropertyr   r   r   r   r   r      s   r   c                 C   s    t jdksttdƒsdS tj | ¡s(t‚d }| |krœtj | ¡rˆt ¡ dkrxzt	| ƒ}W n t
k
rn   Y dS X |dkS t | tj¡S q,| tj | ¡ }} q,dS )NÚwin32ÚgeteuidTr   F)ÚsysÚplatformÚhasattrÚosÚpathÚisabsÚAssertionErrorÚlexistsr   r   ÚOSErrorÚaccessÚW_OKÚdirname)r   ZpreviousZpath_uidr   r   r   Úcheck_path_owner   s    r#   c                 C   sr   zt  | |¡ W n\ ttfk
rl   | |fD ]8}zt|ƒ}W n tk
rP   Y q,X |r,t  d| ¡‚q,‚ Y nX dS )z“Wrap shutil.copy2() but map errors copying socket files to
    SpecialFileError as expected.

    See also https://bugs.python.org/issue37700.
    z`%s` is a socketN)ÚshutilÚcopy2r   ÚIOErrorÚ	is_socketÚSpecialFileError)ÚsrcÚdestÚfZis_socket_filer   r   r   Úcopy2_fixed9   s    r,   c                 C   s   t  t | ¡j¡S r   )ÚstatÚS_ISSOCKr   ÚlstatÚst_mode)r   r   r   r   r'   Q   s    r'   c                 c   sb   t dtj | ¡tj | ¡dd8}td|ƒ}z
|V  W 5 |j ¡  t |j 	¡ ¡ X W 5 Q R X dS )z‰Given a path to a file, open a temp file next to it securely and ensure
    it is written to disk after the context reaches its end.
    Fz.tmp)ÚdeleteÚdirÚprefixÚsuffixr   N)
r   r   r   r"   Úbasenamer   r   ÚflushÚfsyncÚfileno)r   r+   Úresultr   r   r   Úadjacent_tmp_fileV   s    

ü


r:   iè  éú   )Zstop_max_delayZ
wait_fixedc                 C   s@   zt  | |¡ W n* tk
r:   t  |¡ t  | |¡ Y nX d S r   )r   Úrenamer   Úremove)r)   r*   r   r   r   Úreplacem   s
    
r>   c                 C   sH   t j | ¡s(t j | ¡}|| kr"q(|} q t jdkr@t  | t j¡S t| ƒS )zgCheck if a directory is writable.

    Uses os.access() on POSIX, tries creating files on Windows.
    Úposix)r   r   Úisdirr"   Únamer    r!   Ú_test_writable_dir_win)r   Úparentr   r   r   Útest_writable_dir|   s    
rD   c                    sÚ   d}d‰ t dƒD ]¼}|d ‡ fdd„t dƒD ƒ¡ }tj | |¡}zt |tjtjB tjB ¡}W nP tk
r° } z2|j	t	j
kr†W Y ¢ q|j	t	jkržW Y ¢ dS ‚ W 5 d }~X Y qX t |¡ t |¡  d	S qtd
ƒ‚d S )NZ(accesstest_deleteme_fishfingers_custard_Z$abcdefghijklmnopqrstuvwxyz0123456789é
   Ú c                 3   s   | ]}t  ˆ ¡V  qd S r   )ÚrandomÚchoice)Ú.0Ú_©Zalphabetr   r   Ú	<genexpr>–   s     z)_test_writable_dir_win.<locals>.<genexpr>é   FTz3Unexpected condition testing for writable directory)ÚrangeÚjoinr   r   ÚopenÚO_RDWRÚO_CREATÚO_EXCLr   ÚerrnoZEEXISTÚEPERMÚcloseÚunlinkÚEnvironmentError)r   r5   ÚirA   r   ÚfdÚer   rK   r   rB      s&     

ÿrB   ) rT   r   Zos.pathrG   r$   r-   r   Ú
contextlibr   Ztempfiler   Zpip._vendor.retryingr   Zpip._vendor.sixr   Zpip._internal.utils.compatr   Zpip._internal.utils.typingr   r   Útypingr	   r
   r   r#   r,   r'   r:   Z_replace_retryr>   rD   rB   r   r   r   r   Ú<module>   s6   
	
